• 在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
  • 相关阅读:
    [RxJS] sample
    [Whole Web] SQL INJECTION
    [RxJS] Using iif
    [React] Animate SVG Paths with Framer Motion
    bash 教程5 shell 流程控制 条件判断 重定向 read [MD]
    bash 教程4 shell 脚本 调试 环境 [MD]
    『前端算法』数组合并两个有序数组
    块设备读写测试
    IO栈整体认知
    MySQL [Warning] Can’t create test file xxx lowertest(转)
  • 原文地址:https://www.cnblogs.com/AaronBlogs/p/7993696.html
Copyright © 2020-2023  润新知