少年修仙传客户端基础资源
dabaoji
2025-06-09 8ee0256378cbf5dbc9d76ed10b60b65a844ef4dd
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
using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using System.IO;
using System.Linq;
 
public class RemoveMissingScripts : EditorWindow
{
    Vector2 scrollPos = Vector2.zero;
    List<string> pathList = new List<string>();
    List<GameObject> missObj = new List<GameObject>();
 
    private void CleanupMissingScripts()
    {
        foreach (GameObject obj in missObj)
        {
            // var components = obj.GetComponents<Component>();
            // var serializedObject = new SerializedObject(obj);
            var count = GameObjectUtility.RemoveMonoBehavioursWithMissingScript(obj);
            // if (count > 0) 
            Debug.LogFormat("清空空脚本:{0}", count);
            // serializedObject.ApplyModifiedProperties();
            EditorUtility.SetDirty(obj);
        }
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }
 
    private void CheckMissingScripts()
    {
        List<string> withoutExtensions = new List<string>() { ".prefab" };
        string[] files = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories)
            .Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();
 
        int count = 0;
        foreach (string fileName in files)
        {
            count++;
            EditorUtility.DisplayProgressBar("Processing...", "搜寻所有Prefab....", count / files.Length);
            string fixedFileName = fileName.Replace("\\", "/");
            int index = fixedFileName.IndexOf("Assets");
            fixedFileName = fixedFileName.Substring(index);
            pathList.Add(fixedFileName);
        }
        EditorUtility.ClearProgressBar();
 
        int count2 = 0;
        foreach (string path in pathList)
        {
            count2++;
            EditorUtility.DisplayProgressBar("Processing...", "遍历丢失脚本Prefab中....", count / pathList.Count);
            GameObject obj = (GameObject)AssetDatabase.LoadAssetAtPath(path, typeof(GameObject));
            checkMissing(obj);
            getAllChild(obj);
        }
        EditorUtility.ClearProgressBar();
    }
 
    void getAllChild(GameObject go)
    {
        foreach (Transform child in go.transform)
        {
            checkMissing(child.gameObject);
            getAllChild(child.gameObject);
        }
    }
 
    void checkMissing(GameObject go)
    {
        var components = go.GetComponents<Component>();
        for (int j = 0; j < components.Length; j++)
        {
            if (components[j] == null)
            {
                missObj.Add(go.gameObject);
                continue;
            }
        }
    }
 
    void OnGUI()
    {
        scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
 
        if (GUILayout.Button("[查找所有Prefab上的Miss脚本]", GUILayout.Width(300)))
        {
            pathList = new List<string>();
            missObj = new List<GameObject>();
            CheckMissingScripts();
        }
 
        EditorGUILayout.Space();
 
        if (GUILayout.Button("[删除所有Prefab上的Miss脚本]", GUILayout.Width(300)))
        {
            CleanupMissingScripts();
        }
 
        EditorGUILayout.Space();
        EditorGUILayout.Space();
 
        foreach (GameObject obj in missObj)
        {
            EditorGUILayout.ObjectField(obj, typeof(GameObject), false);
        }
 
        EditorGUILayout.EndScrollView();
    }
 
    [MenuItem("程序/移除丢失的脚本")]
    public static void RemoveMissingScript()
    {
        EditorWindow.GetWindow<RemoveMissingScripts>("移除丢失的脚本").Show();
    }
 
}