在 Unity 中使用资源文件夹
Q:
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):
我正在开发一个 HoloLens 项目,需要引用 .txt 文件。我将文件存储在 Unity 的“资源”文件夹中,并且它们工作得很好(通过 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)))
{
...
}
}
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.
但是,在构建 HoloLens 部署程序时,我可以运行代码,但它不起作用。没有显示任何资源,并且在检查 HoloLens Visual Studio 解决方案(通过选择 Unity 中的构建创建)时,我什至没有看到资源或资产文件夹。我想知道我是否做错了什么,或者是否有特殊的方法来处理这些资源。
Also with image and sound files…
还有图像和声音文件…
foreach (string str in im)
{
spriteList.Add(Resources.Load<Sprite>(str));
}
The string ‘str’ is valid; it works absolutely fine with Unity. However, again, it’s not loading anything when running through the HoloLens.
字符串“str”有效;它与 Unity 配合得非常好。然而,再次强调,它在通过 HoloLens 运行时不会加载任何内容。
A:
You can’t read the Resources directory with the StreamReader
or the File
class. You must use Resources.Load
.
您无法使用StreamReader
或File
类读取 Resources 目录。您必须使用Resources.Load
。
1.The path is relative to any Resources folder inside the Assets folder of your project.
1 .该路径相对于项目的 Assets 文件夹内的任何 Resources 文件夹。
2.Do not include the file extension names such as .txt, .png, .mp3 in the path parameter.
2 、路径参数中不要包含.txt 、 .png 、 .mp3等文件扩展名。
3.Use forward slashes instead of back slashes when you have another folder inside the Resources folder. backslashes won’t work.
3 .当Resources文件夹中有另一个文件夹时,请使用正斜杠而不是反斜杠。反斜杠不起作用。
Text files:
文本文件:
TextAsset txtAsset = (TextAsset)Resources.Load("textfile", typeof(TextAsset));
string tileFile = txtAsset.text;
Supported TextAsset formats:
支持的文本资源格式:
txt .html .htm .xml .bytes .json .csv .yaml .fnt
txt .html .htm .xml .bytes .json .csv .yaml .fnt
Sound files:
声音文件:
AudioClip audio = Resources.Load("soundFile", typeof(AudioClip)) as AudioClip;
Image files:
图像文件:
Texture2D texture = Resources.Load("textureFile", typeof(Texture2D)) as Texture2D;
Sprites – Single:
精灵 – 单:
Image with Texture Type set to Sprite (2D and UI) and
纹理类型设置为Sprite(2D 和 UI)的图像以及
Image with Sprite Mode set to Single.
精灵模式设置为Single 的图像。
Sprite sprite = Resources.Load("spriteFile", typeof(Sprite)) as Sprite;
Sprites – Multiple:
精灵 – 多个:
Image with Texture Type set to Sprite (2D and UI) and
纹理类型设置为Sprite(2D 和 UI)的图像以及
Image with Sprite Mode set to Multiple.
精灵模式设置为Multiple 的图像。
Sprite[] sprite = Resources.LoadAll<Sprite>("spriteFile") as Sprite[];
Video files (Unity >= 5.6):
视频文件(Unity >= 5.6) :
VideoClip video = Resources.Load("videoFile", typeof(VideoClip)) as VideoClip;
GameObject Prefab:
游戏对象预制件:
GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject;
3D Mesh (such as FBX files)
3D 网格(例如 FBX 文件)
Mesh model = Resources.Load("yourModelFileName", typeof(Mesh)) as Mesh;
3D Mesh (from GameObject Prefab)
3D 网格(来自 GameObject Prefab)
MeshFilter modelFromGameObject = Resources.Load("yourGameObject", typeof(MeshFilter)) as MeshFilter;
Mesh loadedMesh = modelFromGameObject.sharedMesh; //Or design.mesh
3D Model (as GameObject) 3D 模型(作为游戏对象)
GameObject loadedObj = Resources.Load("yourGameObject") as GameObject;
//MeshFilter meshFilter = loadedObj.GetComponent<MeshFilter>();
//Mesh loadedMesh = meshFilter.sharedMesh;
GameObject object1 = Instantiate(loadedObj) as GameObject;
Accessing files in a sub-folder:
访问子文件夹中的文件:
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:
例如,如果您有一个shoot.mp3文件,该文件位于Resources文件夹中名为“ Sound ”的子文件夹中,则可以使用正斜杠:
AudioClip audio = Resources.Load("Sound/shoot", typeof(AudioClip)) as AudioClip;
Asynchronous Loading:
异步加载:
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;
}
To use: StartCoroutine(loadFromResourcesFolder());
使用方法: StartCoroutine(loadFromResourcesFolder());
本站发布的内容若无意中侵犯到您的权益,请联系我们,本站将在一个工作日内删除。

912sy » 在 Unity 中使用资源文件夹