Loading external resource files – using Unity Default Resources

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).

Note

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.

Loading external resource files – using Unity Default Resources

Getting ready

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

How to do it...

To load the external resources by Unity Default Resources, do the following:

  1. Create a new 3D Unity project.
  2. In the Project window, create a new folder and rename it Resources.
  3. Import the externalTexture.jpg file and place it in the Resources folder.
  4. Create a 3D cube.
  5. Add the following C# Script to your cube:
    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);
      }
    }
  6. Play the scene. The texture will be loaded and displayed on the screen.
  7. If you have another image file, put a copy into the 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.

How it works...

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.

Note

Note: The filename string passed to Resources.Load() does not include the file extension (such as .jpg or .txt).

There's more...

There are some details that you don't want to miss.

Loading text files with this method

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.

Loading text files with this method

Loading and playing audio files with this method

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();
  }
}

See also

Refer to the following recipes in this chapter for more information:

  • Loading external resource files – by manually storing files in Unity Resources folder
  • Loading external resource files – by downloading files from the Internet
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.175.253