Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity Load text from resources

Tags:

c#

unity3d

I'm able to change the text of a UILabel (named about) with the following:

using UnityEngine;
using System.Collections;

public class about : MonoBehaviour
{
   void Start ()
   {
      UILabel lbl = GetComponent<UILabel>();
      lbl.text = "Hello World!";
   }
}

However things go awry when I want to load the label text from a text file in resources (Assets/Resources/about.txt)

lbl.text = Resources.Load(Application.dataPath + "/Resources/about") as String

So I'm not sure where I'm going wrong, and yes I have looked here.

like image 678
Ghoul Fool Avatar asked Feb 05 '14 16:02

Ghoul Fool


People also ask

How do I read a text file in Unity?

Another way to read and write data from a text file that's in a specific directory is to use the StreamWriter and StreamReader classes from the System.IO namespace to access characters from a byte stream and then load the file as a TextAsset in Unity.

Should I use resources load Unity?

Resources. Load is still very useful for loading prefabs that are common to many scenes. However I put it behind my own prefab loader so that I can load prefabs by references such as item SKU's, and I cache the objects that I load via Resources. Load.

What is difference between resources and StreamingAssets folder?

Streaming Assets : Any files placed in StreamingAssets are copied as it is to a particular folder on a target machine. Any asset placed inside StreamingAssets can be used while the application is running. Resources : Resources class allows you to find and access Objects including assets.

What is TextAsset in Unity?

Description. Represents a raw text or binary file asset.


1 Answers

I searched for alot of methods online and implemented them, but none of them worked. So I went to Unity docs. Here is a link hope it helps. Unity Read Resource
Read it carefully.

Edit:
here is a simple implementation:

  • Create Resources folder in Assets.
  • Place your text file in that folder(Resources).

code:

var textFile = Resources.Load<TextAsset>("file name only not path and no file extension(.txt)");

//store it in any List line by line(optional)
List<String> words = new List<string>(textFile.text.Split('\n'));
like image 133
Single-byte Avatar answered Oct 12 '22 11:10

Single-byte