Unity3D游戏开发源码(第二版)11-29查找哪些资源文件引用了当前资源

Unity3D游戏开发源码(第二版)11-29查找哪些资源文件引用了当前资源

using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;

static public class FindReferences
{
    [MenuItem("Assets/Find References", false, 10)]
    static private void Find()
    {
        Dictionary<string, string> guidDics = new Dictionary<string, string>();
        foreach (UnityEngine.Object o in Selection.objects)
        {
            string path = AssetDatabase.GetAssetPath(o);
            if (!string.IsNullOrEmpty(path))
            {
                string guid = AssetDatabase.AssetPathToGUID(path);
                if (!guidDics.ContainsKey(guid))
                {
                    guidDics[guid] = o.name;
                }
            }
        }
        if (guidDics.Count > 0)
        {
            List<string> withoutExtensions = new List<string>() { ".prefab", ".unity", ".mat", ".asset" };
            string[] files = Directory.GetFiles(Application.dataPath, "*.*",
            SearchOption.AllDirectories);
            files = files.Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();
            for (int i = 0; i < files.Length; i++)
            {
                string file = files[i];
                if (i % 20 == 0)
                {
                    bool isCancel = EditorUtility.DisplayCancelableProgressBar("匹配资源中", file, (float)i / (float)files.Length);
                    if (isCancel)
                    {
                        break;
                    }
                }
                foreach (KeyValuePair<string, string> guildItem in guidDics)
                {
                    if (Regex.IsMatch(File.ReadAllText(file), guildItem.Key))
                    {
                        Debug.Log(string.Format("name: {0} file: {1}", guildItem.Value, file),
                        AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(GetRelativeAssetsPath(file)));
                    }
                }
            }
            EditorUtility.ClearProgressBar();
            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)
    {
        string appfull = Path.GetFullPath(Application.dataPath);
        string pathfull = Path.GetFullPath(path);
        string pathfulRepAppfull = pathfull.Replace(appfull, "");
        return "Assets" + pathfulRepAppfull.Replace('\\', '/');
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值