2014년 3월 17일 월요일

[Unity3d]Component를 다른 오브젝트로 복사하고 싶을경우

(http://answers.unity3d.com/questions/458207/copy-a-component-at-runtime.html)
<이함수를 사용하면 된다>



static public T CopyComponent<T>(T original, GameObject destination) where T : Component
    {
        System.Type type = original.GetType();
        Component copy = destination.AddComponent(type);
        System.Reflection.FieldInfo[] fields = type.GetFields();
        foreach (System.Reflection.FieldInfo field in fields)
        {
            field.SetValue(copy, field.GetValue(original));
        }
        return copy as T;
    }

댓글 없음:

댓글 쓰기