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().bounds.size.x;
        float objHeight = obj.GetComponent().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="abc@d&efg";
char[] separator={'@','&'};
string[] floatArray=str.Split(separator);
那么执行结果就是字符串数组floatArray={"abc","d","efg"}

你可能感兴趣的:(Unity开发)