| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using UnityEngine;
- using System.IO;
- using System.Text;
- using System;
- namespace WildAttack
- {
- /// <summary>
- /// CSV读表工具类
- /// </summary>
- public class CSVUtils : Singleton<CSVUtils>
- {
- public static string GetStreamingAssetPath() {
- string path =
- #if UNITY_ANDROID && !UNITY_EDITOR
- Application.streamingAssetsPath;
- #elif UNITY_IPHONE && !UNITY_EDITOR
- "file://" + Application.streamingAssetsPath;
- #elif UNITY_STANDALONE_WIN || UNITY_EDITOR
- "file://" + Application.streamingAssetsPath;
- #else
- string.Empty;
- #endif
- // 对路径进行 URL 编码
- path = Uri.EscapeUriString(path);
- //Debug.Log("读取文件路径:"+ path);
- return path;
- }
- #region Members
- //利用二维数组存储对应行和列中的字符串
- public string[][] m_ArrayData;
- #endregion
- #region Functions
- public string[] LoadFile(string path, string fileName)
- {
- //读取新文件前保证之前的数据为空
- m_ArrayData = new string[0][];
- string fillPath = path + "/" + fileName;
- //解析每一行的数据
- string[] lineArray;
- try
- {
- //注意编码方式,这里用的是Windows系统自定义的编码方式Encoding.Default,其实也就是GB2312(简体中文),编码不对应的话解析出来很可能是乱码
- lineArray = File.ReadAllLines(fillPath, Encoding.GetEncoding("utf-8"));
- //Debug.Log("file finded!");
- }
- catch
- {
- //Debug.Log("file not find!");
- return null;
- }
- return lineArray;
- }
- // 下面两个API改协程实现 pc可直接调用 android端用协程
- //public void SaveData(string[] lineArr)
- //{
- // m_ArrayData = new string[lineArr.Length][];
- // for (int i = 0; i < lineArr.Length; i++)
- // {
- // //CSV格式的文件采用英文逗号作为分隔符
- // m_ArrayData[i] = lineArr[i].Split(',');
- // }
- //}
- //外部调用的取值接口,需要先LoadFile()后才能返回正确的数据
- //public string GetVaule(int row, int col)
- //{
- // return m_ArrayData[row][col];
- //}
- #endregion
- }
- }
|