Accessing Assets in Unity Projects for HoloLens Deployment
Developing HoloLens applications often involves loading assets like text, images, or audio from the Resources folder. However, the method for accessing these assets differs significantly between the Unity editor and a deployed HoloLens application.
Unity Editor Asset Loading
Within the Unity editor, you might attempt to load assets using file system paths, like this:
string basePath = Application.dataPath; string metadataPath = String.Format(@"\Resources\...\metadata.txt", list); 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)); }
This approach is incompatible with HoloLens deployments.
Correct Asset Loading for HoloLens
The correct method for loading assets in a HoloLens build relies exclusively on Resources.Load()
. Here's the proper technique:
1. Asset Path Specification:
Resources
folder within your project's Assets
folder..txt
, .png
, .mp3
, etc.) from the path./
) as path separators, even on Windows.2. Loading Asset Types:
Employ the appropriate Resources.Load()
overload for your asset type:
Text Files:
TextAsset txtAsset = Resources.Load<TextAsset>("textfile"); string tileFile = txtAsset.text;
Audio Files:
AudioClip audio = Resources.Load<AudioClip>("soundFile");
Image Files:
Texture2D texture = Resources.Load<Texture2D>("textureFile");
Sprites (Single):
Sprite sprite = Resources.Load<Sprite>("spriteFile");
Sprites (Multiple):
Sprite[] sprites = Resources.LoadAll<Sprite>("spriteFolder");
Video Files (Unity 5.6 ):
VideoClip video = Resources.Load<VideoClip>("videoFile");
Game Objects (Prefabs):
GameObject prefab = Resources.Load<GameObject>("shipPrefab");
3D Meshes:
Mesh mesh = Resources.Load<Mesh>("yourModelFileName");
Subfolders:
Access assets in subfolders using forward slashes:
AudioClip audio = Resources.Load<AudioClip>("Sound/shoot");
Asynchronous Loading:
For improved performance, use Resources.LoadAsync()
for asynchronous asset loading.
Summary:
By adhering to these guidelines, you can reliably load assets from the Resources
folder when deploying your Unity applications to HoloLens. Remember to always use Resources.Load()
and relative paths within the Resources
folder, omitting file extensions and using forward slashes.
The above is the detailed content of How to Correctly Load Resources from the Resources Folder in Unity for HoloLens Deployment?. For more information, please follow other related articles on the PHP Chinese website!