当前位置:网站首页>[unity Editor Extension practice], find all prefabrications through code
[unity Editor Extension practice], find all prefabrications through code
2022-06-28 12:21:00 【Unique_ eight hundred and forty-nine million nine hundred and n】
stay Unity Editor extensions in , Usually, I will add, delete, modify and check files , Here are two Unity Find prefabricated methods in , The same goes for other documents .
1、 utilize AssetDatabase Find prefabrication :
adopt AssetDatabase Of FindAssets Method can find all the files guid, Parameters filter Is a filter character . Namely Unity in Project The two buttons on the right side of the search bar under the window pop up ,t The sign is Type,l The sign is lable.( Such as "Prefab t:Prefab" Is the name with Prefab Prefabrication of , This parameter is the same as here )
Parameters searchInFolders Is the relative path of the folder ( No path is in the project Assets The lookup ).
then use GUIDToAssetPath Find the prefabricated path , Reuse LoadAssetAtPath Load as 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、 utilize Directory Find files :
Directory and AssetDatabase Empathy , It is only used here Directory Of GetFiles Method to find the prefabrication path ,
path: Relative paths ,
searchPattern:? matching 0 Or a character ,* matching 0 Live multiple characters .
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;
}
Expand small knowledge :
Generally, the prefabrication obtained by these two methods is the same , But there is one case where there is a difference . stay Unity Of Assets Next create a folder , When naming, enter two points in front of the file name , Such as :..Prefab. You'll find out Unity Of Project Next , You will not see this folder and the files in it . In this case, if Directory.GetFiles Will get the path of the file , But use AssetDatabase.LoadAssetAtPath It cannot be loaded when loading .
All the prefabrications are found here , The same goes for other resources .
边栏推荐
- Build your own website (18)
- 2022年理财产品的常见模式有哪些?
- 3. seat number
- Difference (one dimension)
- AcWing 609. Salary (implemented in C language)
- 來吧元宇宙,果然這熱度一時半會兒過不去了
- Unity加载设置:Application.backgroundLoadingPriority
- 30套JSP网站源代码合集「建议收藏」
- Many benefits of SEO optimization are directly related to traffic
- It really doesn't matter if a woman fails to pass the college entrance examination and buys thousands of villas in a counter attack
猜你喜欢
Self use demo of basic component integration of fluent
Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method
Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting
Android应用安全之JNI混淆
UGUI使用小技巧(六)Unity实现字符串竖行显示
[C language] about scanf() and scanf_ Some problems of s()
Graphics view framework for QT learning (to realize startup animation)
Share the easy-to-use fastadmin open source system - practical part
KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架
Using MySQL database in the express framework of node
随机推荐
零基础C语言(一)
女子高考落榜读专科逆袭买千万别墅,考得不好真的没关系
运维思考 | 你知道CMDB与监控是什么关系吗?
AsyncTask经验小结
Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting
【JS】斐波那契数列实现(递归与循环)
已知两个点和中间一个比例的点,求该点坐标
RemoteViews的作用及原理
AcWing 609. Salary (implemented in C language)
6.A-B
【Unity编辑器扩展实践】、利用txt模板动态生成UI代码
Come on, yuanuniverse. Sure enough, the heat won't pass for a while
Redis principle - List
Asynctask experience summary
[C language] use of file read / write function
如何获取泛型的类型
fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
1. print hourglass
EMC RS485 interface EMC circuit design scheme
Daily practice of C language - day 4: find the sum of all even numbers within 100