When developing HoloLens applications in Unity, it is important to deal with resources such as text, images, and audio. Although these files can be accessed directly in the UNITY development environment, they need to use specific methods to access them in constructed HoloLens applications.
Use Resources.load to access resources
or Resources.Load
classes. The following is the method of loading different types of resources: StreamReader
File
Text file:
Sound file:
<code class="language-csharp">TextAsset txtAsset = (TextAsset)Resources.Load("textfile", typeof(TextAsset)); string tileFile = txtAsset.text;</code>
Image file:
<code class="language-csharp">AudioClip audio = Resources.Load("soundFile", typeof(AudioClip)) as AudioClip;</code>
A single elf:
<code class="language-csharp">Texture2D texture = Resources.Load("textureFile", typeof(Texture2D)) as Texture2D;</code>
Video file (unity & gt; = 5.6):
<code class="language-csharp">Sprite sprite = Resources.Load("spriteFile", typeof(Sprite)) as Sprite;</code>
Game object prefabricated body:
<code class="language-csharp">Sprite[] sprites = Resources.LoadAll<Sprite>("spriteFile");</code>
<code class="language-csharp">VideoClip video = Resources.Load("videoFile", typeof(VideoClip)) as VideoClip;</code>
Load 3D grid from the prefabricated body of the game:
<code class="language-csharp">GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject;</code>
Resource path precautions
<code class="language-csharp">Mesh model = Resources.Load("yourModelFileName", typeof(Mesh)) as Mesh;</code>
The resource path should be relative to the folder in the folder.
<code class="language-csharp">MeshFilter modelFromGameObject = Resources.Load("yourGameObject", typeof(MeshFilter)) as MeshFilter; Mesh loadedMesh = modelFromGameObject.sharedMesh;</code>
When specifying the path in the folder, use a positive slope (/) instead of the back slope ().
If you use a subfolder in a folder, use a positive slash to divide the subclase and file name.<code class="language-csharp">GameObject loadedObj = Resources.Load("yourGameObject"); GameObject object1 = Instantiate(loadedObj);</code>
Assets
.txt file loading example Resources
Resources
and string formatting. Use the relative path "Resources/Metadata.txt" to be more concise and efficient. The above is the detailed content of How to Access Resources in Unity for HoloLens Development?. For more information, please follow other related articles on the PHP Chinese website!