当前位置:网站首页>【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加载的时候加载不出来。
这里查找了所有的预制,其他的资源同理。
边栏推荐
- Bisection (integer bisection and floating point bisection)
- KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架
- Interview skills for interview steps
- 【C语言】结构体嵌套二级指针的使用
- Function and principle of remoteviews
- Day24 JS notes 2021.09.15
- Simulation of the Saier lottery to seek expectation
- Two writing methods of JNI function
- Day37 JS note motion function 2021.10.11
- fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
猜你喜欢

Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting

Day36 JS notes ecma6 syntax 2021.10.09

Data analysis learning notes

Build your own website (18)

不到一小时,苹果摧毁了15家初创公司

【C语言】NextDay问题

Deployment and optimization of vsftpd service

Prefix and (2D)

What method is required for word, PDF and txt files to realize full-text content retrieval?

JNI confusion of Android Application Security
随机推荐
URL append parameter method, considering #$ Situation of
Map sorting tool class
PrecomputedTextCompat用法及原理
案例驱动 :从入门到掌握Shell编程详细指南
Contract quantification system development (construction explanation) - contract quantification system development (source code analysis and ready-made cases)
Mutual conversion between mytipartfile and file
6.A-B
Is it feasible to be a programmer at the age of 26?
recent developments
【C语言】二叉树的实现及三种遍历
Day24 JS notes 2021.09.15
Day32 JS note event (Part 1) September 27, 2021
[Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
UGUI强制刷新Layout(布局)组件
. Net hybrid development solution 24 webview2's superior advantages over cefsharp
Pre parsing, recursive functions and events in day25 JS 2021.09.16
Custom title bar view
Array method in JS 2021.09.18
双缓冲绘图
【北京航空航天大学】考研初试复试资料分享