标签:
个人认为这段代码可读性不是很好,因为我用了这么久看起来还是很不顺眼~! 但是实用还是非常实用的, 记录下来,以后有需要直接取走吧~
PS:遍历行数读取对应值的时候, 遍历值要+1, 因为遍历默认是从表头开始的,而真正的值是从第二行开始的, +1即从第二行开始遍历
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LoadText : MonoBehaviour
{
public string dataName;
string[][] Array;
string [] allitems;
void DescDataInit(){
//1到4步的内容是为了把文本内容完全copy下来并且使其可以数据化可以任意读取
TextAsset allItemDataBase = Resources.Load("Text/"+dataName, typeof(TextAsset)) as TextAsset; //1.读取文件
allitems = allItemDataBase.text.Split ("\r"[0]); // 2.读取每一行的内容
Array = new string [allitems.Length][]; // 3.一个二维数组,行和列分别为: 文本总行数和空
for(int i =0;i < allitems.Length; i++){ // 4. 遍历所有文本,以逗号为分隔符存储每一行的内容
Array[i] = allitems[i].Split ("," [0]);
}
}
string GetDataByIdAndName(string strName,int nId){
if (Array.Length <= 0)
return "";
int nRow = Array.Length;
int nCol = Array[0].Length;
for (int i = 0; i < nRow; i++) { //行
if (i == nId) {
for (int j = 0; j < nCol; j++) { //列
if (Array[0][j] == strName) {
return Array[i][j];
}
}
}
}
return "";
}
}
标签:
原文地址:http://www.cnblogs.com/hellozzz/p/4654710.html