脚本丢失查询及清理

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#if UNITY_EDITOR
namespace Fury.Editor
{
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    using UnityEditor;
 
    public class MissingReferenceFinder : EditorWindow
    {
 
        private Vector2 scrollPos = Vector2.zero;
        GUIStyle headStyle = new GUIStyle();
 
        private int selectedButtonIndex = -1;
 
        private Color selectedColor = Color.white;
 
        public List<Result> results = new List<Result>();
        [MenuItem("工具箱/一键移除丢失脚本")]
        public static void ShowWindow()
        {
            MissingReferenceFinder window = GetWindow<MissingReferenceFinder>();
            window.titleContent = new GUIContent("MissingReferenceFinder");
            window.Show();
        }
 
 
        private void OnEnable()
        {
            headStyle.fontSize = 15;
            headStyle.fontStyle = FontStyle.Bold;
            headStyle.alignment = TextAnchor.MiddleCenter;
            headStyle.normal.textColor = Color.white;
        }
 
        public void OnGUI()
        {
            EditorGUILayout.BeginVertical(GUILayout.Width(position.width), GUILayout.Height(position.height));
            {
                GUILayout.Space(10); // Add gap before the heading
 
                DrawLabel("一键移除丢失脚本", headStyle);
 
                GUILayout.Space(10); // Add gap after the heading
 
                EditorGUILayout.BeginHorizontal();
                {
                    if (GUILayout.Button("搜索资源文件夹", GetButtonStyle(0)))
                    {
                        selectedButtonIndex = 0;
                        SearchAssetFolder();
                    }
 
                    if (GUILayout.Button("搜索当前场景", GetButtonStyle(1)))
                    {
                        selectedButtonIndex = 1;
                        SearchCurrentScene();
                    }
 
                    if (GUILayout.Button("搜索选中的物体", GetButtonStyle(2)))
                    {
                        selectedButtonIndex = 2;
                        SerachSelectedAsset();
                    }
                }
                EditorGUILayout.EndHorizontal();
 
                EditorGUILayout.BeginVertical(EditorStyles.helpBox, GUILayout.Width(position.width - 5), GUILayout.Height(position.height - 105));
                {
                    scrollPos = EditorGUILayout.BeginScrollView(scrollPos); // Adding scroll view for the Components List
                    {
                        foreach (var item in results)
                        {
                            EditorGUILayout.ObjectField(item.Obj, typeof(GameObject), true);
                        }
                    }
                    EditorGUILayout.EndScrollView();
                }
                EditorGUILayout.EndVertical();
                if (GUI.Button(new Rect(position.width / 2 - 60, position.height - 35, 120, 30), "一键清除空引用"))
                {
                    ClearNowNullCommponents();
                }
            }
            EditorGUILayout.EndVertical();
        }
        private void ClearNowNullCommponents()
        {
            Debug.Log("当前拥有空脚本引用的物体个数:" + results.Count);
            for (int x = 0; x < results.Count; x++)
            {
                GameObjectUtility.RemoveMonoBehavioursWithMissingScript(results[x].Obj);
            }
            Debug.Log("移除成功");
        }
        private GUIStyle GetButtonStyle(int buttonIndex)
        {
            if (buttonIndex == selectedButtonIndex)
            {
                // Create a GUIStyle for the selected button appearance
                GUIStyle selectedButtonStyle = new GUIStyle(EditorStyles.toolbarButton);
 
                // Adjust the visual properties to make the border more visible
                selectedButtonStyle.normal.textColor = Color.white; // Change the text color
                selectedButtonStyle.normal.background = MakeTexture(2, 2, new Color(0.49f, 0.25f, 0.03f, 1f)); // Add a background color
 
                return selectedButtonStyle;
            }
            else
            {
                // Return the default button style
                return EditorStyles.miniButton;
            }
        }
 
        private Texture2D MakeTexture(int width, int height, Color color)
        {
            Color[] pixels = new Color[width * height];
            for (int i = 0; i < pixels.Length; i++)
            {
                pixels[i] = color;
            }
 
            Texture2D texture = new Texture2D(width, height);
            texture.SetPixels(pixels);
            texture.Apply();
 
            return texture;
        }
 
 
        private void DrawLabel(string text, GUIStyle style)
        {
            Rect rect = GUILayoutUtility.GetRect(GUIContent.none, style);
            Color32 color = new Color32(125, 64, 8, 225);
            EditorGUI.DrawRect(rect, color);
            GUI.Label(rect, text, style);
        }
 
        private void SerachSelectedAsset()
        {
            results.Clear();
            if (Selection.activeGameObject)
                Traverse(Selection.activeGameObject.transform);
            Debug.Log("> Total Results: " + results.Count);
        }
 
        public void SearchCurrentScene()
        {
            results.Clear();
            GameObject[] gos = SceneManager.GetActiveScene().GetRootGameObjects();
            foreach (GameObject go in gos) Traverse(go.transform);
            Debug.Log("> Total Results: " + results.Count);
        }
 
        public void SearchAssetFolder()
        {
            results.Clear();
            var allPrefabs = GetAllPrefabAddress();
            int count = 0;
            EditorUtility.DisplayProgressBar("Processing...", "Begin Job", 0);
 
            foreach (string prefab in allPrefabs)
            {
                UnityEngine.Object o = AssetDatabase.LoadMainAssetAtPath(prefab);
 
                if (o == null)
                {
                    Debug.Log("prefab " + prefab + " null?");
                    continue;
                }
 
                GameObject go;
                go = o as GameObject;
                if (go != null)
                {
                    EditorUtility.DisplayProgressBar("Processing...", go.name, ++count / (float)allPrefabs.Length);
                    Traverse(go.transform);
 
 
                }
            }
            Debug.Log("> Total Results: " + results.Count);
            EditorUtility.ClearProgressBar();
        }
 
 
        public string[] GetAllPrefabAddress()
        {
            string[] temp = AssetDatabase.GetAllAssetPaths();
            List<string> result = new List<string>();
            foreach (string s in temp)
            {
                if (s.Contains(".prefab")) result.Add(s);
            }
            return result.ToArray();
        }
 
        private void AppendComponentResult(string childPath, int index, GameObject obj)
        {
            results.Add(new Result()
            {
                address = "Missing Component " + index + " of " + childPath,
                Obj = obj,
            });
        }
        private void AppendTransformResult(string childPath, string name, GameObject obj)
        {
            results.Add(new Result()
            {
                address = "Missing Prefab for \"" + name + "\" of " + childPath,
                Obj = obj,
            });
        }
        private void Traverse(Transform transform, string path = "")
        {
            string thisPath = path + "/" + transform.name;
            Component[] components = transform.GetComponents<Component>();
            for (int i = 0; i < components.Length; i++)
            {
                if (components[i] == null)
                {
                    AppendComponentResult(thisPath, i, transform.gameObject);
                }
            }
 
            for (int c = 0; c < transform.childCount; c++)
            {
                Transform t = transform.GetChild(c);
                PrefabAssetType pt = PrefabUtility.GetPrefabAssetType(t.gameObject);
                if (pt == PrefabAssetType.MissingAsset)
                {
                    AppendTransformResult(path + "/" + transform.name, t.name, transform.gameObject);
                }
                else
                {
                    Traverse(t, thisPath);
                }
            }
        }
    }
 
    [System.Serializable]
    public class Result
    {
        public string address;
        public GameObject Obj;
    }
}
 
#endif

  

posted @   游戏鼻祖  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2017-10-23 unity 同一标签里内容拥有不同颜色
点击右上角即可分享
微信分享提示