Loading external resource files – by manually storing files in Unity's Resources folder

At times the contents of external resource files may need to be changed after the game is compiled. Hosting resource files on the Web may not be an option. There is a method of manually storing and reading files from the Resources folder of the compiled game. This allows for those files to be changed after the game's compilation.

Note

This technique only works when you compile to a Windows or Mac standalone executable. It will not work for Web Player builds, for example.

Getting ready

The 0423_07_01 folder provides the externalTexture.jpg texture image that you can use for this recipe.

How to do it...

  1. Add the following C# script to the Main Camera:
    // file: ReadManualResourceImageFile.cs
    using UnityEngine;
    using System.Collections;
    using System.IO;
    
    public class ReadManualResourceImageFile : MonoBehaviour {
      private string fileName = "externalTexture.jpg";
      private string url;
      private Texture2D externalImage;
    
      private void Start () {
         url = "file:" + Application.dataPath;
         url = Path.Combine(url, "Resources");
         url = Path.Combine(url, fileName);
        
        StartCoroutine( LoadWWW() );
      }
      
      private void OnGUI() {
        GUILayout.Label ( "url = " + url );
        GUILayout.Label ( externalImage );
      }
    
      private IEnumerator LoadWWW(){
        yield return 0;
        WWW www = new WWW (url);
        yield return www;
          externalImage = www.texture;
      }
    }
  2. Build your (Windows or Mac) standalone executable.
  3. Copy the externalTexture.jpg image into your standalone's Resources folder.

    Note

    You will need to place the files in the Resources folder manually after every compilation.

    WINDOWS and LINUX: When you create a Windows or Linux standalone executable, there is also a _Data folder created with the executable application file. The Resources folder can be found inside this data folder.

    MAC: A Mac standalone application executable looks like a single file, but it is actually a Mac OS package folder. Right-click the executable file and select Show Package Contents. You will then find the standalone's Resources folder inside the Contents folder.

  4. Run your standalone game application, and the image should be displayed.

How it works...

Note the need to use the System.IO package for this recipe.

When the executable runs, the WWW object spots that the URL starts with the word "file:" so Unity attempts to find the external resource file in its Resources folder and then load its contents.

There's more...

Here are some details you don't want to miss.

Avoiding cross-platform problems with Path.Combine() rather than "/" or ""

The filepath folder separator character is different for Windows and Mac filesystems ("" backslash for Windows, "/" forward slash for the Mac). However, Unity knows which kind of standalone you are compiling your project into, therefore the Path.Combine() method will insert the appropriate separator slash character to form the file URL that is required.

Loading a text file using this approach

To use this technique to load an external text file, you'll need a private string variable:

private string textFileContents = "(still loading file ...)";

Replace your LoadWWW() method with one that extracts the text of the loaded resource and stores it in your private string variable:

private IEnumerator LoadWWW(){
  yield return 0;
  WWW www = new WWW (url);
  yield return textFile;
    textFileContents = www.text;
}

Then change OnGUI() to display the string as a label:

private void OnGUI() {
  GUILayout.Label ( "url = " + url );
  GUILayout.Label ( textFileContents );
}

WWW and resource contents

The WWW class defines several different properties and methods to allow downloaded media resource file data to be extracted into appropriate variables for use in the game. The most useful of these include:

  • .text: A read-only property returning web data as a string
  • .texture: A read-only property returning web data as a Texture2D image
  • .GetAudioClip(): A method that returns web data as an AudioClip object

Note

For more information about the Unity WWW class, see http://docs.unity3d.com/Documentation/ScriptReference/WWW.html.

See also

  • The Loading external resource files – by Unity Default Resources recipe.
  • The Loading external resource files – by downloading files from the Internet recipe.
..................Content has been hidden....................

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