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

At times, the contents of the external resource files may need to be changed after the game compilation. Hosting the 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, which allows for those files to be changed after the game compilation.

Note

This technique only works when you compile to a Windows or Mac stand alone executable—it will not work for Web Player builds, for example.

Getting ready

The 1362_10_01 folder provides the texture image that you can use for this recipe:

  • externalTexture.jpg

How to do it...

To load external resources by manually storing the files in the Resources folder, do the following:

  1. In a 2D project, create a new Image UI GameObject.
  2. Add the following C# script class as a component of your Image object:
    using UnityEngine;
    using System.Collections;
    
    using UnityEngine.UI;
    using System.IO;
    
    public class ReadManualResourceImageFile : MonoBehaviour {
      private string fileName = "externalTexture.jpg";
      private string url;
      private Texture2D externalImage;
    
      IEnumerator Start () {
        url = "file:" + Application.dataPath;
        url = Path.Combine(url, "Resources");
        url = Path.Combine(url, fileName);
    
        WWW www = new WWW (url);
        yield return www;
    
        Texture2D texture = www.texture;
        GetComponent<Image>().sprite = TextureToSprite(texture);
      }
    
      private Sprite TextureToSprite(Texture2D texture){
        Rect rect = new Rect(0, 0, texture.width, texture.height);
        Vector2 pivot = new Vector2(0.5f, 0.5f);
        Sprite sprite = Sprite.Create(texture, rect, pivot);
        return sprite;
      }
    }
  3. Build your (Windows or Mac) standalone executable.
  4. Copy the externalTexture.jpg image to your standalone's Resources folder.

    Note

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

    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.

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

  5. Run your standalone game application and the image will be displayed:
    How to do it...

How it works...

Note the need to use the System.IO and UnityEngine.UI packages for this recipe.

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

There's more...

There are some details that 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 file systems (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 form the file URL that is required.

See also

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

  • Loading external resource files – by Unity Default Resources
  • 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.144.107.193