unity editor 文件加载相关: 1.获取指定路径下所有指定类型的文件 2.获取鼠标Select选择的文件

一: 使用AssetDatabase.FindAssets获取指定路径下资源。

如下,获取指定路径下所有挂接

ActionTimelineProfile类型的

资源名:

复制代码
 1         string[] pathArr = { ConstUtils.cfgPrefix_ActionTimeline };
 2         private Dictionary<string, string> names = new();
 3 
 4         public Dictionary<string, string> GetTimelineNames()
 5         {
 6             names.Clear();
 7 #if UNITY_EDITOR
 8             var assetsGuid = AssetDatabase.FindAssets("t:ActionTimelineProfile", pathArr);
 9             for (var i = 0; i < assetsGuid.Length; i++)
10             {
11                 var guid = assetsGuid[i];
12                 var path = AssetDatabase.GUIDToAssetPath(guid);
13                 var lastIndex = path.LastIndexOf('/');
14                 if (lastIndex >= 0)
15                 {
16                     var name = (path.Substring(lastIndex + 1)).Replace(".asset", "");
17                     names.Add(name, name);
18                 }
19             }
20 #endif
21             return names;
22         }
复制代码

 

二:获取鼠标Select选择的文件,C# DirectoryInfo和FileInfo方式

需要注意反斜杠的替换

如下:

modelAssetPath 可以用于读取asset资源。
复制代码
 1             string[] guids = Selection.assetGUIDs;
 2             foreach (var guid in guids)
 3             {
 4                 string assetPath = AssetDatabase.GUIDToAssetPath(guid);
 5                 if (!string.IsNullOrEmpty(assetPath) && Directory.Exists(assetPath))
 6                 {  
 7                     DirectoryInfo dInfo = new DirectoryInfo(assetPath);
 8                     FileInfo[] fileInfos = dInfo.GetFiles("*", SearchOption.AllDirectories);
 9                     foreach (var fileInfo in fileInfos)
10                     {
11                         string assetName = fileInfo.Name;
12                         string directoryName = fileInfo.DirectoryName;
13                         directoryName = directoryName.Replace(@"\", "/");
14                         string assetFullNameRegex = "";
15                         string regexString = "";
16                         string checkName = "";
17                         string fileType = "";
18                         string fullAssetType = "";
19 
20                         var modelAssetPath = fileInfo.FullName.Substring(fileInfo.FullName.IndexOf(@"Assets\"));
21                         modelAssetPath.Replace(@"\", "/");
复制代码

 

posted @   sun_dust_shadow  阅读(987)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 我与微信审核的“相爱相杀”看个人小程序副业
点击右上角即可分享
微信分享提示