在开发中需要经常用到读取数据,常用的方法是读取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);
}
}
}
工程链接: 点击打开链接