In this recipe, we will load an external image file, and display it on the screen, using the Unity Default Resources file (a library created at the time the game was compiled).
This method is perhaps the simplest way to store and read the external resource files. However, it is only appropriate when the contents of the resource files will not change after compilation, since the contents of these files are combined and compiled into the resources.assets
file.
The resources.assets
file can be found in the Data
folder for a compiled game.
In the 1362_10_01
folder, we have provided an image file, a text file, and an audio file in the .ogg
format for this recipe:
externalTexture.jpg
cities.txt
soundtrack.ogg
To load the external resources by Unity Default Resources, do the following:
Resources
.externalTexture.jpg
file and place it in the Resources
folder.using UnityEngine; using System.Collections; public class ReadDefaultResources : MonoBehaviour { public string fileName = "externalTexture"; private Texture2D externalImage; void Start () { externalImage = (Texture2D)Resources.Load(fileName); Renderer myRenderer = GetComponent<Renderer>(); myRenderer.material.SetTexture("_MainTex", externalImage); } }
Resources
folder. Then, in the Inspector window, change the public file name to the name of your image file and play the scene again. The new image will now be displayed.The Resources.Load(fileName)
statement makes Unity look inside its compiled project data file called resources.assets
for the contents of a file named externalTexture
. The contents are returned as a texture image, which is stored into the externalImage
variable. The last statement in the Start()
method sets the texture of the GameObject the script has been attached to our externalImage
variable.
There are some details that you don't want to miss.
You can load the external text files using the same approach. The private variable needs to be a string (to store the text file contents). The Start()
method uses a temporary TextAsset
object to receive the text file contents, and the text property of this object contains the string contents that are to be stored in the private variable textFileContents
:
public class ReadDefaultResourcesText : MonoBehaviour { public string fileName = "textFileName"; private string textFileContents; void Start () { TextAsset textAsset = (TextAsset)Resources.Load(fileName); textFileContents = textAsset.text; Debug.Log(textFileContents); } }
Finally, this string is displayed on the console.
You can load external audio files using the same approach. The private variable needs to be an AudioClip
:
using UnityEngine; using System.Collections; [RequireComponent (typeof (AudioSource))] public class ReadDefaultResourcesAudio : MonoBehaviour { public string fileName = "soundtrack"; private AudioClip audioFile; void Start (){ AudioSource audioSource = GetComponent<AudioSource>(); audioSource.clip = (AudioClip)Resources.Load(fileName); if(!audioSource.isPlaying && audioSource.clip.isReadyToPlay) audioSource.Play(); } }
3.145.175.253