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:
<code class="language-C#">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)); }</code>
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:
<code class="language-C#">TextAsset txtAsset = Resources.Load<TextAsset>("textfile"); string tileFile = txtAsset.text;</code>
Audio Files:
<code class="language-C#">AudioClip audio = Resources.Load<AudioClip>("soundFile");</code>
Image Files:
<code class="language-C#">Texture2D texture = Resources.Load<Texture2D>("textureFile");</code>
Sprites (Single):
<code class="language-C#">Sprite sprite = Resources.Load<Sprite>("spriteFile");</code>
Sprites (Multiple):
<code class="language-C#">Sprite[] sprites = Resources.LoadAll<Sprite>("spriteFolder");</code>
Video Files (Unity 5.6 ):
<code class="language-C#">VideoClip video = Resources.Load<VideoClip>("videoFile");</code>
Game Objects (Prefabs):
<code class="language-C#">GameObject prefab = Resources.Load<GameObject>("shipPrefab");</code>
3D Meshes:
<code class="language-C#">Mesh mesh = Resources.Load<Mesh>("yourModelFileName");</code>
Subfolders:
Access assets in subfolders using forward slashes:
<code class="language-C#">AudioClip audio = Resources.Load<AudioClip>("Sound/shoot");</code>
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!