Unity读取txt注意事项

(1)在读取txt数据时,如果遇到读取的每一行数据存储,那么使用‘\r’ '\n'

如:string[] str =  sText.Split(new char[] {'\r', '\n' });

读取Txt文件:

public static void Load()
    {
        if (Application.platform == RuntimePlatform.Android)
        {
            string[] strArray = LoadFile("/sdcard/nobody.txt");	//android路径
        }

        TextAsset configFile = Resources.Load("Config/nobody", typeof(TextAsset)) as TextAsset;
        string[] stringArray = configFile.text.Split(new char[] { '\r', '\n' }, System.StringSplitOptions.RemoveEmptyEntries);//获取txt中每行数据
    }

    static string[] LoadFile(string fullPath)
    {
        StreamReader sr = null;
        try
        {
            sr = File.OpenText(fullPath);//打开文件
        }
        catch (Exception e)
        {
            return null;
        }

        string line;
        ArrayList arrlist = new ArrayList();
        while ((line = sr.ReadLine()) != null)
            arrlist.Add(line);//按行读取,获取每行数据

        sr.Close();
        sr.Dispose();

        string[] strArray = new string[arrlist.Count];
        for (int i = 0; i < arrlist.Count; ++i)
            strArray[i] = (string)arrlist[i];
        return strArray;
    }

encoding编码转换类 : http://blog.163.com/elite_lcf/blog/static/678395382009416168396/

本文持续更新中...

你可能感兴趣的:(Unity3D)