1. 程式人生 > >Unity C# TXT檔案寫入和讀取以及Split()的用法

Unity C# TXT檔案寫入和讀取以及Split()的用法

寫於2017-3-14

以下程式碼應該是從某些部落格摘的,當時沒標記,現在找不著了。

這裡做一下整理,方便自己用。

一、Unity C# TXT檔案寫入和讀取

void WriteTXT(){
StreamWriter sw;
FileInfo fi= new FileInfo(Application.streamingAssetsPath+"//"+name);
if (!fi.Exists) {
Debug.Log ("寫入 不存在");
sw = fi.CreateText ();
//sw.WriteLine ("this is a line.");
} else {
Debug.Log ("寫入 存在");
sw = fi.CreateText ();
//sw = fi.AppendText ();
//sw.WriteLine ("this is a line.");
}

for (int i = 0; i < Map.GetLength (0); i++) {
for (int j = 0; j < Map.GetLength (1); j++) {
sw.Write (Map [i, j]);
if (j != Map.GetLength (1) - 1)
sw.Write (",");
}
sw.Write ("\n");
}
sw.Close();
sw.Dispose();
}


void ReadTXT(){

FileInfo fi= new FileInfo(Application.streamingAssetsPath+"//"+name);
if (!fi.Exists) {
Debug.Log ("讀取 不存在");
//sr = fi.AppendText()
//sw.WriteLine ("this is a line.");
} else {
Debug.Log ("讀取 存在");
StreamReader sr = new StreamReader (Application.streamingAssetsPath+"//"+name);
string s;
//s=sr.ReadLine ();
//Debug.Log (s);
//string s2;
//s2 = s.Split (',')[0];
//Debug.Log (s2);

//Debug.Log ("長度為" + s.Split (',').GetLength (0));

GameObject obj = Resources.Load ("Prefabs/Block")as GameObject;
float objLength = obj.GetComponent<Renderer> ().bounds.size.x;
float objHeight = obj.GetComponent<Renderer> ().bounds.size.y;
int i = 0;
s = sr.ReadLine ();
while (s!=null)
{

for (int j = 0; j < s.Split (',').GetLength (0); j++) 
{
if (int.Parse(s.Split (',') [j]) == 0) {
}
if(int.Parse(s.Split (',') [j]) == 1) {
GameObject tile=Instantiate (Resources.Load ("Prefabs/Dirt"), this.transform.position + new Vector3 (j * objLength, -i * objHeight, 0), this.transform.rotation)as GameObject;
//tile.transform.parent = Ground.transform;
}
if(int.Parse(s.Split (',') [j]) == 2) {
GameObject tile=Instantiate (Resources.Load ("Prefabs/Sand"), this.transform.position + new Vector3 (j * objLength, -i * objHeight, 0), this.transform.rotation)as GameObject;
//tile.transform.parent =  Ground.transform;
}
if(int.Parse(s.Split (',') [j]) == 3) {
GameObject tile=Instantiate (Resources.Load ("Prefabs/Rock"), this.transform.position + new Vector3 (j * objLength, -i * objHeight, 0), this.transform.rotation)as GameObject;
//tile.transform.parent =  Ground.transform;
}
if(int.Parse(s.Split (',') [j]) == 7) {
GameObject tile=Instantiate (Resources.Load ("Prefabs/Water"), this.transform.position + new Vector3 (j * objLength, -i * objHeight, 0), this.transform.rotation)as GameObject;
//tile.transform.parent =  Ground.transform;
//tile.transform.position += new Vector3 (0, 0, -0.4f);
}
if(int.Parse(s.Split (',') [j]) == 8) {
GameObject tile=Instantiate (Resources.Load ("Prefabs/Platform"), this.transform.position + new Vector3 (j * objLength, -i * objHeight, 0), this.transform.rotation)as GameObject;
//tile.transform.parent =  Ground.transform;
}
if(int.Parse(s.Split (',') [j]) == 9) {
GameObject tile=Instantiate (Resources.Load ("Prefabs/Ladder"), this.transform.position + new Vector3 (j * objLength, -i * objHeight, 0), this.transform.rotation)as GameObject;
//tile.transform.parent =  Ground.transform;
}
}
i++;
s = sr.ReadLine ();
}
//sw = fi.AppendText ();
//sw.WriteLine ("this is a line.");
}
}
 

二、C#中Sprit()的用法


string.Split()的引數是一個字元陣列,把所給串按照引數裡包含的所有字元拆分成一截一截的字串,所以返回值是字串陣列,比如
string str="[email protected]&efg";
char[] separator={'@','&'};
string[] floatArray=str.Split(separator);
那麼執行結果就是字串陣列floatArray={"abc","d","efg"}