当前位置:网站首页>【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加载的时候加载不出来。
这里查找了所有的预制,其他的资源同理。
边栏推荐
- Daily practice of C language - day 4: find the sum of all even numbers within 100
- JS foundation 10
- Leetcode 48. 旋转图像(可以,已解决)
- 不到一小时,苹果摧毁了15家初创公司
- Day36 JS notes ecma6 syntax 2021.10.09
- 多维度监控:智能监控的数据基础
- Leetcode 705. 设计哈希集合
- Daily practice of C language - day 3: calculate the number of occurrences of sub strings of strings
- RemoteViews布局和类型限制源码分析
- 5. Sum of N numbers
猜你喜欢

Function and principle of remoteviews

UGUI强制刷新Layout(布局)组件

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

Day30 JS notes BOM and DOM 2021.09.24

Day37 JS note motion function 2021.10.11

Web3 security serials (3) | in depth disclosure of NFT fishing process and prevention techniques

Redis principle - List
![[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

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

Software test interview classic + 1000 high-frequency real questions, and the hit rate of big companies is 80%
随机推荐
來吧元宇宙,果然這熱度一時半會兒過不去了
【C语言】如何很好的实现复数类型
AcWing 607. Average 2 (implemented in C language)
JNI confusion of Android Application Security
ProCAST finite element casting process simulation software
深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图...
【Unity编辑器扩展实践】、查找所有引用该图片的预制体
Prefix and (2D)
Cohere, a large model company, completed the round B financing of US $125million
1. print hourglass
6. calculation index
如何获取泛型的类型
AGCO AI frontier promotion (2.16)
Graphics view framework for QT learning (to realize startup animation)
How to deploy the software testing environment?
Contract quantification system development (construction explanation) - contract quantification system development (source code analysis and ready-made cases)
Database Series: is there any way to seamlessly upgrade the business tables of the database
Why do many people want to change careers as programmers, while some programmers want to change careers as others?
Day30 JS notes BOM and DOM 2021.09.24
Redis principle - List