A straightforward way to store data in text files and then choose between them before compiling is to use a public variable of the class TextAsset
.
For this recipe, you'll need a text (.txt
) file. In the 0423_08_01
folder, we have provided two such files:
cities.txt
countries.txt
To load external text files using TextAsset
, perform the following steps:
cities.txt
)// file: ReadPublicTextAsset.cs using UnityEngine; using System.Collections; public class ReadPublicTextAsset : MonoBehaviour { public TextAsset dataTextFile; private string textData = ""; private void Start() { textData = dataTextFile.text; } private void OnGUI() { GUILayout.Label ( textData ); } }
cities.txt
file into the public string variable dataTextFile in the Inspector view.3.137.41.205