当前位置:网站首页>【Unity编辑器扩展实践】、通过代码查找所有预制
【Unity编辑器扩展实践】、通过代码查找所有预制
2022-06-28 12:00:00 【Unique_849997563】
在Unity中对编辑器的扩展,通常都会操作文件的增删改查,这里介绍两种Unity中查找预制的方法,其他文件同理。
1、利用AssetDatabase查找预制:
通过AssetDatabase的FindAssets方法可以查找到所有文件的guid,参数filter是过滤字符。就是Unity中Project窗口下搜索栏右边两个按钮弹出来的东西,t标识是Type,l标识是lable。(如"Prefab t:Prefab"就是名字中带有Prefab的预制,这个参数就跟这里的一样)

参数searchInFolders是文件夹的相对路径(不加路径就是在项目Assets下查找)。
然后 用GUIDToAssetPath找到预制的路径,再用LoadAssetAtPath加载成GameObject。
public static List<GameObject> GetAllPrefabByAssetDatabase(params string[] path)
{
List<GameObject> _prefabList = new List<GameObject>();
string[] _guids = AssetDatabase.FindAssets("t:Prefab", path);
string _prefabPath = "";
GameObject _prefab;
foreach (var _guid in _guids)
{
_prefabPath = AssetDatabase.GUIDToAssetPath(_guid);
_prefab = AssetDatabase.LoadAssetAtPath(_prefabPath, typeof(GameObject)) as GameObject;
_prefabList.Add(_prefab);
}
return _prefabList;
}2、利用Directory查找文件:
Directory和AssetDatabase同理,只是这里用Directory的GetFiles方法查找预制路径,
path:相对路径,
searchPattern:?匹配0个或一个字符,*匹配0个活多个字符。
public static List<GameObject> GetAllPrefabByDirectory(string path)
{
string[] files = Directory.GetFiles(path, "*.prefab", SearchOption.AllDirectories);
List<GameObject> _prefabList = new List<GameObject>();
GameObject _prefab;
foreach (var _path in files)
{
_prefab = AssetDatabase.LoadAssetAtPath(_path, typeof(GameObject)) as GameObject;
_prefabList.Add(_prefab);
}
return _prefabList;
}扩张小知识:
一般情况下这两种方式获得的预制都是一样的,但是有一种情况下会出现差异。在Unity的Assets下创建一个文件夹,命名的时候在文件名前输入两个点,如:..Prefab。你会发现在Unity的Project下,你会看不到这个文件夹和里面的文件。这种情况下如果使用Directory.GetFiles时会获取到文件的路径,但是使用 AssetDatabase.LoadAssetAtPath加载的时候加载不出来。
这里查找了所有的预制,其他的资源同理。
边栏推荐
- JNI confusion of Android Application Security
- MapReduce project case 1
- 不到一小时,苹果摧毁了15家初创公司
- 6. calculation index
- 【Unity编辑器扩展实践】、利用txt模板动态生成UI代码
- Daily practice of C language - day 3: calculate the number of occurrences of sub strings of strings
- 【北京航空航天大学】考研初试复试资料分享
- Unity加载设置:Application.backgroundLoadingPriority
- Remote login sshd service
- FTP protocol for Wireshark packet capture analysis
猜你喜欢

【vi/vim】基本使用及命令汇总

【C语言】如何产生正态分布或高斯分布随机数

【Unity编辑器扩展实践】、利用txt模板动态生成UI代码

Using soapUI to obtain freemaker's FTL file template

【C语言】判断三角形
![[Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination](/img/06/df5a64441814c9ecfa2f039318496e.jpg)
[Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination

【C语言】NextDay问题

什么是数据合规?怎样做到数据合规?

. Net hybrid development solution 24 webview2's superior advantages over cefsharp

【Unity编辑器扩展基础】、EditorGUILayout (三)
随机推荐
4. maximum continuity factor
Array method in JS 2021.09.18
【C语言】NextDay问题
Interview skills for interview steps
AcWing 610. Salary and bonus (implemented in C language)
【C语言】随机数文件对其进行三种排序方法
JS foundation 10
fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
SEO优化的许多好处是与流量有直接关系
Redis 原理 - List
Map排序工具类
How to deploy the software testing environment?
什么是数据合规?怎样做到数据合规?
Using soapUI to obtain freemaker's FTL file template
Chapter 2 do you remember the point, line and surface (2)
Come on, yuanuniverse. Sure enough, the heat won't pass for a while
Multi dimensional monitoring: the data base of intelligent monitoring
Connectionreseterror: [winerror 10054] the remote host forced an existing connection to be closed
Which programming language will attract excellent talents?
Redis hash hash type string (5)