Unity寻找丢失脚本

发表于2018-11-11
评论0 6.7k浏览
如何寻找丢失脚本,并打印出相应路径,下面就给大家分享一个寻找丢失脚本的脚本工具帮助大家,一起来看看吧。

该脚本放在Editor目录下

思路:对于选择的物体,扫描他及他的所有自物体下面的组件,如果发现某个组件为空(只有可能是脚本丢失),打印出该组件的位置(依附在哪个物体上及该物体的路径)
脚本代码如下:
using UnityEngine;
using UnityEditor;
public class FindMissingComponents : EditorWindow
{
        //To find missing Components in Scene or prefab
        //How to use: select the prefab or Gameobjects in Scene which you want to detected whether there are some missing Components
        //Result: print how many Gameobjects/components have been detected and how many missing components found
        //Also pring the path of missing component like "GameConsistantData has an empty Component attached in position: 4"
        static int go_count = 0, components_count = 0, missing_count = 0;
        [MenuItem("Assets/Tool/FindMissingComponents")]
        private static void FindMissingComponentInAllSeletedGO()
        {
                GameObject[] toDetectedGameObjects = Selection.gameObjects;
                go_count = 0;
                components_count = 0;
                missing_count = 0;
                foreach (GameObject g in toDetectedGameObjects)
                {
                        detectGameObject(g);
                }
                Debug.Log(string.Format("Searched {0} GameObjects, {1} components, found {2} missing", go_count, components_count, missing_count));
        }
        private static void detectGameObject(GameObject detectedGO)
        {
                go_count++;
                Component[] components = detectedGO.GetComponents<Component>();
                for (int index = 0; index < components.Length; index++)
                {
                        components_count++;
                        if (components[index] == null)//Missing Component
                        {
                                missing_count++;
                                string missingPath = detectedGO.name;
                                Transform t = detectedGO.transform;
                                while (t.parent != null)
                                {
                                        missingPath = t.parent.name + "/" + missingPath;
                                        t = t.parent;
                                }
                                Debug.Log(missingPath + " has an empty Component attached in position: " + index, detectedGO);
                        }
                }
                foreach (Transform childT in detectedGO.transform) //Find in child of detectedGameObject
                {
                        detectGameObject(childT.gameObject);
                }
        }
}

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

标签: