在Unity中查找缺失的引用


這篇博客是查找unity中缺失引用的一個簡單簡短的解決方案。你可以從GitHub上獲取源碼。
缺失引用
一個丟失引用與沒有引用(在檢視表顯示“None”)是完全不同的概念。這些友各種原因造成,比如:把資源文件從Unity編輯器中移除,導致在.meta文件混亂,其中有個指向無效的連接。
主要問題是缺失引用在項目中會被隱藏在某處,只有等運行中找到時已經太晚了。幸運的是,我們編寫了編輯器腳本來補救...
 
解決方案:
以防止你也遇到這種問題,下面是查找缺失引用的所有代碼:
 
 1 using System.Collections;
 2 using System.Linq;
 3 using UnityEditor;
 4 using UnityEngine;
 5   
 6 public class MissingReferencesFinder : MonoBehaviour 
 7 {
 8     [MenuItem("Tools/Show Missing Object References in scene", false, 50)]
 9     public static void FindMissingReferencesInCurrentScene()
10     {
11         var objects = GetSceneObjects();
12         FindMissingReferences(EditorApplication.currentScene, objects);
13     }
14   
15     [MenuItem("Tools/Show Missing Object References in all scenes", false, 51)]
16     public static void MissingSpritesInAllScenes()
17     {
18         foreach (var scene in EditorBuildSettings.scenes.Where(s => s.enabled))
19         {
20             EditorApplication.OpenScene(scene.path);
21             FindMissingReferences(scene.path, GetSceneObjects());
22         }
23     }
24   
25     [MenuItem("Tools/Show Missing Object References in assets", false, 52)]
26     public static void MissingSpritesInAssets()
27     {
28         var allAssets = AssetDatabase.GetAllAssetPaths();
29         var objs = allAssets.Select(a => AssetDatabase.LoadAssetAtPath(a, typeof(GameObject)) as GameObject).Where(a => a != null).ToArray();
30           
31         FindMissingReferences("Project", objs);
32     }
33   
34     private static void FindMissingReferences(string context, GameObject[] objects)
35     {
36         foreach (var go in objects)
37         {
38             var components = go.GetComponents();
39               
40             foreach (var c in components)
41             {
42                 if (!c)
43                 {
44                     Debug.LogError("Missing Component in GO: " + FullPath(go), go);
45                     continue;
46                 }
47                   
48                 SerializedObject so = new SerializedObject(c);
49                 var sp = so.GetIterator();
50                   
51                 while (sp.NextVisible(true))
52                 {
53                     if (sp.propertyType == SerializedPropertyType.ObjectReference)
54                     {
55                         if (sp.objectReferenceValue == null
56                             && sp.objectReferenceInstanceIDValue != 0)
57                         {
58                             ShowError(context, go, c.GetType().Name, ObjectNames.NicifyVariableName(sp.name));
59                         }
60                     }
61                 }
62             }
63         }
64     }
65   
66     private static GameObject[] GetSceneObjects()
67     {
68         return Resources.FindObjectsOfTypeAll()
69             .Where(go => string.IsNullOrEmpty(AssetDatabase.GetAssetPath(go))
70                    && go.hideFlags == HideFlags.None).ToArray();
71     }
72       
73     private const string err = "Missing Ref in: [{3}]{0}. Component: {1}, Property: {2}";
74       
75     private static void ShowError (string context, GameObject go, string c, string property)
76     {
77         Debug.LogError(string.Format(err, FullPath(go), c, property, context), go);
78     }
79       
80     private static string FullPath(GameObject go)
81     {
82         return go.transform.parent == null
83             ? go.name
84                 : FullPath(go.transform.parent.gameObject) + "/" + go.name;
85     }
86 }
作者: Lior Tal


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM