Unity编辑器里查找某一类型的资源

MenuItem 和 ContextMenu

https://blog.csdn.net/wpapa/article/details/51066397

反射与特性,自定义特性

https://www.cnblogs.com/whuanle/p/12182962.html

编辑器里查找某一类型的资源

复制代码
public static void GetModels(string AssetsPath)
    {
        try
        {
            ModelConfig modelConfig = AssetDatabase.LoadAssetAtPath<ModelConfig>(Paths.MODELCONFIG_PATH);
            modelConfig.m_AllModelName.Clear();
            DirectoryInfo directoryInfo = new DirectoryInfo(AssetsPath);
            FileInfo[] fileInfos = directoryInfo.GetFiles("*", SearchOption.AllDirectories);
            for (int i = 0; i < fileInfos.Length; i++)
            {
                if (fileInfos[i].Name.EndsWith(".fbx"))
                {
                    modelConfig.m_AllModelName.Add(fileInfos[i].Name.Replace(".fbx", ""));
                }
            }
        }
        catch (System.Exception e)
        {
            Debug.LogError($"获取模型失败!{e}");
        }
    }
上面的是正儿八经的判断后缀去获取模型,下面的是通过.FindAssets这个api去查找,返回的是GUID,有通过GUID来得到路径的api,其中传入的参数t:是有含义的,代表根据类型来查找,model代表模型,其他字母的含义请查阅相关api。
 try
        {
            List<string> modelPath = new List<string>();
            string[] allStr = AssetDatabase.FindAssets("t:model", new string[] { pathList });
            for (int i = 0; i < allStr.Length; i++)
            {
                string path = AssetDatabase.GUIDToAssetPath(allStr[i]);

                if (allStr.Length > 100)
                {
                    EditorUtility.DisplayProgressBar("查找模型", "ModelPath:" + path, i * 1.0f / allStr.Length);
                }
                modelPath.Add(path);
            }
            EditorUtility.ClearProgressBar();
            return modelPath;

        }
        catch (System.Exception e)
        {
            EditorUtility.ClearProgressBar();
            Debug.LogError($"查找模型失败{e}!");
            return null;

        }
复制代码

 

posted @   过往云烟吧  阅读(524)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示