forked from XINCGer/UnityToolchainsTrick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MissingComponentChecker.cs
68 lines (62 loc) · 2.4 KB
/
MissingComponentChecker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace ToolKits
{
public class MissingComponentChecker
{
private const string PATH = "Assets/Editor/Examples/Example_08_MissingComponentChecker/Prefabs";
private const string PrefabNameColor = "#00FF00";
private const string PathColor = "#0000FF";
[MenuItem("Tools/CheckMissingComponent", priority = 8)]
private static void MissingComponentCheck()
{
var guids = AssetDatabase.FindAssets("t:GameObject", new[] {PATH});
var length = guids.Length;
var index = 1;
foreach (var guid in guids)
{
var path = AssetDatabase.GUIDToAssetPath(guid);
EditorUtility.DisplayProgressBar("玩命检查中", "玩命检查中..." + path, (float) index / length);
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
FindMissingComponentRecursive(prefab, prefab, path);
index++;
}
EditorUtility.ClearProgressBar();
EditorUtility.DisplayDialog("提示", "检查结束", "OK");
}
private static void FindMissingComponentRecursive(GameObject gameObject, GameObject prefab, string path)
{
var cmps = gameObject.GetComponents<Component>();
for (int i = 0; i < cmps.Length; i++)
{
if (null == cmps[i])
{
Debug.LogError(
$"<color={PrefabNameColor}>{GetRelativePath(gameObject, prefab)}</color> has missing components! path is: <color={PathColor}>{path}</color>"
);
}
}
foreach (Transform trans in gameObject.transform)
{
FindMissingComponentRecursive(trans.gameObject, prefab, path);
}
}
private static string GetRelativePath(GameObject gameObject, GameObject prefab)
{
if (null == gameObject.transform.parent)
{
return gameObject.name;
}
else if (gameObject == prefab)
{
return gameObject.name;
}
else
{
return GetRelativePath(gameObject.transform.parent.gameObject, prefab) + "/" + gameObject.name;
}
}
}
}