Unity中资源被引用位置查找

本文作者分享了在Unity中查找资源被具体哪个节点引用的方法,现有的解决方案只能找到大概的引用预设,而作者的实现能精确到节点。代码已附上,期待社区的反馈和建议。
摘要由CSDN通过智能技术生成

搜了一下网上的 关于资源引用查找的 只有 查找大概的引用预设,并不能显示具体到哪个节点引用的。所有自己写了一个,分享出来。欢迎大佬指点

话不多说,直接上代码

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

public class FindAssetsImpro
{
   
	private static readonly string[] searchFolders = {
    "Assets" };
    private static readonly string[] searchTarget = {
    "t:Prefab", "t:Material" };
    private static Dictionary<string, string> tempGUIDDic;
    [MenuItem("Assets/查找该资源预设引用位置", false, 10)]
    private static void FindAss()
    {
   
        if (tempGUIDDic == null) tempGUIDDic = new Dictionary<string, string>();
        tempGUIDDic.Clear();
        string curentGUID = "";
        System.Type type;
        Dictionary<string, string> spriteFileIDDic = null;
        List<MatchContent> AlreadyMatchList = new List<MatchContent>();
        Dictionary<string, Object> spriteNmeObjDic = new Dictionary<string, Object>();
        Dictionary<string, List<System.Action>> texturePrintAct = new Dictionary<string, List<System.Action>>();
        string spriteFileName = string.Empty;
        if (EditorSettings.serializationMode != SerializationMode.ForceText) EditorSettings.serializationMode = SerializationMode.ForceText;
        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
        string selectionName = Selection.activeObject.name;
        type = Selection.activeObject.GetType();
        if (type == typeof(UnityEngine.Texture2D) || type == typeof(UnityEngine.Sprite))
        {
   
            spriteFileIDDic = GetTexture2DSpritfileIDDic(path, out spriteNmeObjDic);
            if (type == typeof(UnityEngine.Sprite)) spriteFileName = Selection.activeObject.name;
        }
        if (!string.IsNullOrEmpty(path))
        {
   
            string guid = AssetDatabase.AssetPathToGUID(path);
            List<string> guidsList = new List<string>();
            for (int i = 0; i < searchTarget.Length; i++)
            {
   
                guidsList.AddRange(AssetDatabase.FindAssets(searchTarget[i], searchFolders));
            }
            List<string> fs = new List<string>();
            for (int i = 0; i < guidsList.Count; i++)
            {
   
                fs.Add(AssetDatabase.GUIDToAssetPath(GUIDS[i]));
            }
            string[] files = fs.ToArray();
            int startIndex = 0;
            int matchCount = 0;
            EditorApplication.update = delegate ()
            {
   
                bool isCancel = EditorUtility.DisplayCancelableProgressBar("匹配资源中", files[startIndex], (float)startIndex / (float)files.Length);
                bool isHave = false;
                foreach (var item in AssetDatabase.GetDependencies(files[startIndex]))
                {
   
                    if (!tempGUIDDic.TryGetValue(item, out curentGUID))
                    {
   
                        curentGUID = AssetDatabase.AssetPathToGUID(item);
                        tempGUIDDic[item] = curentGUID;
                    }
                    if (string.Equals(guid, curentGUID))
                    {
   
                        isHave = true;
                        break;
                    }
                }
                if (!isHave)
                {
   
                    startIndex++;
                }
                else
                {
   
                    string objTxt = File.ReadAllText(files[startIndex]);
                    if (Regex.IsMatch(objTxt, guid))
                    {
   
                        string delimiter = string.Empty;
                        for (int i = 0; i < specialCharacters.Length; i++)
                        {
   
                            if (!Regex.IsMatch(objTxt, "\\" + specialCharacters[i]))
                            {
   
                                delimiter = specialCharacters[i];
                                break;
                            }
                        }
                        if (string.IsNullOrEmpty(delimiter))
                        {
   
                            UnityEngine.Debug.LogError("No special symbols to split text.Please add some special symbols to specialCharacters");
                            EditorUtility.ClearProgressBar();
                            EditorApplication.update = null;
                        }
                        string[] objTxtArry = SelectText(objTxt, "---", delimiter);
                        var matchContent = new MatchContent(guid) {
    type = type };
                        if (spriteFileIDDic != null)
                        {
   
                            matchContent.spriteFileIDDic = spriteFileIDDic;
                            if (type == typeof(UnityEngine.Sprite)) matchContent.spriteFileId = spriteFileIDDic.FirstOrDefault(q => q.Value == spriteFileName).Key;
                        }
                        var tempList = CheckOut(objTxtArry, matchContent, files[startIndex], ref matchCount, ref texturePrintAct, spriteNmeObjDic)
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值