public void AddComponent(Type type)
        {
            conflictingMembers.Clear();
            AddComponentUtility.GetConflictingMembers(type, target, ref conflictingMembers);

            int conflictCount = conflictingMembers.Count;

                        #if DEV_MODE
            Debug.Log(GetType().Name + ".AddComponent(" + (type == null ? "null" : type.Name) + ") called with conflictCount=" + conflictCount + "!");
                        #endif

            bool viewWasLocked = inspector.State.ViewIsLocked;

            if (conflictCount == 0)
            {
                if (OnComponentAdded != null)
                {
                    OnComponentAdded(type);
                }

                target.AddComponent(type, true);
                Close();
                return;
            }

            if (type == typeof(Transform))
            {
                var gameObjectDrawers = inspector.State.drawers.Members;
                for (int d = gameObjectDrawers.Length - 1; d >= 0; d--)
                {
                    var gameObjectDrawer = gameObjectDrawers[d] as IGameObjectDrawer;
                    if (gameObjectDrawer != null)
                    {
                        var gameObjects = gameObjectDrawer.GameObjects;
                        for (int g = gameObjectDrawers.Length - 1; g >= 0; g--)
                        {
                            var gameObject    = gameObjects[g];
                            var rectTransform = gameObject.GetComponent <RectTransform>();
                            if (rectTransform != null)
                            {
                                Platform.Active.Destroy(rectTransform);
                            }
                        }
                    }
                }
                Close();
                return;
            }

            string header;
            string msg;
            string ok;
            if (conflictCount == 1)
            {
                header = "Conflicting Component";
                msg    = "Component " + type.Name + " cannot be added because it conflicts with existing Component " + conflictingMembers[0].Type.Name;
                ok     = "Replace Existing";
            }
            else
            {
                header = "Conflicting Components";
                msg    = "Component " + type.Name + " cannot be added because it conflicts with the following exiting Components:" + conflictingMembers[0].Name;
                foreach (var member in conflictingMembers)
                {
                    msg += "\n" + member.Type.Name;
                }
                ok = "Replace " + conflictCount + " Existing";
            }

            if (DrawGUI.Active.DisplayDialog(header, msg, ok, "Cancel"))
            {
                inspector.State.ViewIsLocked = true;

                foreach (var member in conflictingMembers)
                {
                    var values = member.GetValues();
                    for (int n = values.Length - 1; n >= 0; n--)
                    {
                        var comp = values[n] as Component;
                        Platform.Active.Destroy(comp);
                    }
                }

                if (OnComponentAdded != null)
                {
                    OnComponentAdded(type);
                }

                target.AddComponent(type, true);
                inspector.State.ViewIsLocked = viewWasLocked;

                Close();
            }
        }
Exemplo n.º 2
0
        public void AddComponent(Type type)
        {
            conflictingMembers.Clear();
            AddComponentUtility.GetConflictingMembers(type, target, ref conflictingMembers);

            int conflictCount = conflictingMembers.Count;

                        #if DEV_MODE
            Debug.Log(GetType().Name + ".AddComponent(" + (type == null ? "null" : type.Name) + ") called with conflictCount=" + conflictCount + "!");
                        #endif

            bool viewWasLocked = inspector.State.ViewIsLocked;

            if (conflictCount == 0)
            {
                if (OnComponentAdded != null)
                {
                    OnComponentAdded(type);
                }

                target.AddComponent(type, true);
                Close();
                return;
            }

            //more reliable method to get components first? then even if Drawer change, component refs will remain the same
            //however locking the inspector should do the same thing
            //var conflictingComponents = conflictingMembers.Select((m)=>m.GetValues()).ToArray();

            //TO DO: handle Transform to RectTransform and vice versa
            //but warn that it requires rebuilding the Components
            //and could result in lost references? Although I guess
            //could even try and retain those too, by seraching through
            //all fields on all objects in hierarchy :D
            if (type == typeof(Transform))
            {
                Debug.LogError("Replacing Transform Not Yet Supported");
                return;
            }
            if (type == typeof(RectTransform))
            {
                Debug.LogError("Replacing RectTransform Not Yet Supported");
                return;
            }

            //TO DO: handle dependencies of all conflicting components too!
            //so first list conflicting components
            //then if they have dependencies, list also "which have dependencies with the following components:"

            string header;
            string msg;
            string ok;
            if (conflictCount == 1)
            {
                header = "Conflicting Component";
                msg    = "Component " + type.Name + " cannot be added because it conflicts with existing Component " + conflictingMembers[0].Type.Name;
                ok     = "Replace Existing";
            }
            else
            {
                header = "Conflicting Components";
                msg    = "Component " + type.Name + " cannot be added because it conflicts with the following exiting Components:" + conflictingMembers[0].Name;
                foreach (var member in conflictingMembers)
                {
                    msg += "\n" + member.Type.Name;
                }
                ok = "Replace " + conflictCount + " Existing";
            }

            if (DrawGUI.Active.DisplayDialog(header, msg, ok, "Cancel"))
            {
                inspector.State.ViewIsLocked = true;

                foreach (var member in conflictingMembers)
                {
                    var values = member.GetValues();
                    for (int n = values.Length - 1; n >= 0; n--)
                    {
                        var comp = values[n] as Component;
                        Platform.Active.Destroy(comp);
                    }
                }

                if (OnComponentAdded != null)
                {
                    OnComponentAdded(type);
                }

                target.AddComponent(type, true);
                inspector.State.ViewIsLocked = viewWasLocked;

                Close();
            }
        }