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);
}
}
}
工程链接:点击打开链接
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);
}
}
}
工程链接:点击打开链接
相关文章推荐
- Lucene3.1 之读取txt文本文档内容
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例 .
- 1.读取TXT文本内容
- Unity 简单读取txt文档内容的方法
- Android读取SD卡.TXT文本内容
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法
- c# 读取txt文本内容
- python读取 .txt 文本内容以及将程序执行结果写入txt文件
- 2.读取TXT文本内容,显示至图形界面中
- Unity读取本地文本txt的研究一
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- java读取txt文本的内容
- 随机读取某一个txt文本文档的内容
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- java中读取txt文本内容
- [object-c]读取txt文本内容
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- 请教Ado.Net按文本读取CSV/Txt文件时,如何禁止将内容转换成数字
- ajax读取txt文本内容