资源使用查询的几个方法

雨松的方法,代码量很少,放在工程里就行
原文

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
  
public class FindReferences
{
   
    [MenuItem("Assets/Find References", false, 10)]
    static private void Find()
    {
        EditorSettings.serializationMode = SerializationMode.ForceText;
        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
        if (!string.IsNullOrEmpty(path))
        {
            string guid = AssetDatabase.AssetPathToGUID(path);
            string withoutExtensions = "*.prefab*.unity*.mat*.asset";
            string[] files = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories)
                .Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();
            int startIndex = 0;
  
            EditorApplication.update = delegate()
            {
                string file = files[startIndex];
             
                 bool isCancel = EditorUtility.DisplayCancelableProgressBar("匹配资源中", file, (float)startIndex / (float)files.Length);
  
                if (Regex.IsMatch(File.ReadAllText(file), guid))
                {
                    Debug.Log(file, AssetDatabase.LoadAssetAtPath<Object>(GetRelativeAssetsPath(file)));
                }
  
                startIndex++;
                if (isCancel || startIndex >= files.Length)
                {
                    EditorUtility.ClearProgressBar();
                    EditorApplication.update = null;
                    startIndex = 0;
                    Debug.Log("匹配结束");
                }
  
            };
        }
    }
  
    [MenuItem("Assets/Find References", true)]
    static private bool VFind()
    {
        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
        return (!string.IsNullOrEmpty(path));
    }
  
    static private string GetRelativeAssetsPath(string path)
    {
        return "Assets" + Path.GetFullPath(path).Replace(Path.GetFullPath(Application.dataPath), "").Replace('\\', '/');
    }
}

后来看到baidudaren的另一个脚本,支持5.0以上
原文

#define UseAutoFind
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
using System.Reflection;
public class ArtFindSet{
public bool _onlyShowNoUsed;
}
public class ArtAssetsEx : EditorWindow{
#region Init
static ArtAssetsEx Instance;
static List<string> _filePrefabPath = new List<string>();
static List<string> _fileAssetsPath = new List<string>();
static List<PrefabDependencies> _prefabs = new List<PrefabDependencies>();
 
static List<string> _readyToFind;
static bool _path;
static public AssetUsedData _AssetUsedData;
static public AllAssetsData _AllAssetsData;
static public string[] _AssetType;
static public int _SelectAssetType = -1;
static public ArtFindSet _artFindSet = new ArtFindSet();
static bool _repaint;
[MenuItem("Window/资源浏览器")]
public static void Init(){
Instance = EditorWindow.GetWindow<ArtAssetsEx> ();
//#if UseAutoFind
_filePrefabPath.Add (Application.dataPath);
//        Find ();
//#endif
}
//        [MenuItem("Assets/设为Find Art Prefab Path")]
//        public static void AddFilePrefabPath(){
//        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
//        path = path.Replace ("Assets", "");
//        _filePrefabPath.Add(Application.dataPath + path);
//        }
//        [MenuItem("Assets/设为Find Art Assets Path")]
//        public static void AddFileAssetsPath(){
//        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
//        path = path.Replace ("Assets", "");
//        _fileAssetsPath.Add(Application.dataPath + path);
//        }
[MenuItem("Assets/查找这个东西使用的情况")]
public static void FindUsePrefab(){
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if (Instance == null) {
Init();
Find();
}
_AssetUsedData = new AssetUsedData (path);
_AssetUsedData.FindUsed (_prefabs);
_repaint = true;
}
//        [MenuItem("Assets/设为Find Use Prefab")]
//        public static void FindUsePrefab(){
//        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
////        AssetDatabase.SetLabels(
//        }
static public string[] _toolStr = {"浏览器","设置"};
static public int _selectTool;
void OnGUI(){
_selectTool = GUILayout.Toolbar (_selectTool, _toolStr);
switch (_selectTool) {
case 1:
GUILayout.Label ("预制路径");
for (int i = 0; i < _filePrefabPath.Count; i++) {
EditorGUILayout.BeginHorizontal ();
if (GUILayout.Button ("-", GUILayout.Width (30))) {
_filePrefabPath.RemoveAt (i);
i++;
} else {
GUILayout.Label (_filePrefabPath [i]);
}
EditorGUILayout.EndHorizontal ();
}
GUILayout.Label ("资源路径");
for (int i = 0; i < _fileAssetsPath.Count; i++) {
EditorGUILayout.BeginHorizontal ();
if (GUILayout.Button ("-", GUILayout.Width (30))) {
_fileAssetsPath.RemoveAt (i);
i++;
} else {
GUILayout.Label (_fileAssetsPath [i]);
}
EditorGUILayout.EndHorizontal ();
}
break;
case 0:
//        if (GUILayout.Button ("Find",GUILayout.Height(100),GUILayout.MaxWidth(300)))
//        Find ();
if(_prefabs != null && _AllAssetsData!=null){
EditorGUILayout.BeginVertical();
EditorGUILayout.LabelField("项目的预制与场景数目:"+_prefabs.Count);
EditorGUILayout.LabelField("项目使用的文件类型数:"+_AllAssetsData._data.Count);
EditorGUILayout.EndVertical();
 
}
ShowTest ();        
break;
}
if (_repaint) {
Repaint ();
}
}
Vector2 pos;
void ShowTest(){
bool change = false;
EditorGUILayout.BeginHorizontal ();
if (_AssetType != null) {
_SelectAssetType = EditorGUILayout.Popup("查询文件类型:",_SelectAssetType,_AssetType,GUILayout.MaxWidth(300));
if(GUI.changed){
change = true;
EditorGUILayout.EndHorizontal ();
_artFindSet._onlyShowNoUsed = false;
_AllAssetsData.GetDepend(_SelectAssetType,_prefabs);
}
}
if(!change)
EditorGUILayout.EndHorizontal ();
if (_AssetUsedData != null) {
if(GUILayout.Button("查看全局数据"))
_AssetUsedData = null;
if(_AssetUsedData != null)
_AssetUsedData.OnGUI ();
}
if(_AssetUsedData == null && _SelectAssetType != -1){
_artFindSet._onlyShowNoUsed = EditorGUILayout.Toggle(!_artFindSet._onlyShowNoUsed?"显示使用的":"显示未使用的" ,_artFindSet._onlyShowNoUsed);
EditorGUILayout.LabelField(_AssetType[_SelectAssetType]+"总资源数:"+_AllAssetsData._data[_SelectAssetType]._allAssets);
EditorGUILayout.LabelField(_AssetType[_SelectAssetType]+"被使用数:"+_AllAssetsData._data[_SelectAssetType]._allUsedAssets);
EditorGUILayout.LabelField(_AssetType[_SelectAssetType]+"未使用数:"+_AllAssetsData._data[_SelectAssetType]._allUnUsedAssets);
pos = EditorGUILayout.BeginScrollView (pos);
_AllAssetsData.OnGUI(_SelectAssetType,_artFindSet);
EditorGUILayout.EndScrollView ();
}
}
#endregion
#region Find
static void Find(){
_SelectAssetType = -1;
if (_filePrefabPath == null || _filePrefabPath.Count == 0)
_filePrefabPath.Add (Application.dataPath);
_readyToFind = new List<string> ();
_prefabs = new List<PrefabDependencies> ();
_AllAssetsData = new AllAssetsData ();
for (int i = 0; i < _filePrefabPath.Count; i++) {
FindAllPath(_filePrefabPath[i]);
}
 
CreatePrefabData ();
RestPrefabDependencie ();
 
_AssetType = new string[_AllAssetsData._data.Count];
int count = 0;
for (int i = 0;i < _AllAssetsData._data.Count;i++) {
_AssetType[count] = _AllAssetsData._data[i]._expandname;
count++;
}
 
}
static void CreatePrefabData(){
for(int i = 0;i < _readyToFind.Count;i++){
string expandname = Path.GetExtension(_readyToFind[i]);
if(expandname == ".prefab"||expandname == ".unity"){
PrefabDependencies pd = new PrefabDependencies(_readyToFind[i]);
_prefabs.Add(pd);
}
_AllAssetsData.AddAssets(_readyToFind[i]);
}
}
static void RestPrefabDependencie(){
for (int i = 0; i < _prefabs.Count; i++) {
_prefabs[i].GetDependencies();
if(EditorUtility.DisplayCancelableProgressBar("获取索引","GetDependencie:"+i,(float)i/_prefabs.Count)){
EditorUtility.ClearProgressBar ();
return;
}
}
EditorUtility.ClearProgressBar ();
}
static void FindAllPath(string path){
string[] Directorys = new string[0]; 
try{
Directorys = Directory.GetFiles(path);
}catch{}
if(Directorys != null){
for(var i = 0;i < Directorys.Length;i++){
if(!_readyToFind.Contains(Directorys[i]))
_readyToFind.Add(Directorys[i]);
}
}
Directorys = Directory.GetDirectories (path);
for (int i = 0; i < Directorys.Length; i++) {
string newpath;
newpath = Path.GetDirectoryName(Directorys[i])+"/"+Path.GetFileName(Directorys[i]);
FindAllPath(newpath);
}
}
#endregion
}
public class AllAssetsData{
 
public List<AssetsUsedData> _data = new List<AssetsUsedData>();
public void GetDepend(int _id,List<PrefabDependencies> prefabs){
if(_id>=0&&_id<_data.Count)
_data [_id].GetDepend (prefabs);
}
public void AddAssets(string path){
string expandname = Path.GetExtension(path);
if (expandname == ".meta")
return;
int id = IsContainsKey (expandname);
if (id == -1) {
_data.Add(new AssetsUsedData(expandname));
id = _data.Count - 1;
}
_data[id].AddData(path);
}
public int IsContainsKey(string expandname){
for(int i = 0;i < _data.Count;i++){
if(_data[i]._expandname == expandname)
return i;
}
return -1;
}
public void OnGUI(int _SelectAssetType,ArtFindSet set){
if (_SelectAssetType >= 0 && _SelectAssetType < _data.Count) {
int x = 0;
for(int i = 0;i < _data[_SelectAssetType]._data.Count;i++){
if((_data[_SelectAssetType]._data[i]._usedPrefab.Count == 0 && set._onlyShowNoUsed)||
(_data[_SelectAssetType]._data[i]._usedPrefab.Count > 0 && !set._onlyShowNoUsed)
){
Color c = (x%2==0?new Color(0.8f,0.8f,0.8f):Color.white);
GUI.color = c;
EditorGUILayout.BeginVertical("box");
GUI.color = Color.white;
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Used:"+_data[_SelectAssetType]._data[i]._usedPrefab.Count+" ",GUILayout.Width(100));
//        EditorGUILayout.LabelField(_data[_SelectAssetType]._data[i]._path);
//        EditorGUILayout.ObjectField(_data[_SelectAssetType]._data[i].assetObj,typeof(GameObject));
EditorGUILayout.EndHorizontal();
_data[_SelectAssetType]._data[i].OnGUI();
EditorGUILayout.EndVertical();
x++;
}
}
}
//        foreach (KeyValuePair<string,AssetsUsedData> value in _data) {
//        List<AssetUsedData> datas = value.Value._data;
//        for(int i= 0;i < datas.Count;i++){
//        EditorGUILayout.LabelField(datas[i]._path);
//        datas[i].OnGUI();
//        }
//        }
}
}
public class AssetsUsedData{
public int _allAssets;
public int _allUsedAssets;
public int _allUnUsedAssets;
public string _expandname;
public AssetsUsedData(string expandname){
_expandname = expandname;
}
public List<AssetUsedData> _data = new List<AssetUsedData>();
public void AddData(string path){
path = "Assets"+path.Replace(Application.dataPath,"");
path = path.Replace ("\\", "/");
_data.Add (new AssetUsedData (path));
}
public void GetDepend(List<PrefabDependencies> prefabs){
for(int i = 0;i<_data.Count;i++){
if(EditorUtility.DisplayCancelableProgressBar("搜索使用情况","数量:"+i,(float)i/_data.Count)){
_data.Clear();
EditorUtility.ClearProgressBar ();
return;
}
_data[i].FindUsed(prefabs);
 
}
EditorUtility.ClearProgressBar ();
for (int i = 0; i < _data.Count; i++) {
if(EditorUtility.DisplayCancelableProgressBar("加载模型","数量:"+i,(float)i/_data.Count)){
_data.Clear();
EditorUtility.ClearProgressBar ();
return;
}
_data[i].LoadAssetObj();
}
EditorUtility.ClearProgressBar ();
_allAssets = _data.Count;
_allUsedAssets = 0;
for (int i = 0; i < _data.Count; i++) {
if(EditorUtility.DisplayCancelableProgressBar("统计数据","数量:"+i,(float)i/_data.Count)){
_data.Clear();
EditorUtility.ClearProgressBar ();
return;
}
if(_data[i]._usedPrefab.Count > 0){
_allUsedAssets ++;
}
}
_allUnUsedAssets = _allAssets - _allUsedAssets;
EditorUtility.ClearProgressBar ();
}
}
public class AssetUsedData{
public AssetUsedData(string path){
_path = path;
}
public string _path;
public Object _assetObj;
public void LoadAssetObj(){
if (_assetObj == null)
_assetObj = AssetDatabase.LoadAssetAtPath<Object> (_path);
}
public Object assetObj{
get{
if (_assetObj == null)
_assetObj = AssetDatabase.LoadAssetAtPath<Object> (_path);
return _assetObj;
}
}
public List<Object> _usedPrefab = new List<Object>();
public void FindUsed(List<PrefabDependencies> prefabs){
_usedPrefab = new List<Object> ();
for (int i = 0; i < prefabs.Count; i++) {
if(prefabs[i]._dependencies.Contains(_path))
{
_usedPrefab.Add (AssetDatabase.LoadAssetAtPath<Object>(prefabs[i]._prefabPath));
}
}
}
public void OnGUI(){
EditorGUILayout.BeginVertical ();
EditorGUILayout.ObjectField("查询资源:",assetObj,typeof(GameObject));
for (int i = 0; i < _usedPrefab.Count; i++) {
EditorGUILayout.BeginHorizontal();
EditorGUILayout.ObjectField("使用的预制:",_usedPrefab[i],typeof(GameObject));
//        if(GUILayout.Button("See"))
//        SeeAssetData.SeeAsset(_usedPrefab[i],_path);
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical ();
}
}
public class PrefabDependencies{
public PrefabDependencies(string path){
_prefabPath = "Assets"+path.Replace(Application.dataPath,"");
_prefabPath = _prefabPath.Replace ("\\", "/");
}
//.prefab or unity
public string _prefabPath;
public List<string> _dependencies;
public bool _showDependencies;
public void GetDependencies(){
string[] path = new string[1];
path [0] = _prefabPath;
string[] paths =AssetDatabase.GetDependencies (path); 
_dependencies = new List<string> ();
for(int i = 0;i < paths.Length;i++){
_dependencies.Add(paths[i]);
}
}
}
public class SeeAssetData{
static Transform _parent;
public static Transform Parent{
get{
if (_parent == null)
_parent = new GameObject ("Find Assets See").transform;
return _parent;
}
}
public static void SeeAsset(Object prefab,string asset){
string path = AssetDatabase.GetAssetPath (prefab);
string _expandname = Path.GetExtension (path);
Debug.Log (_expandname);
switch (_expandname) {
case ".prefab":
SeeAssetsInPrefab(prefab,asset);
break;
case ".unity":
SeeAssetsInScene(prefab,asset);
break;
}
}
 
static void SeeAssetsInPrefab(Object prefab,string asset){
GameObject obj = GameObject.Instantiate (prefab) as GameObject;
obj.transform.parent = Parent;
HierarchyProperty h = new HierarchyProperty(HierarchyType.Assets);
h.SetSearchFilter ("x",0);
 
}
static void SeeAssetsInScene(Object prefab,string asset){
 
}
[MenuItem("Window/test")]
static void Test(){
//        Assembly m = typeof(EditorWindow).Assembly;
//        Type t = m.GetType ("UnityEditor.DockArea");
//        UnityEngine.Object d = System.Activator.CreateInstance (t);
//        HierarchyProperty h;
//        h.SetSearchFilter ("x",0);
}
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,378评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,356评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,702评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,259评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,263评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,036评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,349评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,979评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,469评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,938评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,059评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,703评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,257评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,262评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,485评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,501评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,792评论 2 345

推荐阅读更多精彩内容