Book Image

Mastering Unity Scripting

By : Alan Thorn
Book Image

Mastering Unity Scripting

By: Alan Thorn

Overview of this book

Table of Contents (17 chapters)
Mastering Unity Scripting
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Working with Text Data Assets


Throughout all examples so far, we've considered text directly stored in string objects, but you can also work with text files in Unity. Specifically, you can load in text from external sources. Here, I will demonstrate how.

Text Assets – static loading

The first method is to drag-and-drop a text file into a Unity project that imports the text asset. The file is imported as a TextAssets type, as shown here:

Importing text files into Unity as TextAssets

You can access the file and its text data from any script file by exposing a TextAsset public member, as shown in the following code sample 6-25:

//--------------------------------------------------
using UnityEngine;
using System.Collections;
//--------------------------------------------------
public class TextFileAccess : MonoBehaviour 
{
   //Reference a text file
   public TextAsset TextData = null;

   // Use this for initialization
   void Start () 
   {
         //Display text in file
         Debug.Log (TextData...