在 Unity 中使用资源文件夹

编程入门 行业动态 更新时间:2024-10-26 04:22:59
本文介绍了在 Unity 中使用资源文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个需要引用 .txt 文件的 HoloLens 项目.我将文件存储在 Unity 的资源"文件夹中,并且它们运行良好(通过 Unity 运行时):

I have am developing a HoloLens project that needs to reference .txt files. I have the files stored in Unity's 'Resources' folder and have them working perfectly fine (when run via 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 中选择构建创建)时,我什至没有看到资源或资产文件夹.我想知道是我做错了什么,还是有特殊的方式来处理这些资源.

However, when building the program for HoloLens deployment, I am able to run the code but it doesn't work. None of the resources show up and when examining the HoloLens Visual Studio solution (created by selecting build in Unity), I don't even see a resources or assets folder. I am wondering if I am doing something wrong or if there was a special way to deal with such resources.

还有图像和声音文件...

Also with image and sound files...

foreach (string str in im) { spriteList.Add(Resources.Load<Sprite>(str)); }

字符串str"有效;它与 Unity 一起工作得非常好.但是,再次强调,它在通过 HoloLens 运行时不会加载任何内容.

The string 'str' is valid; it works absolutely fine with Unity. However, again, it's not loading anything when running through the HoloLens.

推荐答案

您无法使用 StreamReader 或 File 类读取资源目录.您必须使用 Resources.Load.

You can't read the Resources directory with the StreamReader or the File class. You must use Resources.Load.

1.该路径相对于您项目的 Assets 文件夹中的任何 Resources 文件夹.

1.The path is relative to any Resources folder inside the Assets folder of your project.

2.不要不要包含文件扩展名,例如.txt、.png、.mp3 在路径参数中.

2.Do not include the file extension names such as .txt, .png, .mp3 in the path parameter.

3.当您在 Resources 文件夹中有另一个文件夹时,请使用正斜杠而不是反斜杠.反斜杠不起作用.

3.Use forward slashes instead of back slashes when you have another folder inside the Resources folder. backslashes won't work.

文本文件:

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;

精灵 - 单个:

Texture Type 设置为 Sprite(2D 和 UI) 和

Sprite Mode 设置为 Single 的图像.

Sprite sprite = Resources.Load("spriteFile", typeof(Sprite)) as Sprite;

精灵 - 多个:

Texture Type 设置为 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 = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject;

3D Mesh(如 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 loadedObj = Resources.Load("yourGameObject") as GameObject; //MeshFilter meshFilter = loadedObj.GetComponent<MeshFilter>(); //Mesh loadedMesh = meshFilter.sharedMesh; GameObject object1 = Instantiate(loadedObj) as GameObject;

访问子文件夹中的文件:

例如,如果您有一个 shoot.mp3 文件,该文件位于 Resources 中名为Sound"的子文件夹中em> 文件夹,你使用正斜杠:

For example, if you have a shoot.mp3 file which is in a sub-folder called "Sound" that is placed in the Resources folder, you use the forward slash:

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());

更多推荐

在 Unity 中使用资源文件夹

本文发布于:2023-07-25 12:08:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1209756.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   资源   Unity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!