Game Development Reference
In-Depth Information
In these cases, you want a specific range of abilities. The first one is the ability to
dynamically load data from the file into memory in a way that Unity can parse
and understand. The second is the ability to change and edit the file contents, even
after importing it into Unity, and then have the effects of the changes update in
the game without requiring code changes. The third is the ability to compile and
distribute your standalone game with the file included as part of the main Unity
build, rather than as a separate and editable file alongside the main executable. To
elaborate further on the third point, you don't usually want to distribute your game
as a standalone build alongside separate and external files, such as XML files, which
can be opened and edited by the gamer. Instead, as the developer, you want to edit
and change the files from the Unity Editor, and you want the files themselves to be
compiled and built into your final Unity standalone project, like other assets. You
can achieve this using resource folders.
To use resource folders, create a folder named resources in the Unity project.
A project can feature none, one, or more resources folders. Inside the folder,
add all assets, such as text files that can be loaded by Unity at runtime:
Adding external files to the resources folder
Once a file is added to the resources folder, you can load it into memory with the
Resources.Load function. See the following code sample 10-1, which loads a sample
text asset into a UI text component:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
//---------------------------------------------
public class LoadTextData : MonoBehaviour
{
//Reference to UI Text Component
private Text MyText = null;
//Reference to text asset in resources folder
private TextAsset TextData = null;
//---------------------------------------------
// Use this for initialization
 
Search WWH ::




Custom Search