예제 #1
0
        public static void RecordComponent(MonoBehaviour component)
        {
            System.Type type = component.GetType();
            if (type == typeof(MonoBehaviour))
            {
                return;
            }

            List <FieldInfo> serializableFields = new List <FieldInfo>();

            while (type != typeof(MonoBehaviour))
            {
                serializableFields.AddRange(type.GetSerializableFields());
                type = type.BaseType();
            }


            bool endRecord = false;

            if (!IsRecording)
            {
                endRecord = true;
                BeginRecord();
            }

            for (int i = 0; i < serializableFields.Count; ++i)
            {
                RecordValue(component, serializableFields[i]);
            }

            if (endRecord)
            {
                EndRecord();
            }
        }
예제 #2
0
        public static T CopyComponent <T>(GameObject p_destination, T p_original, bool p_pasteAsValue = true, bool p_deleteOldOne = false) where T : Component
        {
            if (p_destination != null && p_original != null)
            {
                System.Type v_type = p_original.GetType();
                Component   v_copy = null;
                if (p_deleteOldOne)
                {
                    v_copy = p_destination.GetComponent(v_type);
                    if (v_copy != null)
                    {
                        Object.DestroyImmediate(v_copy);
                    }
                }
                if (p_pasteAsValue)
                {
                    v_copy = p_destination.GetComponent(v_type);
                }
                if (v_copy == null)
                {
                    v_copy = p_destination.AddComponent(v_type);
                }
                System.Reflection.FieldInfo[] v_fields = new System.Reflection.FieldInfo[0];
                while (v_type != null && v_type != typeof(Component) && v_type != typeof(Behaviour) && v_type != typeof(MonoBehaviour))
                {
                    //All Privates and Publics in this Specific Type (Avoid BaseType Component, Behaviour and MonoBehaviour to prevent bugs)
                    v_fields = v_type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Public);
                    foreach (System.Reflection.FieldInfo v_field in v_fields)
                    {
#if UNITY_WINRT && !UNITY_EDITOR
                        var v_customAtts = v_field.GetCustomAttributes(typeof(SerializeField), true) as object[];
                        if (v_customAtts != null && v_customAtts.Length > 0)
                        {
                            object v_newValue = v_field.GetValue(p_original);
                            v_field.SetValue(v_copy, v_newValue);
                        }
#else
                        if (!v_field.IsNotSerialized && System.Attribute.IsDefined(v_field, typeof(SerializeField)))
                        {
                            object v_newValue = v_field.GetValue(p_original);
                            v_field.SetValue(v_copy, v_newValue);
                        }
#endif
                    }
#if UNITY_WINRT && !UNITY_EDITOR && !UNITY_WP8
                    v_type = v_type.BaseType();
#else
                    v_type = v_type.BaseType;
#endif
                }

                if (v_copy is Behaviour && p_original is Behaviour)
                {
                    (v_copy as Behaviour).enabled = (p_original as Behaviour).enabled;
                }


                return(v_copy as T);
            }
            return(null);
        }