示例#1
0
        private void CheckPrefab(string guid, GameObject prefab)
        {
            if (string.IsNullOrEmpty(guid) || prefab == null)
            {
                return;
            }

            var nestedInstances = PEUtils.GetNestedInstances(prefab).ToArray();

            foreach (var e in whatCache[guid])
            {
                whereCache[e].Remove(guid);
            }

            var rootInstance = prefab.GetComponent <PEPrefabScript>();

            if (rootInstance != null)
            {
                if (rootInstance.PrefabGUID == guid && !string.IsNullOrEmpty(rootInstance.ParentPrefabGUID))
                {
                    Add(rootInstance.ParentPrefabGUID, guid);
                }
            }

            foreach (var instance in nestedInstances)
            {
                if (instance.PrefabGUID != guid)
                {
                    Add(instance.PrefabGUID, guid);
                }
            }
            setContainsNestedInstances(guid, nestedInstances.Length > 0 || (rootInstance && !string.IsNullOrEmpty(rootInstance.ParentPrefabGUID)));
        }
        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);
        }
        static void Apply(GameObject gameObject)
        {
            gameObject = PrefabUtility.FindPrefabRoot(gameObject);

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

            if (pi == null)
            {
                DefaultApply(gameObject);
                PEUtils.DoAutoSave();
            }
            else
            {
                PEUtils.DoApply(pi);
            }
        }
        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);
            }
        }
        static public void ApplyPrefab(GameObject gameObject)
        {
            gameObject = PEUtils.FindRootPrefab(gameObject);

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

            if (pi == null)
            {
                DefaultApply(gameObject);
                DoAutoSave();
            }
            else
            {
                DoApply(pi);
            }
        }
示例#7
0
        public IEnumerable <GameObject> GetPrefabsWithInstances(string guid)
        {
            var list = this[guid];

            if (list == null || list.Count == 0)
            {
                yield break;
            }

            foreach (var g in this[guid].ToArray())
            {
                var go = PEUtils.GetAssetByGUID <GameObject>(g);
                if (go)
                {
                    yield return(go);
                }
            }
        }
        static internal PEPrefabScript ReplaceInPlace(this PEPrefabScript _this, GameObject prefabObject, bool applyModifications = true, bool keepPrefabLink = true)
        {
            var newObject = PrefabUtility.InstantiatePrefab(prefabObject) as GameObject;

            PEUtils.SetParentAndSaveLocalTransform(newObject.transform, _this.transform.parent);
            newObject.transform.SetSiblingIndex(_this.transform.GetSiblingIndex());

            PrefabUtility.DisconnectPrefabInstance(newObject);
            PrefabUtility.DisconnectPrefabInstance(_this.gameObject);

            var newObjectPrefabInstance = newObject.GetComponent <PEPrefabScript>();

            if (newObjectPrefabInstance == null)
            {
                Debug.LogWarning("EvolvePrefab not found on replacing object");
            }

            newObject.transform.localPosition = _this.transform.localPosition;

            if (prefabObject == _this.ParentPrefab || !applyModifications)
            {
                foreach (var link in _this.Links.Links)
                {
                    if (newObjectPrefabInstance.Links[link] == null)
                    {
                        newObjectPrefabInstance.Links.Links.Add(link);
                    }
                }
            }

            if (applyModifications && newObjectPrefabInstance)
            {
                _this.ApplyModifications(newObjectPrefabInstance);
            }

            var replaceDict = new Dictionary <Object, Object>();

            replaceDict.Add(_this.gameObject, newObject);
            replaceDict.Add(_this.transform, newObject.transform);

            if (newObjectPrefabInstance)
            {
                foreach (var link in _this.Links.Links)
                {
                    var from = link.InstanceTarget;
                    var to   = newObjectPrefabInstance.Links[link.LIIF];

                    if (from == null || to == null)
                    {
                        continue;
                    }

                    if (from == _this.gameObject || from == _this.transform)
                    {
                        continue;
                    }

                    replaceDict.Add(from, to.InstanceTarget);
                }
            }

            var destroyList = new List <GameObject>();

            foreach (var link in _this.Links.Links)
            {
                if (link == null || link.InstanceTarget is Component)
                {
                    continue;
                }

                var go = link.InstanceTarget as GameObject;

                if (!go)
                {
                    continue;
                }

                if (_this.Modifications.NonPrefabObjects.Any(m => m.child == go.transform))
                {
                    continue;
                }

                destroyList.Add(go);
            }

            PEUtils.ReplaceReference(_this.transform.root, replaceDict);

            var npo = _this.Modifications.NonPrefabObjects.Where(tm => tm != null && tm.child != null && tm.parent != null);

            {
                var indID   = 0;
                var indexes = new List <int>(npo.Select(n => n.child.GetSiblingIndex()));
                npo.Foreach(transformModification => PEUtils.SetParentAndSaveLocalTransform(transformModification.child, transformModification.parent));
                npo.Foreach(transformModification => transformModification.child.SetSiblingIndex(indexes [indID++]));
            }
            var reversedComponents = _this.Modifications.NonPrefabComponents.Reverse <PEModifications.ComponentsData> ();
            var newComponents      = new List <Component> ();

            foreach (var nonPrefabComponent in reversedComponents)
            {
                Component newComponent;
                newComponents.Add(newComponent = nonPrefabComponent.child.CopyComponentTo(nonPrefabComponent.parent));
                PEUtils.CopyPrefabInternalData(nonPrefabComponent.child, newComponent);
            }

            var i = 0;

            foreach (var nonPrefabComponent in reversedComponents)
            {
                var newComponent = newComponents[i++];
                if (newComponent)
                {
                    PEUtils.ReplaceReference(nonPrefabComponent.parent.transform.root, nonPrefabComponent.child, newComponent);
                    replaceDict.Add(nonPrefabComponent.child, newComponent);
                }
            }

            if (applyModifications)
            {
                foreach (var transformModification in _this.Modifications.TransformParentChanges)
                {
                    var index = transformModification.child.GetSiblingIndex();
                    PEUtils.SetParentAndSaveLocalTransform(transformModification.child, transformModification.parent);
                    transformModification.child.SetSiblingIndex(index);
                }

                foreach (var id in _this.Modifications.RemovedObjects)
                {
                    var link = newObjectPrefabInstance.Links[id];

                    if (PEPrefs.DebugLevel > 0 && link != null)
                    {
                        Debug.Log(string.Format("Object to remove: {0} {1}", id, link.InstanceTarget));
                    }

                    if (link != null && link.InstanceTarget)
                    {
                        if (PEPrefs.DebugLevel > 0)
                        {
                            Debug.Log(string.Format("Destroy Object: {0}", link.InstanceTarget));
                        }

                        if (link.InstanceTarget == newObject || link.InstanceTarget == newObjectPrefabInstance)
                        {
                            Debug.LogError("Inconsistent Destroying while replacing");
                            continue;
                        }

                        Object.DestroyImmediate(link.InstanceTarget);
                    }
                }
            }

            if (keepPrefabLink)
            {
                newObjectPrefabInstance.ParentPrefab = _this.ParentPrefab;
                newObjectPrefabInstance.Prefab       = _this.Prefab;
            }

            foreach (var kv in replaceDict)
            {
                PEUtils.CopyPrefabInternalData(kv.Key, kv.Value);
            }

            newObject.name = _this.gameObject.name;
            Object.DestroyImmediate(_this.gameObject);

            foreach (var go in destroyList)
            {
                if (go == null)
                {
                    continue;
                }

                if (go == newObject)
                {
                    Debug.LogError("Inconsistend Destroying while replacing");
                    continue;
                }

                Object.DestroyImmediate(go);
            }

            if (newObjectPrefabInstance)
            {
                newObjectPrefabInstance.BuildModifications();
                EditorUtility.SetDirty(newObjectPrefabInstance);

                if (prefabObject == _this.ParentPrefab)
                {
                    newObjectPrefabInstance.Properties = _this.Properties;
                }

                if (newObjectPrefabInstance.gameObject == null)
                {
                    Debug.LogError("New GameObject is destroyed while replacing... o_O");
                }
            }

            try
            {
                newObjectPrefabInstance.gameObject.name = newObjectPrefabInstance.gameObject.name;
            }
            catch (MissingReferenceException)
            {
                Debug.LogError("New EvolvePrefabScript is destroyed while replacing");
            }
            catch (System.NullReferenceException)
            {
            }

            return(newObjectPrefabInstance);
        }
        static internal void CalculateModifications(this PEModifications _this, PEPrefabScript prefab, PEPrefabScript instance)
        {
            instance.Modifications.Modificated.RemoveAll(m => m.Mode == PEModifications.PropertyData.PropertyMode.Default);
            var counter = 0;

            foreach (var link in instance.Links.Links)
            {
                if (link == null || link.InstanceTarget == null || link.InstanceTarget == instance || link.InstanceTarget is PEPrefabScript)
                {
                    continue;
                }

                var so = new SerializedObject(link.InstanceTarget);

                var property = so.GetIterator();

                var prefabObjectLink = prefab.Links[link];
                if (prefabObjectLink == null)
                {
                    continue;
                }

                var prefabObject = prefabObjectLink.InstanceTarget;

                if (prefabObject == null)
                {
                    continue;
                }

                var prefabSerializedObject = new SerializedObject(prefabObject);

                while (property.Next(CheckChild(property)))
                {
                    counter++;
                    if (PEUtils.PropertyFilter(property))
                    {
                        continue;
                    }

                    var prefabProperty = prefabSerializedObject.FindProperty(property.propertyPath);

                    var isArray     = property.propertyPath.Contains(".Array.data[");
                    var isInherited = link.InstanceTarget.GetType().IsSubclassOf(prefabObject.GetType());
                    if (prefabProperty == null && !isArray && !isInherited)
                    {
                        if (PEPrefs.DebugLevel > 0)
                        {
                            Debug.Log("Property not found(Some times its happens) " + property.propertyPath);
                        }
                        continue;
                    }

                    var instanceValue = property.GetPropertyValue();

                    var prefabValue = prefabProperty == null ? null : prefabProperty.GetPropertyValue();
                    var isChanged   = !object.Equals(instanceValue, prefabValue);
                    if (isChanged)
                    {
                        if (property.propertyType == SerializedPropertyType.ObjectReference)
                        {
                            var instanceLink = instance.Links[instanceValue as Object];
                            var prefabLink   = prefab.Links[prefabValue as Object];

                            if (prefabLink != null && instanceLink != null)
                            {
                                isChanged = prefabLink.LIIF != instanceLink.LIIF;
                            }
                        }
                        else
                        {
                            var animationCurve = instanceValue as AnimationCurve;
                            if (animationCurve != null)
                            {
                                isChanged = !PEUtils.Compare(animationCurve, prefabValue as AnimationCurve);
                            }
                        }
                    }

                    if (!isChanged)
                    {
                        continue;
                    }

                    instance.Modifications.AddModification(new PEModifications.PropertyData {
                        Object       = link.InstanceTarget,
                        PropertyPath = property.propertyPath,
                        ObjeckLink   = link.LIIF,
                    });
                }
            }

            instance.Modifications.CalculateStructureDiff(prefab, instance);
        }
示例#10
0
        public void CheckPrefab(params string[] paths)
        {
            paths = paths.Where(p => !PathFilter(p)).ToArray();
            var i = 0;
            var t = paths.Length;

            foreach (var path in paths)
            {
                var importer = AssetImporter.GetAtPath(path);
                var p        = (float)i++ / (float)t;

                if (importer is ModelImporter)
                {
                    if (!modelPathToApply.Contains(path))
                    {
                        continue;
                    }

                    var asset = PEUtils.GetAssetByPath <GameObject>(path);
                    if (asset == null)
                    {
                        continue;
                    }

                    var prefabScript = asset.GetComponent <PEPrefabScript>();
                    if (prefabScript == null)
                    {
                        continue;
                    }

                    modelPathToApply.Remove(path);

                    if (PEPrefs.DebugLevel > 0)
                    {
                        Debug.Log("Applying " + path);
                    }
                    prefabScript.Prefab = asset;
                    prefabScript.BuildLinks();
                    EditorApplication.delayCall += () => PEUtils.DoApply(prefabScript);
                }
                else
                {
                    if (!path.EndsWith(".prefab"))
                    {
                        continue;
                    }

                    CheckPrefab(AssetDatabase.AssetPathToGUID(path), PEUtils.GetAssetByPath <GameObject>(path));

                    if (paths.Length > 10 && i % 50 == 0)
                    {
                        EditorUtility.DisplayProgressBar("Checking prefab dependencies", path, p);
                    }

                    if (i % 100 == 0)
                    {
                        EditorUtility.UnloadUnusedAssets();
                    }
                }
            }
            if (paths.Length > 10)
            {
                EditorUtility.ClearProgressBar();
            }
        }
示例#11
0
        static void ApplyModifications(this PEPrefabScript _this, PEPrefabScript targetInstance)
        {
            targetInstance.Modifications = _this.Modifications;
            var modificatedList = new List <SerializedObject>();

            foreach (var modification in _this.Modifications.Modificated)
            {
                if (modification.Mode == PEModifications.PropertyData.PropertyMode.Ignore)
                {
                    continue;
                }

                if (modification.Object == null)
                {
                    continue;
                }

                var selfObject = new SerializedObject(modification.Object);

                var linkedObject = targetInstance.Links[modification.ObjeckLink];
                if (linkedObject == null || linkedObject.InstanceTarget == null)
                {
                    continue;
                }

                var targetObject = new SerializedObject(linkedObject.InstanceTarget);

                var selfProperty   = selfObject.FindProperty(modification.PropertyPath);
                var targetProperty = targetObject.FindProperty(modification.PropertyPath);

                if (targetProperty == null)
                {
                    Debug.Log("Property not found " + modification.PropertyPath);
                }
                else
                {
                    var target          = targetProperty.serializedObject.targetObject;
                    var propertyPath    = selfProperty.propertyPath;
                    var targetTransform = target as Transform;
                    if (propertyPath == "m_Father" && targetTransform)
                    {
                        PEUtils.SetParentAndSaveLocalTransform(targetTransform, selfProperty.objectReferenceValue as Transform);
                    }
                    else if (propertyPath == "m_RootOrder" && targetTransform)
                    {
                        targetTransform.SetSiblingIndex(selfProperty.intValue);
                    }
                    else
                    {
                        var value = selfProperty.GetPropertyValue();
                        if (selfProperty.propertyType == SerializedPropertyType.ObjectReference)
                        {
                            var selfValue = selfProperty.GetPropertyValue();

                            var selfLink   = _this.Links[selfValue as Object];
                            var targetLink = targetInstance.Links[selfLink];

                            if (targetLink != null)
                            {
                                value = targetLink.InstanceTarget;
                            }
                        }
                        targetProperty.SetPropertyValue(value);
                        targetObject.ApplyModifiedProperties();
                    }
                }
            }

            foreach (var po in modificatedList)
            {
                po.ApplyModifiedProperties();
            }
        }
示例#12
0
        void Apply()
        {
            var objs = targets.OfType <GameObject>();

            PEUtils.ApplyPrefab(objs.ToArray());
        }
 static void MenuMock()
 {
     Debug.Log("Inspector Prefab Apply");
     PEUtils.ApplyPrefab(Selection.gameObjects);
 }
示例#14
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);
                }
            }
        }
示例#15
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);
                }
            });
        }
        static internal void ApplyChanges(this PEPrefabScript _this, bool buildModifications = false)
        {
            if (!_this)
            {
                return;
            }

            if (PEPrefs.DebugLevel > 0)
            {
                Debug.Log(string.Format("[Begin Apply] {0}", _this.name));
            }

            EditorUtility.DisplayProgressBar("Apply changes", _this.name, 0f);

            if (buildModifications)
            {
                foreach (var pi in _this.GetComponentsInChildren <PEPrefabScript>(true))
                {
                    pi.BuildModifications();
                }
            }

            if (recursionCounter++ > 100)
            {
                Debug.LogError("Recursion");
                recursionCounter = 0;
                EditorUtility.ClearProgressBar();
                return;
            }

            _this.BuildLinks(true);
            _this.BuildModifications();

            var newPrefab = !AssetDatabase.Contains(_this.gameObject) ? PrefabUtility.ReplacePrefab(_this.gameObject, _this.Prefab, ReplacePrefabOptions.ConnectToPrefab) : _this.gameObject;

            var prefabs = _this.GetPrefabsWithInstances();

            foreach (var prefab in prefabs)
            {
                if (PEPrefs.DebugLevel > 0)
                {
                    Debug.Log("[Apply] Found Nested instance:" + prefab.name);
                }

                var instantiatedPrefabsList = new List <GameObject>();
                var instances = new List <PEPrefabScript>();
                if (prefab == null)
                {
                    continue;
                }

                var pi = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
                PrefabUtility.DisconnectPrefabInstance(pi);
                var nestedInstances = PEUtils.GetNestedInstances(pi).Where(p => p.PrefabGUID == _this.PrefabGUID && p.enabled).ToArray();

                instances.AddRange(nestedInstances);

                var rootInstance = pi.GetComponent <PEPrefabScript>();
                if (rootInstance && rootInstance.ParentPrefabGUID == _this.PrefabGUID)
                {
                    instances.Insert(0, rootInstance);
                }

                instantiatedPrefabsList.Add(pi);
                var counter = 0;

                foreach (var instance in instances)
                {
                    if (instance == null || instance.gameObject == null)
                    {
                        Debug.LogWarning("[Apply] Huston we have a problem. Prefab is destroyed before replace");
                        continue;
                    }

                    instantiatedPrefabsList.Remove(instance.gameObject);

                    var newObject = instance.ReplaceInPlace(newPrefab);

                    if (newObject == null || newObject.gameObject == null)
                    {
                        Debug.LogWarning("[Apply] Huston we have a problem. Prefab is destroyed after replace");
                        continue;
                    }

                    if (newObject.ParentPrefab == newPrefab)
                    {
                        instantiatedPrefabsList.Add(newObject.gameObject);
                    }

                    PrefabUtility.RecordPrefabInstancePropertyModifications(newObject);
                    EditorUtility.DisplayProgressBar("Apply changes", _this.name + " replaced in " + newObject.gameObject, ((float)counter++) / (float)instances.Count);
                }

                foreach (var instantiatedPrefab in instantiatedPrefabsList)
                {
                    if (instantiatedPrefab == null)
                    {
                        continue;
                    }

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

                    if (instance)
                    {
                        instance.ApplyChanges();
                    }
                    else
                    {
                        PrefabUtility.ReplacePrefab(instantiatedPrefab, PrefabUtility.GetPrefabParent(instantiatedPrefab), ReplacePrefabOptions.ConnectToPrefab);
                    }

                    Object.DestroyImmediate(instantiatedPrefab);
                }
            }

            recursionCounter--;

            if (PEPrefs.DebugLevel > 0)
            {
                Debug.Log(string.Format("[End Apply] {0}", _this.name));
            }
        }
示例#17
0
 static void MenuMock()
 {
     PEUtils.ApplyPrefab(Selection.gameObjects);
 }
示例#18
0
        protected override void OnHeaderGUI()
        {
            if (EditorApplication.isPlayingOrWillChangePlaymode)
            {
                base.OnHeaderGUI();
                return;
            }
            var   rect                = GUILayoutUtility.GetRect(0, 0);
            float buttonWidth         = (rect.width - 52f - 5f) / 3f;
            var   applyButtonPosition = new Rect(52f + buttonWidth * 2f, 44f + 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 = PrefabUtility.FindPrefabRoot(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 = PrefabUtility.FindValidUploadPrefabInstanceRoot(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 (Event.current.type == EventType.MouseUp && applyButtonPosition.Contains(Event.current.mousePosition))
                    {
                        Event.current.mousePosition = Vector2.one * 5000f;
                        Apply();
                    }

                    base.OnHeaderGUI();
                    GUI.color = c;
                }
                else
                {
                    base.OnHeaderGUI();
                    if (isPrefab && (isNestedPrefab || anyPrefabInstance))
                    {
                        GUILayout.Space(2);
                        if (GUILayout.Button("Apply", EditorStyles.miniButton))
                        {
                            Apply();
                        }
                    }
                }
                var isRoot = PrefabUtility.FindValidUploadPrefabInstanceRoot(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);
                    }
                }
            }
        }
示例#19
0
        public void CheckPrefab(params string[] paths)
        {
            var i            = 0;
            var prefabsPaths = paths.Where(p => p.EndsWith(".prefab")).ToArray();

            foreach (var path in prefabsPaths)
            {
                if (prefabsPaths.Length > 5 && i++ % 50 == 0)
                {
                    EditorUtility.DisplayProgressBar("Checking prefab dependencies", path, i / (float)prefabsPaths.Length);
                }

                if (prefabsPaths.Length > 5 && !canPrefabContainsNestedInstances(path))
                {
                    continue;
                }

                CheckPrefab(AssetDatabase.AssetPathToGUID(path), PEUtils.GetAssetByPath <GameObject>(path));

                if (i % 100 != 0)
                {
                    continue;
                }

#if UNITY_5
                EditorUtility.UnloadUnusedAssetsImmediate();
#else
                EditorUtility.UnloadUnusedAssets();
#endif
            }

            foreach (var path in paths.Where(p => !p.EndsWith(".prefab")))
            {
                if (!modelPathToApply.Contains(path))
                {
                    continue;
                }

                var importer = AssetImporter.GetAtPath(path);

                if (!(importer is ModelImporter))
                {
                    continue;
                }

                var asset = PEUtils.GetAssetByPath <GameObject>(path);
                if (asset == null)
                {
                    continue;
                }

                var prefabScript = asset.GetComponent <PEPrefabScript>();
                if (prefabScript == null)
                {
                    continue;
                }

                modelPathToApply.Remove(path);

                if (PEPrefs.DebugLevel > 0)
                {
                    Debug.Log("Applying " + path);
                }
                prefabScript.Prefab = asset;
                prefabScript.BuildLinks();
                EditorApplication.delayCall += () => PEUtils.DoApply(prefabScript);
            }

            EditorUtility.ClearProgressBar();
        }
示例#20
0
 static public void ApplyPrefab(GameObject[] gameObjects)
 {
     PEUtils.ApplyPrefab(gameObjects);
 }