using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
using System.Linq;
using System.Reflection;
using UnityEditor.SceneManagement;
public class FindMissingWindow : EditorWindow
{
static Dictionary<Object, List<Object>> prefabs = new Dictionary<Object, List<Object>>();
static Dictionary<Object, List<string>> refPaths = new Dictionary<Object, List<string>>();
private Vector3 scroll = Vector3.zero;
[MenuItem("Tools/搜索丢失引用的资源")]
static void ShowWindow()
{
GetWindow<FindMissingWindow>("查找Missing资源").Show();
}
public static void FindMissingReferencesInAssets()
{
var gos = new List<GameObject>();
prefabs = new Dictionary<Object, List<Object>>();
refPaths = new Dictionary<Object, List<string>>();
string selectPath = GetCurrentAssetDirectory();
if (string.IsNullOrEmpty(selectPath))
{
EditorUtility.DisplayDialog("错误提示", "请选择一个文件夹。", "确定");
}
else
{
string[] files = Directory.GetFiles(selectPath, "*.prefab", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{
string path = files[i];
if (i < files.Length)
{
EditorUtility.DisplayProgressBar("查找进度" + i + "/" + files.Length, "查找Prefab:" + path, (float)(i / files.Length));
}
gos.Add(AssetDatabase.LoadAssetAtPath<GameObject>(path));
}
EditorUtility.ClearProgressBar();
FindMissingReferences(gos);
}
}
public static string GetCurrentAssetDirectory()
{
foreach (var obj in Selection.GetFiltered<Object>(SelectionMode.Assets))
{
var path = AssetDatabase.GetAssetPath(obj);
if (string.IsNullOrEmpty(path))
continue;
if (System.IO.Directory.Exists(path))
{
return path;
}
else if (System.IO.File.Exists(path))
{
return System.IO.Path.GetDirectoryName(path);
}
}
return "";
}
private static void FindMissingReferences(List<GameObject> gameObjects)
{
if (gameObjects == null)
{
return;
}
int progress = 0;
foreach (var item in gameObjects)
{
GameObject go = item as GameObject;
if (go == null)
{
continue;
}
if (progress < gameObjects.Count)
{
EditorUtility.DisplayProgressBar("进度" + progress + "/" + gameObjects.Count, "检查资源:" + go.name, (float)(progress / gameObjects.Count));
}
var components = go.GetComponentsInChildren<Component>(true);
foreach (var component in components)
{
if (component == null)
{
if (!prefabs.ContainsKey(go)) prefabs.Add(go, new List<Object>());
prefabs[go].Add(component);
continue;
}
SerializedObject so = new SerializedObject(component);
var sp = so.GetIterator();
var objRefValueMethod = typeof(SerializedProperty).GetProperty("objectReferenceStringValue",
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
// Iterate over the components' properties.
while (sp.NextVisible(true))
{
if (sp.propertyType == SerializedPropertyType.ObjectReference)
{
string objectReferenceStringValue = string.Empty;
if (objRefValueMethod != null)
{
objectReferenceStringValue = (string)objRefValueMethod.GetGetMethod(true).Invoke(sp, new object[] { });
}
if (sp.objectReferenceValue == null
&& (sp.objectReferenceInstanceIDValue != 0
|| objectReferenceStringValue.StartsWith("Missing")))
{
if (!refPaths.ContainsKey(component)) refPaths.Add(component, new List<string>());
refPaths[component].Add(sp.propertyPath);
if (!prefabs.ContainsKey(go)) prefabs.Add(go, new List<Object>());
prefabs[go].Add(component);
}
}
}
}
progress++;
}
EditorUtility.ClearProgressBar();
}
void OnInspectorUpdate() //更新
{
Repaint(); //重新绘制
}
private void OnGUI()
{
if (GUILayout.Button("选择指定目录,寻找missing的资源", GUILayout.Width(250), GUILayout.Height(40)))
{
FindMissingReferencesInAssets();
}
scroll = EditorGUILayout.BeginScrollView(scroll);
EditorGUILayout.BeginVertical();
foreach (var item in prefabs)
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.ObjectField(item.Key, typeof(GameObject), true, GUILayout.Width(200));
EditorGUILayout.BeginVertical();
foreach (var cp in item.Value)
{
EditorGUILayout.BeginHorizontal();
if (cp)
{
EditorGUILayout.ObjectField(cp, cp.GetType(), true, GUILayout.Width(200));
if (refPaths.ContainsKey(cp))
{
string missingPath = null;
foreach (var path in refPaths[cp])
{
missingPath += path + "|";
}
if (missingPath != null)
missingPath = missingPath.Substring(0, missingPath.Length - 1);
GUILayout.Label(missingPath);
}
}
else
{
GUILayout.Label("丢失脚本!");
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical();
EditorGUILayout.EndScrollView();
}
}
Unity搜索丢失引用的资源
最新推荐文章于 2026-02-21 00:15:12 发布

621

被折叠的 条评论
为什么被折叠?



