内容简介:翻译自:https://stackoverflow.com/questions/41326248/using-resources-folder-in-unity
我正在开发一个需要引用.txt文件的HoloLens项目.我将这些文件存储在Unity的“Resources”文件夹中,并让它们完美地运行(通过Unity运行时):
string basePath = Application.dataPath; string metadataPath = String.Format(@"\Resources\...\metadata.txt", list); // If metadata exists, set title and introduction strings. if (File.Exists(basePath + metadataPath)) { using (StreamReader sr = new StreamReader(new FileStream(basePath + metadataPath, FileMode.Open))) { ... } }
但是,在为HoloLens部署构建程序时,我能够运行代码,但它不起作用.没有资源显示,当检查HoloLens Visual Studio解决方案(通过在Unity中选择构建创建)时,我甚至看不到资源或资源文件夹.我想知道我是做错了什么,或者是否有特殊的方法来处理这些资源.
还有图像和声音文件……
foreach (string str in im) { spriteList.Add(Resources.Load<Sprite>(str)); }
字符串’str’有效;它与Unity完全一致.然而,再次,它在通过HoloLens运行时没有加载任何东西.
Resources.Load
.
1.该路径相对于项目的Assets文件夹中的任何Resources文件夹.
2.不要在path参数中包含.txt,.png,.mp3等文件扩展名.
3.当您在Resources文件夹中有另一个文件夹时,使用正斜杠而不是反斜杠.反斜杠不起作用.
文字文件:
TextAsset txtAsset = (TextAsset)Resources.Load("textfile", typeof(TextAsset)); string tileFile = txtAsset.text;
支持的TextAsset格式:
txt .html .htm .xml .bytes .json .csv .yaml .fnt
声音文件:
AudioClip audio = Resources.Load("soundFile", typeof(AudioClip)) as AudioClip;
图像文件:
Texture2D texture = Resources.Load("textureFile", typeof(Texture2D)) as Texture2D;
精灵 – 单身:
纹理类型的图像设置为Sprite(2D和UI)和
Sprite Mode设置为Single的图像.
Sprite sprite = Resources.Load("spriteFile", typeof(Sprite)) as Sprite;
精灵 – 多个:
纹理类型的图像设置为Sprite(2D和UI)和
Sprite Mode设置为Multiple的图像.
Sprite[] sprite = Resources.LoadAll<Sprite>("spriteFile") as Sprite[];
视频文件(Unity> = 5.6):
VideoClip video = Resources.Load("videoFile", typeof(VideoClip)) as VideoClip;
GameObject Prefab:
GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject;
3D网格(例如FBX文件)
Mesh model = Resources.Load("yourModelFileName", typeof(Mesh)) as Mesh;
3D网格(来自GameObject Prefab)
MeshFilter modelFromGameObject = Resources.Load("yourGameObject", typeof(MeshFilter)) as MeshFilter; Mesh loadedMesh = modelFromGameObject.sharedMesh; //Or design.mesh
3D模型(作为GameObject)
GameObject loadedObj = Resources.Load("yourGameObject") as GameObject; //MeshFilter meshFilter = loadedObj.GetComponent<MeshFilter>(); //Mesh loadedMesh = meshFilter.sharedMesh; GameObject object1 = Instantiate(loadedObj) as GameObject;
访问子文件夹中的文件:
例如,如果您有一个名为“Sound”的子文件夹中的shoot.mp3文件放在Resources文件夹中,则使用正斜杠:
AudioClip audio = Resources.Load("Sound/shoot", typeof(AudioClip)) as AudioClip;
异步加载:
IEnumerator loadFromResourcesFolder() { //Request data to be loaded ResourceRequest loadAsync = Resources.LoadAsync("shipPrefab", typeof(GameObject)); //Wait till we are done loading while (!loadAsync.isDone) { Debug.Log("Load Progress: " + loadAsync.progress); yield return null; } //Get the loaded data GameObject prefab = loadAsync.asset as GameObject; }
使用:StartCoroutine(loadFromResourcesFolder());
翻译自:https://stackoverflow.com/questions/41326248/using-resources-folder-in-unity
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Scala在资源文件夹中获取文件的文件路径
- Node.js折腾记一:读指定文件夹,输出该文件夹的文件树
- C/C++遍历文件夹和文件
- QT选择目录等常用文件/文件夹操作
- 如何使用Python删除一个文件或文件夹
- tar打包如何不打包某一个文件夹(排除某些文件夹)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Purely Functional Data Structures
Chris Okasaki / Cambridge University Press / 1999-6-13 / USD 49.99
Most books on data structures assume an imperative language such as C or C++. However, data structures for these languages do not always translate well to functional languages such as Standard ML, Ha......一起来看看 《Purely Functional Data Structures》 这本书的介绍吧!
MD5 加密
MD5 加密工具
HEX HSV 转换工具
HEX HSV 互换工具