| | |
| | | using System.Collections; |
| | | using System.Collections.Generic; |
| | | using System.Collections.Generic; |
| | | using UnityEngine; |
| | | using UnityEditor; |
| | | using Snxxz.UI; |
| | | |
| | | public class PrefabHelper |
| | | { |
| | | [InitializeOnLoadMethod] |
| | | static void StartInitializeOnLoadMethod() |
| | | { |
| | | PrefabUtility.prefabInstanceUpdated = delegate (GameObject instance) |
| | | PrefabUtility.prefabInstanceUpdated += OnPrefabInstanceUpdate; |
| | | } |
| | | static void OnPrefabInstanceUpdate(GameObject instance) |
| | | { |
| | | var prefab = PrefabUtility.GetPrefabParent(instance) as GameObject; |
| | | var prefabLoaders = prefab.GetComponentsInChildren<UIPrefabLoader>(); |
| | | string path = UnityEditor.PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(instance); |
| | | |
| | | var rootGO = PrefabUtility.LoadPrefabContents(path); |
| | | |
| | | var prefabLoaders = rootGO.GetComponentsInChildren<UIPrefabLoader>(); |
| | | var dirty = false; |
| | | foreach (var loader in prefabLoaders) |
| | | { |
| | |
| | | { |
| | | children.Add(loader.transform.GetChild(i)); |
| | | } |
| | | |
| | | for (int i = children.Count - 1; i >= 0; i--) |
| | | { |
| | | if (children[i].name == loader.prefabName) |
| | | { |
| | | GameObject.DestroyImmediate(children[i].gameObject, true); |
| | | EditorUtility.SetDirty(loader.gameObject); |
| | | var childGo = children[i].gameObject; |
| | | GameObject.DestroyImmediate(childGo, true); |
| | | DebugEx.LogFormat("保存预制:{0},删除了:{1}", path, loader.prefabName); |
| | | dirty = true; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (dirty) |
| | | { |
| | | AssetDatabase.SaveAssets(); |
| | | PrefabUtility.SaveAsPrefabAsset(rootGO, path); |
| | | PrefabUtility.UnloadPrefabContents(rootGO); |
| | | } |
| | | }; |
| | | } |
| | | |
| | | |
| | | } |