summaryrefslogtreecommitdiff
path: root/Assets/Plugins/AdvancedInspector/Core/ComponentMonoBehaviour.cs
blob: e5d048840f05c548b4ebb74fe38e85f15c94d9ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace AdvancedInspector
{
    /// <summary>
    /// ScriptableObject are not serializable within the scope of a GameObject.
    /// Therefore, they are improper to prefab, copy and so on.
    /// This class' goal is to provide a polymorphic solution and give an easy way of handling up front.
    /// 
    /// Derived from Object; no polymorphism supported.
    /// Derived from ScriptableObject; cannot be prefabed, copied, duplicated or instanced.
    /// Derived from MonoBehaviour; can only live on a GameObject.
    /// 
    /// A ComponentMonoBehaviour is created from another MonoBehaviour; its parent.
    /// A parent can be another ComponentMonoBehaviour.
    /// If the parent is destroyed, the subcomponent is destroyed too.
    /// If a subcomponent is found without a parent, it's destroyed too.
    /// </summary>
    [AdvancedInspector]
    public abstract class ComponentMonoBehaviour : MonoBehaviour
    {
        [SerializeField]
        private MonoBehaviour owner;

        /// <summary>
        /// The owner of a "subcomponent".
        /// Use to know if this component lost its parent.
        /// If so, the AdvancedInspector will delete any unused component.
        /// </summary>
        public MonoBehaviour Owner
        {
            get { return owner; }
            set
            {
                if (value != null)
                    owner = value;
            }
        }

        /// <summary>
        /// A subcomponent is not visible the normal way in the Inspector.
        /// It's shown as being part of another item.
        /// </summary>
        protected virtual void Reset()
        {
            hideFlags = HideFlags.HideInInspector;
        }

        /// <summary>
        /// Called when the inspector is about to destroy this one.
        /// Loop in all the internal and destroy sub-components.
        /// </summary>
        public void Erase()
        {
            foreach (FieldInfo info in GetFields(GetType(), false))
            {
                object value = info.GetValue(this);

                if (value is ComponentMonoBehaviour)
                {
                    ComponentMonoBehaviour component = value as ComponentMonoBehaviour;

                    if (component.Owner == Owner)
                        component.Erase();
                }
            }

            DestroyImmediate(this, true);
        }

        /// <summary>
        /// Instanciate an existing Component on the same owner GameObject
        /// </summary>
        public ComponentMonoBehaviour Instantiate()
        {
            return Instantiate(gameObject, Owner);
        }

        /// <summary>
        /// Instanciate an existing Component on the same owner GameObject but with a new onwer.
        /// </summary>
        public ComponentMonoBehaviour Instantiate(MonoBehaviour owner)
        {
            return Instantiate(gameObject, owner);
        }

        /// <summary>
        /// Instanciate an existing Component on the target GameObject.
        /// </summary>
        public ComponentMonoBehaviour Instantiate(GameObject go, MonoBehaviour owner)
        {
            return CopyObject(go, owner, this) as ComponentMonoBehaviour;
        }

        private static object CopyObject(GameObject go, MonoBehaviour owner, object original)
        {
            if (original == null)
                return null;

            Type type = original.GetType();

            if (type == typeof(string))

                return ((string)original).Clone();
            else if (type.Namespace == "System")
                return original;
            else if (typeof(IList).IsAssignableFrom(type))
                return CopyList(go, owner, (IList)original);
            else if (typeof(ComponentMonoBehaviour).IsAssignableFrom(type) && ((ComponentMonoBehaviour)original).Owner == owner)
                return CopyComponent(go, owner, (ComponentMonoBehaviour)original);
            else if (typeof(Component).IsAssignableFrom(type))
                return original;
            else if (typeof(ScriptableObject).IsAssignableFrom(type))
                return ScriptableObject.Instantiate((ScriptableObject)original);
            else if (typeof(UnityEngine.Object).IsAssignableFrom(type))
                return original;
            else if (type.IsClass)
                return CopyClass(go, owner, original);
            else
                return original;
        }

        private static IList CopyList(GameObject go, MonoBehaviour owner, IList original)
        {
            Type type = original.GetType();
            IList copy;

            if (type.IsArray)
            {
                copy = Array.CreateInstance(type.GetElementType(), original.Count);
                for (int i = 0; i < original.Count; i++)
                    copy[i] = CopyObject(go, owner, original[i]);
            }
            else
            {
                copy = Activator.CreateInstance(type) as IList;
                for (int i = 0; i < original.Count; i++)
                    copy.Add(CopyObject(go, owner, original[i]));

            }

            return copy;
        }

        private static ComponentMonoBehaviour CopyComponent(GameObject go, MonoBehaviour owner, ComponentMonoBehaviour original)
        {
            Type type = original.GetType();
            ComponentMonoBehaviour copy = go.AddComponent(original.GetType()) as ComponentMonoBehaviour;

            foreach (FieldInfo info in GetFields(type, false))
            {
                if (info.IsLiteral)
                    continue;

                info.SetValue(copy, CopyObject(go, copy, info.GetValue(original)));
            }

            copy.Owner = owner;

            return copy;
        }

        private static object CopyClass(GameObject go, MonoBehaviour owner, object original)
        {
            Type type = original.GetType();
            object copy = Activator.CreateInstance(type);

            foreach (FieldInfo info in GetFields(type, false))
            {
                if (info.IsLiteral)
                    continue;

                info.SetValue(copy, CopyObject(go, owner, info.GetValue(original)));
            }

            return copy;
        }

        private static List<FieldInfo> GetFields(Type type, bool recursive)
        {
            List<FieldInfo> infos;

            if (recursive)
                infos = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance).ToList();
            else
                infos = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).ToList();

            if (type.BaseType != null && type.BaseType != typeof(object))
                infos.AddRange(GetFields(type.BaseType, true));

            return infos;
        }
    }
}