在父Prefab中嵌套子Prefab,那么如果对这个嵌套Prefab进行修改,改变将不会应用到子Prefab中;同理,对子Prefab的修改,也不会应用到这个嵌套Prefab中。因此,就会出现一些问题,例如子Prefab已经是最新的了,但是嵌套Prefab却是旧的。在unity2018.3中,已经有新功能来处理嵌套Prefab的问题了,但是之前的版本还是得要处理这个问题。
1.替换Prefab
如图,Button是父Prefab,RawImage是子Prefab,Button下的RawImage是嵌套Prefab
选中嵌套Prefab,右键选择RefreshPrefab,即可将其替换为子Prefab
PrefabEditor.cs
1 using UnityEngine; 2 using System.Collections.Generic; 3 using UnityEditor; 4 5 public class PrefabEditor { 6 7 private static string[] searchFolders = new string[] { "Assets/Prefab", }; //查找路径 8 9 [MenuItem("GameObject/RefreshPrefab", priority = 1)] 10 static void RefreshPrefab() 11 { 12 List<string> paths = new List<string>(); 13 var ids = AssetDatabase.FindAssets("t:Prefab", searchFolders); 14 for (int i = 0; i < ids.Length; i++) 15 { 16 var path = AssetDatabase.GUIDToAssetPath(ids[i]); 17 paths.Add(path); 18 } 19 20 var goes = Selection.gameObjects; 21 for (int i = 0; i < goes.Length; i++) 22 { 23 var go = goes[i]; 24 25 //找出对应的Project中的prefab 26 GameObject prefabGo = null; 27 for (int j = 0; j < paths.Count; j++) 28 { 29 if (paths[j].Contains(go.name + ".prefab")) 30 { 31 prefabGo = AssetDatabase.LoadAssetAtPath<GameObject>(paths[j]); 32 break; 33 } 34 } 35 36 //替换 37 if (prefabGo) 38 { 39 var pos = go.transform.localPosition; 40 var rot = go.transform.localEulerAngles; 41 var sca = go.transform.localScale; 42 43 GameObject tempGo = PrefabUtility.ConnectGameObjectToPrefab(go, prefabGo); 44 tempGo.transform.localPosition = pos; 45 tempGo.transform.localEulerAngles = rot; 46 tempGo.transform.localScale = sca; 47 } 48 } 49 } 50 }