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.
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):
To load external resources by downloading them from the Internet, do the following:
RawImage
UI GameObject.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; } }
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 are some details that you don't want to miss.
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; }
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 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 AudioClipFor more information about the Unity
WWW
class visit http://docs.unity3d.com/ScriptReference/WWW.html.
3.17.76.175