在Unity中使用资源文件夹 [英] Using Resources Folder in 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)))
{
...
}
}
但是,在构建用于HoloLens部署的程序时,我能够运行代码,但是它不起作用.没有资源显示出来,当检查HoloLens Visual Studio解决方案(通过在Unity中选择build创建)时,我什至看不到资源或资产文件夹.我想知道我是在做错什么,还是有一种特殊的方式来处理这些资源.
还有图像和声音文件...
foreach (string str in im)
{
spriteList.Add(Resources.Load<Sprite>(str));
}
字符串'str'是有效的;它在Unity上绝对可以正常工作.但是,同样,在通过HoloLens运行时,它不会加载任何内容.
您不能使用StreamReader
或File
类读取Resources目录.您必须使用 Resources.Load
.
1 .该路径相对于项目的Assets文件夹内的任何Resources文件夹.
2 .请勿不包含文件扩展名,例如 .txt , .png , .mp3 在path参数中.
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;
Sprites-Single :
将纹理类型设置为 Sprite(2D和UI)和的图像
将 图片. 子画面-多个:Sprite sprite = Resources.Load("spriteFile", typeof(Sprite)) as Sprite;
图像
将 图像. 视频文件(Unity> = 5.6): GameObject Prefab : 3D网格物体(例如FBX文件) 3D网格物体(来自GameObject Prefab) 3D模型(作为GameObject) 访问子文件夹中的文件: 例如,如果您有一个 shoot.mp3 文件,该文件位于资源 Sound "中, em>文件夹,则使用正斜杠: 异步加载: 要使用: 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): 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... The string 'str' is valid; it works absolutely fine with Unity. However, again, it's not loading anything when running through the HoloLens. You can't read the Resources directory with the 1.The path is relative to any Resources folder inside the Assets folder of your project. 2.Do not include the file extension names such as .txt, .png, .mp3 in the path parameter. 3.Use forward slashes instead of back slashes when you have another folder inside the Resources folder. backslashes won't work. Text files: Supported TextAsset formats: txt .html .htm .xml .bytes .json .csv .yaml .fnt Sound files: Image files: Sprites - Single: Image with Texture Type set to Sprite (2D and UI) and Image with Sprite Mode set to Single. Sprites - Multiple: Image with Texture Type set to Sprite (2D and UI) and Image with Sprite Mode set to Multiple. Video files (Unity >= 5.6): GameObject Prefab: 3D Mesh (such as FBX files) 3D Mesh (from GameObject Prefab) 3D Model (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: Asynchronous Loading: To use: 这篇关于在Unity中使用资源文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!Sprite[] sprite = Resources.LoadAll<Sprite>("spriteFile") as Sprite[];
VideoClip video = Resources.Load("videoFile", typeof(VideoClip)) as VideoClip;
GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject;
Mesh model = Resources.Load("yourModelFileName", typeof(Mesh)) as Mesh;
MeshFilter modelFromGameObject = Resources.Load("yourGameObject", typeof(MeshFilter)) as MeshFilter;
Mesh loadedMesh = modelFromGameObject.sharedMesh; //Or design.mesh
GameObject loadedObj = Resources.Load("yourGameObject") as GameObject;
//MeshFilter meshFilter = loadedObj.GetComponent<MeshFilter>();
//Mesh loadedMesh = meshFilter.sharedMesh;
GameObject object1 = Instantiate(loadedObj) as GameObject;
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());
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)))
{
...
}
}
foreach (string str in im)
{
spriteList.Add(Resources.Load<Sprite>(str));
}
StreamReader
or the File
class. You must use Resources.Load
.TextAsset txtAsset = (TextAsset)Resources.Load("textfile", typeof(TextAsset));
string tileFile = txtAsset.text;
AudioClip audio = Resources.Load("soundFile", typeof(AudioClip)) as AudioClip;
Texture2D texture = Resources.Load("textureFile", typeof(Texture2D)) as Texture2D;
Sprite sprite = Resources.Load("spriteFile", typeof(Sprite)) as Sprite;
Sprite[] sprite = Resources.LoadAll<Sprite>("spriteFile") as Sprite[];
VideoClip video = Resources.Load("videoFile", typeof(VideoClip)) as VideoClip;
GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject;
Mesh model = Resources.Load("yourModelFileName", typeof(Mesh)) as Mesh;
MeshFilter modelFromGameObject = Resources.Load("yourGameObject", typeof(MeshFilter)) as MeshFilter;
Mesh loadedMesh = modelFromGameObject.sharedMesh; //Or design.mesh
GameObject loadedObj = Resources.Load("yourGameObject") as GameObject;
//MeshFilter meshFilter = loadedObj.GetComponent<MeshFilter>();
//Mesh loadedMesh = meshFilter.sharedMesh;
GameObject object1 = Instantiate(loadedObj) as GameObject;
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());