-
Notifications
You must be signed in to change notification settings - Fork 322
/
MissingPrefabChecker.cs
74 lines (65 loc) · 2.62 KB
/
MissingPrefabChecker.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
69
70
71
72
73
74
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace ToolKits
{
public class MissingPrefabChecker
{
private const string PATH = "Assets/Editor/Examples/Example_07_MissingPrefabChecker/Prefabs";
private const string PrefabNameColor = "#00FF00";
private const string SubPrefabNameColor = "#FF0000";
[MenuItem("Tools/CheckMissingPrefab", priority = 7)]
private static void CheckMissingPrefab()
{
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);
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
EditorUtility.DisplayProgressBar("玩命检查中", "玩命检查中..." + path, (float) index / length);
FindMissingPrefabRecursive(prefab, prefab.name, true);
}
EditorUtility.ClearProgressBar();
}
static void FindMissingPrefabRecursive(GameObject gameObject, string prefabName, bool isRoot)
{
if (gameObject.name.Contains("Missing Prefab"))
{
Debug.LogError(
$"<color={PrefabNameColor}>{prefabName}</color> has missing prefab <color={SubPrefabNameColor}>{gameObject.name}</color>");
return;
}
if (PrefabUtility.IsPrefabAssetMissing(gameObject))
{
Debug.LogError(
$"<color={PrefabNameColor}>{prefabName}</color> has missing prefab <color={SubPrefabNameColor}>{gameObject.name}</color>");
return;
}
if (PrefabUtility.IsDisconnectedFromPrefabAsset(gameObject))
{
Debug.LogError(
$"<color={PrefabNameColor}>{prefabName}</color> has missing prefab <color={SubPrefabNameColor}>{gameObject.name}</color>");
return;
}
if (!isRoot)
{
if (PrefabUtility.IsAnyPrefabInstanceRoot(gameObject))
{
return;
}
GameObject root = PrefabUtility.GetNearestPrefabInstanceRoot(gameObject);
if (root == gameObject)
{
return;
}
}
foreach (Transform childT in gameObject.transform)
{
FindMissingPrefabRecursive(childT.gameObject, prefabName, false);
}
}
}
}