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.
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.
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.
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.
Description. Represents a raw text or binary file asset.
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:
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'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With