Esempio n. 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 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));
            }
        }