예제 #1
0
        static void DefaultApply(GameObject obj)
        {
            foreach (var pi in obj.GetComponentsInChildren <PEPrefabScript>(true))
            {
                pi.BuildModifications();
            }

            var gameObject = obj;
            var prefabType = PrefabUtility.GetPrefabType(gameObject);

            if (prefabType == PrefabType.PrefabInstance || prefabType == PrefabType.DisconnectedPrefabInstance)
            {
                var gameObject2 = PEUtils.FindRootPrefab(gameObject);

                if (gameObject2 == null)
                {
                    return;
                }

                var prefabParent = PrefabUtility.GetPrefabParent(gameObject2);
                var assetPath    = AssetDatabase.GetAssetPath(prefabParent);

                var method     = typeof(Provider).GetMethod("PromptAndCheckoutIfNeeded", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
                var canReplace = (bool)method.Invoke(null, new object[] {
                    new [] {
                        assetPath
                    }, "The version control requires you to checkout the prefab before applying changes."
                });

                if (canReplace)
                {
                    PrefabUtility.ReplacePrefab(gameObject2, prefabParent, ReplacePrefabOptions.ConnectToPrefab);
                }
            }
        }
        static internal PEPrefabScript GetDiffWith(this PEPrefabScript _this)
        {
            var go = _this.ParentPrefab != null &&
                     (PrefabUtility.GetPrefabParent(_this.gameObject) == _this.Prefab ||
                      PEUtils.FindRootPrefab(_this.gameObject) == _this.Prefab)
                                ? _this.ParentPrefab :
                     _this.Prefab;

            return(go != null?go.GetComponent <PEPrefabScript>() : null);
        }
예제 #3
0
        static public void ApplyPrefab(GameObject[] targets)
        {
            var list = new List <GameObject>();

            foreach (var target in targets)
            {
                var root = PEUtils.FindRootPrefab(target);
                list.RemoveAll(r => r == root);
                list.Add(root);
            }

            foreach (GameObject target in list)
            {
                ApplyPrefab(target);
            }
        }
예제 #4
0
        static public void ApplyPrefab(GameObject gameObject)
        {
            gameObject = PEUtils.FindRootPrefab(gameObject);

            var pi = gameObject.GetComponent <PEPrefabScript>();

            if (pi == null)
            {
                DefaultApply(gameObject);
                DoAutoSave();
            }
            else
            {
                DoApply(pi);
            }
        }
        protected override void OnHeaderGUI()
        {
            if (EditorApplication.isPlayingOrWillChangePlaymode)
            {
                base.OnHeaderGUI();
                return;
            }
            var   rect                = GUILayoutUtility.GetRect(0, 0);
            float buttonWidth         = (rect.width - 53f - 5f) / 3f;
            var   applyButtonPosition = new Rect(53f + buttonWidth * 2f, 53f + rect.y, buttonWidth, 15f);

            var list = new List <GameObject>();

            foreach (var t in this.targets)
            {
                var go = t as GameObject;
                if (go == null)
                {
                    continue;
                }

                var root = PEUtils.FindRootPrefab(go);
                list.RemoveAll(r => r == root);
                list.Add(root);
            }

            if (targets.Length > 1)
            {
                base.OnHeaderGUI();

                if (list.Count > 0 && GUILayout.Button("Apply " + list.Count + (list.Count > 1 ? " Prefabs" : " Prefab"), EditorStyles.miniButton))
                {
                    Apply();
                }
            }
            else
            {
                var gameObject = (target as GameObject);
                var prefabType = PrefabUtility.GetPrefabType(gameObject);

                var isPrefab                 = prefabType == PrefabType.Prefab;
                var isPrefabInstance         = prefabType == PrefabType.PrefabInstance || prefabType == PrefabType.DisconnectedPrefabInstance;
                var isPrefabOrPrefabInstance = prefabType == PrefabType.Prefab || isPrefabInstance;

                gameObject = PEUtils.FindRootPrefab(gameObject);

                if (gameObject == null)
                {
                    gameObject = target as GameObject;
                }

                var prefabInstance = gameObject != null?gameObject.GetComponent <PEPrefabScript>() : null;

                var anyPrefabInstance = gameObject != null && gameObject.GetComponentsInChildren <PEPrefabScript>(true).Any();
                var isNestedPrefab    = prefabInstance != null;

                if (isPrefabInstance && (isNestedPrefab || anyPrefabInstance))
                {
                    var c = GUI.color;
                    GUI.color = new Color(0.95f, 1, 0.95f, 1);

                    if (applyButtonPosition.Contains(Event.current.mousePosition))
                    {
                        if (Event.current.type == EventType.MouseUp)
                        {
                            Event.current.mousePosition = Vector2.one * 5000f;
                            Apply();
                        }
                        if (Event.current.isMouse)
                        {
                            var pos = Event.current.mousePosition;
                            Event.current.mousePosition = Vector2.one * 5000f;
                            base.OnHeaderGUI();
                            Event.current.mousePosition = pos;
                        }
                        else
                        {
                            base.OnHeaderGUI();
                        }
                    }
                    else
                    {
                        base.OnHeaderGUI();
                    }
                    GUI.Button(applyButtonPosition, "Nested Apply", EditorStyles.miniButtonRight);
                    GUI.color = c;
                }
                else
                {
                    base.OnHeaderGUI();
                    if (isPrefab && (isNestedPrefab || anyPrefabInstance))
                    {
                        GUILayout.Space(2);
                        if (GUILayout.Button("Apply", EditorStyles.miniButton))
                        {
                            Apply();
                        }
                    }
                }
                var isRoot = PEUtils.FindRootPrefab(target as GameObject) == target;
                if (isPrefabOrPrefabInstance && !isNestedPrefab && isRoot)
                {
                    GUILayout.Space(3);
                    if (GUILayout.Button(new GUIContent("Allow this prefab to be nested"), EditorStyles.miniButton))
                    {
                        PEUtils.MakeNested(gameObject);
                    }
                }
            }

            /*var prefabsWinthoutPrefabScript = list.Where(p => PrefabUtility.GetPrefabType(p) == PrefabType.Prefab && p.GetComponent<PEPrefabScript>() == null).ToArray();
             * if (prefabsWinthoutPrefabScript.Length > 0)
             * {
             *      GUILayout.Space(3);
             *      if (GUILayout.Button(new GUIContent("Allow this prefabs to be nested"), EditorStyles.miniButton))
             *              prefabsWinthoutPrefabScript.Foreach(PEUtils.MakeNested);
             * }*/

            if (PEPrefs.DrawOnHeader && targets.Length == 1)
            {
                var go = (target as GameObject);
                if (go != null)
                {
                    var prefabScript = go.GetComponent <PEPrefabScript>();

                    if (prefabScript)
                    {
                        PEPrefabScriptEditor.DrawView(prefabScript);
                    }
                }
            }
        }
예제 #6
0
        static void OnItemGUI(int instanceID, Rect rect)
        {
            var instance = EditorUtility.InstanceIDToObject(instanceID) as GameObject;

            if (instance == null)
            {
                return;
            }

            var prefabInstance = instance.GetComponent <PEPrefabScript>();

            var isPrefab = PrefabUtility.GetPrefabParent(instance) && PEUtils.FindRootPrefab(instance) == instance;

            if (prefabInstance)
            {
                if (Event.current.type == EventType.Repaint && PEUtils.FindRootPrefab(Selection.activeGameObject) == instance && Selection.activeGameObject != instance)
                {
                    EditorGUI.DrawRect(rect, new Color(0, 1, 0, 0.25f));
                }
                bool rootPrefab = PrefabUtility.GetPrefabParent(prefabInstance.gameObject) == prefabInstance.Prefab;
                var  color      = GUI.color;
                GUI.color = rootPrefab ? Color.green : (Color.yellow);

                if (!prefabInstance.enabled)
                {
                    GUI.color = Color.white;
                }

                if (prefabInstance.Prefab == null)
                {
                    GUI.color = Color.red;
                }

                const int width = 15;
                var       br    = rect;
                br.height -= 2;
                br.y      += 2 / 2;
                br.x      += br.width - width;
                br.width   = width;

                var content = new GUIContent(PEResources.icon, prefabInstance.Prefab ? prefabInstance.Prefab.name : "Missiog prefab with guid: " + prefabInstance.PrefabGUID);
                var click   = GUI.Button(br, content, emptyStyle);

                GUI.color = color;

                var evt = Event.current;
                if (prefabInstance.Prefab && (evt.type == EventType.ContextClick || click || evt.type == EventType.MouseUp))
                {
                    var mousePos = evt.mousePosition;
                    if (br.Contains(mousePos))
                    {
                        var menu = new GenericMenu();
                        BuildMenu(menu, prefabInstance, rootPrefab);

                        menu.ShowAsContext();
                        evt.Use();
                    }
                }
            }
            else
            if (isPrefab)
            {
                var click = PEPrefs.AutoPrefabs;
                if (click)
                {
                    MakeNested(instance);
                }
            }
        }
예제 #7
0
        private static void OnPropertyContextMenu(GenericMenu menu, SerializedProperty property)
        {
            var component  = property.serializedObject.targetObject as Component;
            var gameObject = property.serializedObject.targetObject as GameObject;

            GameObject targetGameObject = null;

            if (component != null)
            {
                targetGameObject = component.gameObject;
            }

            if (gameObject != null)
            {
                targetGameObject = gameObject;
            }

            if (targetGameObject == null)
            {
                return;
            }

            var prefabRoot = PEUtils.FindRootPrefab(targetGameObject);

            if (prefabRoot == null)
            {
                return;
            }

            var prefabScript = prefabRoot.GetComponent <PEPrefabScript>();

            if (prefabScript == null)
            {
                return;
            }

            if (!PEExposedPropertiesEditor.CanEditProperties(new[] { prefabScript }))
            {
                return;
            }

            var exposed = prefabScript.Properties.HasProperty(property.serializedObject.targetObject, property.propertyPath);

            property = property.Copy();
            menu.AddItem(new GUIContent("Expose Property"), exposed, () =>
            {
                if (!exposed)
                {
                    prefabScript.Properties.Add(new ExposedProperty()
                    {
                        Label = property.displayName, PropertyPath = property.propertyPath, Target = property.serializedObject.targetObject
                    });
                    PEExposedPropertiesEditor.current.needRebuild = true;
                    EditorUtility.SetDirty(prefabScript);
                }
                else
                {
                    prefabScript.Properties.Remove(property.serializedObject.targetObject, property.propertyPath);
                    PEExposedPropertiesEditor.current.needRebuild = true;
                    EditorUtility.SetDirty(prefabScript);
                }
            });
        }