您的位置:首页 > 移动开发 > Unity3D

unity手游开发--读取Txt文本内容

2016-08-17 10:25 405 查看
在开发中需要经常用到读取数据,常用的方法是读取excel,但是在移动设备中是不支持其格式。最好的办法是转化成txt格式进行读取

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class LoadURL : MonoBehaviour
{

int x = 0;
public InputField input;
public static string url = "";
void Start ()
{
TextCsv();

}
void Update()
{
x = System.Int32.Parse(input.text);
TextCsv();

}
void TextCsv()
{
//读取csv二进制文件
TextAsset binAsset = Resources.Load("csv", typeof(TextAsset)) as TextAsset;
//显示在GUITexture中
// GetComponent<GUIText>().text = binAsset.text;

//读取每一行的内容
string[] lineArray = binAsset.text.Split("\r"[0]);

//创建二维数组
string[][] Array = new string[lineArray.Length][];

//把csv中的数据储存在二位数组中
for (int i = 0; i < lineArray.Length; i++)
{
Array[i] = lineArray[i].Split(";"[0]);
}

//通过索引即可得到数据内容
// Debug.Log(Array[0][1]);
// Debug.Log(lineArray[1]);
// Debug.Log(Array[2][1]);
string str = Array[x][1];
url = str;
GetComponent<GUIText>().text = str;
}

void OnGUI()
{
if (GUI.Button(new Rect(0, 0, 100, 100), "load"))
{
Application.LoadLevel(1);

}
}
}



工程链接:点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity TXT 读取