프로그래밍/예전글
Unity 컴퍼넌트 복사(Copy component)
Cat체셔
2016. 7. 28. 14:19
출처 : http://answers.unity3d.com/questions/458207/copy-a-component-at-runtime.html
1 2 3 4 5 6 7 8 9 10 11 12 | 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; } | cs |
Type.GetFiels
(https://msdn.microsoft.com/ko-kr/library/ch9714z3(v=vs.110).aspx)
- 현재 타입에서 public 필드를 가져오는 함수입니다. [SerializeField]는 가져오지 않습니다.