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 @ 2022-10-09 17:14  sun_dust_shadow  阅读(804)  评论(0编辑  收藏  举报