Unity 3D - 编辑器扩展之列出Prefab使用的资源 :
C#代码 :
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Linq;
namespace CZGame
{
public class CheckAllUIPrefabResource : EditorWindow
{
private List<string > checkType = new List<string >();
Dictionary<string , List<string >> mapReferenceOtherModulePref = new Dictionary<string , List<string >>();
Vector2 scrollPos = Vector2.zero;
private void StartCheck ()
{
List<string > withoutExtensions = new List<string >() { ".prefab" };
string [] files = Directory.GetFiles(Application.dataPath + "/GameRes/UIPrefab" , "*.*" , SearchOption.AllDirectories)
.Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();
mapReferenceOtherModulePref.Clear();
float count = 0 ;
foreach (string file in files)
{
count++;
EditorUtility.DisplayProgressBar("Processing..." , "检索中...." , count / files.Length);
string strFile = file.Substring(file.IndexOf("Asset" )).Replace('\\' , '/' );
string temp = strFile.Substring(strFile.IndexOf("/" , strFile.IndexOf("UIPrefab" )) + 1 );
temp = "GameRes/UI/" + temp.Substring(0 , temp.IndexOf("/" ));
string [] dependenciesFiles = AssetDatabase.GetDependencies(strFile, false );
List<string > OtherFiles = null ;
string txt = null ;
foreach (string depFile in dependenciesFiles)
{
bool isNeedShow = false ;
foreach (string type in checkType)
{
if (depFile.IndexOf(type) > -1 )
{
isNeedShow = true ;
break ;
}
}
if (isNeedShow == false )
{
continue ;
}
if (OtherFiles == null )
{
if (!mapReferenceOtherModulePref.TryGetValue(strFile, out OtherFiles))
{
OtherFiles = new List<string >();
mapReferenceOtherModulePref.Add(strFile, OtherFiles);
}
}
if (txt == null )
{
string fullPath = Path.GetFullPath(file);
txt = File.ReadAllText(fullPath);
}
string ugui = AssetDatabase.AssetPathToGUID(depFile);
int fileIDIndex = 8 + txt.IndexOf("fileID: " , txt.LastIndexOf("m_GameObject:" , txt.IndexOf(ugui)));
int fileIDendIndex = txt.IndexOf("}" , fileIDIndex);
string fileID = txt.Substring(fileIDIndex, fileIDendIndex - fileIDIndex);
int nameIndex = 8 + txt.IndexOf("m_Name: " , txt.IndexOf("&" + fileID));
int endNameIndex = txt.IndexOf("\n" , nameIndex);
string gameObjectName = txt.Substring(nameIndex, endNameIndex - nameIndex);
string totalStr = gameObjectName + "#" + depFile;
string [] totalStrArr = totalStr.Split('/' );
int maxCount = 0 ;
int index = -1 ;
int targetIndex = 0 ;
foreach (string nowFile in OtherFiles)
{
index++;
string [] splitArr = nowFile.Split('/' );
int sameCount = 0 ;
for (var i = 0 ; i < splitArr.Length; i++)
{
if (i == 0 ) continue ;
for (int j = 0 ; j < totalStrArr.Length; j++)
{
if (j == 0 ) continue ;
if (splitArr[i] == totalStrArr[j])
{
sameCount++;
}
}
}
if (sameCount > maxCount)
{
targetIndex = index;
maxCount = sameCount;
}
}
OtherFiles.Insert(targetIndex, totalStr);
}
}
EditorUtility.ClearProgressBar();
}
void OnGUI()
{
EditorGUI.BeginChangeCheck();
if (GUILayout.Button("[Png 和 JPG]" , GUILayout.Width(100 )))
{
checkType.Clear();
checkType.Add(".jpg" );
checkType.Add(".png" );
StartCheck();
}
if (GUILayout.Button("[字体]" , GUILayout.Width(100 )))
{
checkType.Clear();
checkType.Add(".fontsettings" );
checkType.Add(".ttf" );
StartCheck();
}
if (GUILayout.Button("[C#脚本文件]" , GUILayout.Width(100 )))
{
checkType.Clear();
checkType.Add(".cs" );
StartCheck();
}
EditorGUI.EndChangeCheck();
scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
int count = 0 ;
foreach (var kv in mapReferenceOtherModulePref)
{
count++;
string prefabName = kv.Key;
EditorGUILayout.LabelField("" + count + ": " + prefabName);
foreach (string strIllegalRes in kv.Value)
{
int spliterIndex = strIllegalRes.IndexOf("#" );
string gameObjectName = strIllegalRes.Substring(0 , spliterIndex);
string resPath = strIllegalRes.Substring(spliterIndex + 1 );
var obj = AssetDatabase.GetMainAssetTypeAtPath(resPath);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(gameObjectName, GUILayout.Width(200 ));
EditorGUILayout.TextField(resPath);
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.Space();
EditorGUILayout.Space();
}
EditorGUILayout.EndScrollView();
}
}
}