Unity里常用的一些路径等信息
来源:互联网 发布:java 登录界面 编辑:程序博客网 时间:2024/06/04 22:44
0. Asset folder的路径:Application.dataPath; 得到的是自磁盘根目录开始的路径。
0. C#自带的: string[] paths = Directory.GetFiles(Application.dataPath,"*.*",SearchOption.AllDirectories); //第三个参数可以改为SearchOption 。TopDirectoryOnly则只搜索指定的目录,不搜索下面的子目录
string[] matpaths =Directory.GetFiles(Application.dataPath,"*.mat",SearchOption.AllDirectories); //搜索Unity工程Asset目录(包括子目录)下所有的Material
1. LoadAsset
Resources.Load,只Load位于Resources文件夹及其子目录下面的Asset。 但有Resources.LoadAssetAtPath()可以Load放置于任意位置的Asset。
Resources.LoadAssetAtPath :Returns a resource at an asset path (Editor Only).
This function always return null in the standalone player or web player. This is useful for quickly accessing an asset for use in the editor only.
AssetDatabase.LoadAssetAtPath : Returns the first asset object of type type at given path assetPath.
Materialmat = (Material)Resources.LoadAssetAtPath(propermatpath,typeof(Material));
// In Editor, same with Material mat = (Material)AssetDatabase.LoadAssetAtPath(propermatpath, typeof(Material));
注意!!!! 很容易导致Load fail 的两个细节!!!
(1) Unity里的各种Load,都要求路径是相对的路径。All paths are relative to the project folder, for example: "Assets/MyTextures/hello.png".
(2) 分隔符为单个的‘/’, Windows平台也是一样。 ALL asset names & paths in Unity use forward slashes, even on Windows.
static string GetRelativeAssetPath(string _fullPath)
{
_fullPath = GetRightFormatPath(_fullPath);
int idx = _fullPath.IndexOf("Assets");
string assetRelativePath = _fullPath.Substring(idx);
return assetRelativePath;
}
static string GetRightFormatPath(string _path)
{
return _path.Replace("\\", "/");
}
2.FindObjects
GameObject[] objs = ( GameObject[])FindObjectsOfType(typeof (GameObject)); //get all gameobjects including sub_GameObject.
Renderer[] renderers = (Renderer [])FindObjectsOfType<Renderer>();
Object.FindObjectsOfType : Returns a list of all active loaded objects of Type type. It will return no assets (meshes, textures, prefabs, ...) or inactive objects.
Resources.FindObjectsOfTypeAll :
This function can return any type of Unity object that is loaded, including game objects, prefabs, materials, meshes, textures, etc. It will also list internal stuff, therefore please be extra careful the way you handle the returned objects. Contrary to Object.FindObjectsOfType this function will also list disabled objects.
Asset的获取,以获取Material为例
Object[] selections = Selection.GetFiltered(typeof(Material),SelectionMode.TopLevel);
这里获取到Material类型的Object[], 但这里并不能直接将selections强转为Material[]。 需要将这些Materail Load进内存再处理。
// for me : 一个重要的思想是,不是想着有那些API,然后用API去迁就你的功能实现,而是把注意力放在功能上,让功能按照正常思维去做。按照这个方式去找合适的API。软件一般是按照人们的正常思维来设计的,所以按照这个流程,可以比较顺畅地实现功能需求。如何将Material Load进内存呢,我们知道有Resources.Load(),Resources.LoadAssetAtPath(),意味着我们需要知道AssetPath然后将之Load进来。事实上,我们得到了selections,这是Object[],由Object应该可以得到它的AssetPath吧。嗯,有的,AssetDatabase.GetAssetPath(obj.GetInstanceID()); //UnityEngine.Object里有GetInstanceID(),就是为了方便大家根据InstanceID()获取Object的各种信息吧。
小技巧:
1. 测试某些功能时,可以直接在放在任意位置的.cs文件(是的,不一定要放在Editor文件夹下)里写 [MenuItem("***")] 和相应的static函数(必须是static函数)就行了。例如:
[MenuItem("IvyTrial/ClickMe")]
staticvoidClickme()
{
Debug.Log("Click me" );
}
2. C#里写数组,Object向Object[]的转换
Object[] selections =Selection.GetFiltered(typeof(Material),SelectionMode.TopLevel);
foreach(Objectobjinselections)
{
Object[] roots = newObject[]{obj};
Object[] depends = EditorUtility .CollectDependencies(roots);
}
0 0
- Unity里常用的一些路径等信息
- $_SERVER[]:包含一些诸如头信息,路径和脚本等信息的数组。
- UNITY里用的一些C#宏
- 【File】一些常用的路径
- thinkphp的一些常用路径
- Unity UNet Lobby 的一些测试信息
- 一些常用的java信息
- 经常使用的一些配置信息路径
- 【Unity】Kinect一些常用的API
- UNITY开发中常用的一些API
- 一些OpenCV里常用的图像处理
- [Web.config]里的一些常用设置
- Unity里鼠标点击移动的一些方法
- 将信息写入指定路径的Txt文件里
- linux 一些常用的命令等
- 如何获取android的相关信息,以及安装路径等信息的获取等
- android的一些常用手机信息获取
- android的一些常用手机信息获取
- Android操作系统11种传感器介绍
- httpclient4.4的简单demo
- xlsxwriter使用啊
- 第13周-交通类(3)
- 用C语言制作Web服务器
- Unity里常用的一些路径等信息
- Lua读取文件和写入文件
- @Aspect注解无效
- 安卓盒子launcher界面开发之添加自动定位,获取当地天气
- 关于ucos任务堆栈的理解
- Contains Duplicate
- R语言学习之路(二)矩阵
- IP地址中的保留地址
- 解决域名访问出现中文乱码,而本地测试不会出现的问题