Using the resource folder in unity
I am developing a HoloLens project that needs to reference .txt files. I have files stored in the Resources folder and they work fine (when run through 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 a program to deploy HoloLens, I can run the code, but it doesn't work. None of the resources show up and when looking at the HoloLens Visual Studio solution (created by selecting an assembly in Unity) I don't even see the resources or assets folder. I am wondering if I am doing something wrong or if there is a specific way to deal with such resources.
Also with images and sound files ...
foreach (string str in im)
{
spriteList.Add(Resources.Load<Sprite>(str));
}
The string 'str' is valid; it works great with Unity. However, again, it doesn't download anything as it goes through the HoloLens.
source to share
You cannot read resource directory with class StreamReader
or File
. You must use Resources.Load
.
1 . The path refers to any Resources folder within the Assets folder of your project.
2 . Do not include file extension names like .txt, .png, .mp3 in the path parameter.
3 . Use leading slashes instead of backslashes when you have another folder in your Resources folder. backslashes won't work.
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
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 is set to Sprite (2D and UI) and
Sprite image is set to Single.
Sprite sprite = Resources.Load("spriteFile", typeof(Sprite)) as Sprite;
Sprites - several :
Image with texture type is set to Sprite (2D and UI) and
Sprite image is set to Multiple.
Sprite[] sprite = Resources.LoadAll<Sprite>("spriteFile") as Sprite[];
Video files (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 (e.g. FBX files)
Mesh model = Resources.Load("yourModelFileName", typeof(Mesh)) as Mesh;
3D Mesh (from GameObject Prefab)
MeshFilter modelFromGameObject = Resources.Load("yourGameObject", typeof(MeshFilter)) as MeshFilter;
Mesh loadedMesh = modelFromGameObject.sharedMesh; //Or design.mesh
3D Model (as GameObject)
GameObject loadedObj = Resources.Load("yourGameObject") as GameObject;
//MeshFilter meshFilter = loadedObj.GetComponent<MeshFilter>();
//Mesh loadedMesh = meshFilter.sharedMesh;
GameObject object1 = Instantiate(loadedObj) as GameObject;
Access to files in a subfolder :
For example, if you have a shoot.mp3 file that is located in a subfolder called Sound, which is placed in the Resources folder, you use a forward slash:
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;
}
For use: StartCoroutine(loadFromResourcesFolder());
source to share