unity 安卓端读取Excel文件(xslx文件转为csv文件)

关于路径有4个类型:

 Application.dataPath(只读):该路径指向咱们Unity编辑器的Asset文件夹

Application.persistentDataPath(可读可写):该路径指向iOS和Android的沙盒路径

 Application.streamingAssetsPath(只读):streamingAsset文件夹路径,在任何平台均可以经过这个路径读取到文件夹里的内容

 Application.temporaryCachePath(只读):临时数据文件路径

Resources下一般在放预制,StreamingAssets下放二进制文件(csv、bin、txt、xml、json、AB包等),安卓不能通过File类和FileStream来读取这个路径,只能通过WWW类获取。

读取csv代码如下:

 public Dictionary<int, LingZhi> _InfoData = new Dictionary<int, LingZhi>();
    void Start()
    {
        StartCoroutine(Lingzhi());
        
        //Data infodata = CSV._Instance._InfoData[1];
        //Debug.Log(infodata.id);
        //Debug.Log(infodata.name);
        //Debug.Log(infodata.intro);
    }
    void Update()
    {
        
    }
    IEnumerator Lingzhi()
    {
        string fullFileName = Application.streamingAssetsPath + "/TianDao.csv";
        UnityWebRequest www = UnityWebRequest.Get(fullFileName);
        Debug.Log(www);
        yield return www.SendWebRequest();
        if (www.result == UnityWebRequest.Result.Success)
        {
            var fileData = www.downloadHandler.text; //文件内的数据信息
            fileData = fileData.Replace("\r", "");
            fileData = fileData.Replace("\"", "");
            string[] infoDatas = fileData.Split('\n');
            for (int i = 2; i < infoDatas.Length; i++)
            {
                if (infoDatas[i] != "")
                {
                    string[] infos = infoDatas[i].Split(',');
                    LingZhi data = new LingZhi(int.Parse(infos[0]), infos[1], int.Parse(infos[2]));
                    //{
                    //    id = int.Parse(infos[0]),
                    //    name = infos[1],
                    //    intro = int.Parse(infos[2]),
                    //};
                    _InfoData.Add(data.id, data);
                }
            }
            LingZhi infodata = _InfoData[1];
            Debug.Log(infodata.id);
            Debug.Log(infodata.name);
            Debug.Log(infodata.sum);
        }
        else
        {
          
        }
    }
}
[System.Serializable]
public class LingZhi
{
    public int id;
    public string name;
    public int sum;

    public LingZhi(int id,string name,int sum)
    {
        this.id = id;
        this.name = name;
        this.sum = sum;
    }
}

猜你喜欢

转载自blog.csdn.net/m0_71624363/article/details/132012709
今日推荐