unity3D读取Txt文件中信息

unity读取txt文件

方法1:

 1 using UnityEngine;
 2 using System.Collections;
 3 using UnityEngine.UI;
 4 public class GetTxT : MonoBehaviour {
 5 
 6     public Text texts;
 7     void Start () {
 8         texts.text = Resources.Load("Mytext").ToString();
 9     }
10 }

然后在你的Resources文件中必须有Mytext文本。

运行后你发现我擦。然后没出现。什么问题呢

 

原来我的文本里面写的是中文。而文本是ANSI格式的需要改成UEF8格式的

然后运行就OK了。

但是我想读取外部的文本文件怎末办呢。这时候就要用到www类了。把你的文本放到StreamingAssets中。然后代码如下;

 

 1   public Text texts;
 2     void Start () {
 3         // texts.text = Resources.Load("Mytext").ToString();
 4         string path = "file://" + Application.streamingAssetsPath + "/" + "Mytext.txt";
 5         Debug.Log(path);
 6         StartCoroutine(DownLoadTxT(path));
 7     }
 8 
 9     IEnumerator DownLoadTxT(string path)
10     {
11         WWW www = new WWW(path);
12         yield return www;
13         if (www.isDone)
14         {
15             texts.text = www.text;
16 
17         }
18         
19     }

 

然后测试果也可以用,还有一种是读取AsseetBundle中的资源,跟这个一样,就是得先把AB包下载下来以后解压完后在读取资源。就不写了。

 

posted on 2016-11-17 15:31  萨瓦迪卡麦兜兜  阅读(12222)  评论(1编辑  收藏  举报

导航