Unity---资源管理中不同资源的路径获取方式
1、首先需要先了解两个知识点: Unity内置的文件路径获取方式、windows的Directory.GetFiles文件获取方式:
1>Unity内置的文件路径获取方式,以下是官方解释:https://docs.unity3d.com/ScriptReference/AssetDatabase.FindAssets.html
以下是自己对AssetDatabase类中一些方法的简单测试:
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEditor; 5 6 using UnityEngine; 7 using UnityEditor; 8 9 public class TestAssetDatabase : Editor 10 { 11 const string TestMatPath = @"Assets/Materials/"; 12 const string TestMatName = "TestMaterial.mat"; 13 14 static string MatPath = string.Format("{0}{1}", TestMatPath, TestMatName); 15 16 [MenuItem("Tools/Create Asset")] 17 static void CreateMaterial() 18 { 19 var material = new Material(Shader.Find("Specular")); 20 AssetDatabase.CreateAsset(material, MatPath); //Materials文件夹 需要手动创建 21 } 22 23 [MenuItem("Tools/Delete Asset")] 24 static void DeleteMaterial() 25 { 26 AssetDatabase.DeleteAsset(MatPath); 27 } 28 29 [MenuItem("Tools/Copy Asset")] 30 static void CopyMaterial() 31 { 32 AssetDatabase.CopyAsset(MatPath, "Assets/NewMaterials/TestMaterial.mat"); //NewMaterials文件夹 需要手动创建 33 } 34 35 [MenuItem("Tools/Load Asset")] 36 static void LoadMaterial() 37 { 38 //加载资源两种不同的写法,需要些资源后缀的 39 //Material mat = AssetDatabase.LoadAssetAtPath<Material>(MatPath); 40 Material mat = AssetDatabase.LoadAssetAtPath(MatPath, typeof(Material)) as Material; 41 Debug.Log(mat.color); 42 } 43 44 45 //Filter可以是:Name、Lable、Type(内置的、自定义) 46 47 //Type 关键字 t: 48 static string fliterMat = "t:Material"; //单个 49 static string filterPrefab = "t:Prefab"; 50 static string fliterMatPre = "t:Material t:Prefab"; //多个 51 static string filterCustomDefine = "t:CustomDefine"; //自定义的类型,需要是ScriptableObject创建的资源 52 53 //Label 关键字 l: 54 static string filterLabel = "l:lgs"; 55 56 static string filterMixed = "Cube l:lgs"; //混合过滤---名字中有Cube,Label为 lgs 57 58 [MenuItem("Tools/Find Asset")] 59 static void FindAsset() 60 { 61 string[] guidArray = AssetDatabase.FindAssets(filterMixed); 62 foreach (string item in guidArray) 63 { 64 Debug.Log(AssetDatabase.GUIDToAssetPath(item)); //转换来的资源路径是带有后缀名字的 65 } 66 } 67 }
2>windows的Directory.GetFiles文件获取方式,官方解释:https://docs.microsoft.com/zh-cn/dotnet/api/system.io.directory.getfiles?view=netframework-4.7.2#System_IO_Directory_GetFiles_System_String_System_String_System_IO_SearchOption_
以下是官方代码示例(指定以字母开头的文件数):
1 using System; 2 using System.IO; 3 4 class Test 5 { 6 public static void Main() 7 { 8 try 9 { 10 // Only get files that begin with the letter "c." 11 string[] dirs = Directory.GetFiles(@"c:\", "c*"); 12 Console.WriteLine("The number of files starting with c is {0}.", dirs.Length); 13 foreach (string dir in dirs) 14 { 15 Console.WriteLine(dir); 16 } 17 } 18 catch (Exception e) 19 { 20 Console.WriteLine("The process failed: {0}", e.ToString()); 21 } 22 } 23 }
在获取不同资源的时候,只需要对不同类型的资源加以不同的过滤标签,
例如:
过滤器,若以t:开头,表示用unity的方式过滤;若以f:开头,表示用windows的SearchPattern方式过滤;若以r:开头,表示用正则表达式的方式过滤
再根据过滤标签,获取不同的资源路径即可
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步