Loading external resource files – by downloading files from the Internet

One way to store and read a text file data is to store the text files on the Web. In this recipe, the contents of a text file for a given URL are downloaded, read, and then displayed.

Getting ready

For this recipe, you need to have access to the files on a web server. If you run a local web server such as Apache, or have your own web hosting, then you can use the files in the 1362_10_01 folder and the corresponding URL.

Otherwise, you may find the following URLs useful; since they are the web locations of an image file (a Packt Publishing logo) and a text file (an ASCII-art badger picture):

How to do it...

To load external resources by downloading them from the Internet, do the following:

  1. In a 2D project, create a new RawImage UI GameObject.
  2. Add the following C# script class as a component of your image object:
    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    
    public class ReadImageFromWeb : MonoBehaviour {
      public string url = "http://www.packtpub.com/sites/default/files/packt_logo.png";
    
      IEnumerator Start() {
        WWW www = new WWW(url);
        yield return www;
        Texture2D texture = www.texture;
        GetComponent<RawImage>().texture = texture;
      }
    }
  3. Play the scene. Once downloaded, the contents of the image file will be displayed:
    How to do it...

How it works...

Note the need to use the UnityEngine.UI package for this recipe.

When the game starts, our Start() method starts the coroutine method called LoadWWW(). A coroutine is a method that can keep on running in the background without halting or slowing down the other parts of the game and the frame rate. The yield statement indicates that once a value can be returned for imageFile, the remainder of the method can be executed—that is, until the file has finished downloading, no attempt should be made to extract the texture property of the WWW object variable.

Once the image data has been loaded, execution will progress past the yield statement. Finally, the texture property of the RawImage GameObject, to which the script is attached, is changed to the image data that is downloaded from the Web (inside the texture variable of the www object).

There's more...

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

Converting from Texture to Sprite

While in the recipe we used a UI RawImage, and so could use the downloaded Texture directly, there may be times when we wish to work with a Sprite rather than a Texture. Use this method to create a Sprite object from a 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;
  }

Downloading a text file from the Web

Use this technique to download a text file:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ReadTextFromWeb : MonoBehaviour {
  public string url = "http://www.ascii-art.de/ascii/ab/badger.txt";

  IEnumerator Start(){
    Text textUI = GetComponent<Text>();
    textUI.text = "(loading file ...)";

    WWW www = new WWW(url);
    yield return www;
    string textFileContents = www.text;
    Debug.Log(textFileContents);

    textUI.text = textFileContents;
  }
}

The WWW class and the resource contents

The WWW class defines several different properties and methods to allow the 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 the web data as string
  • .texture: A read-only property, returning the web data as a Texture2D image
  • .GetAudioClip(): A method that returns the web data as an AudioClip

Note

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

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 manually storing files in the Unity Resources folder
..................Content has been hidden....................

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