CSVUtils.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using UnityEngine;
  2. using System.IO;
  3. using System.Text;
  4. namespace WildAttack
  5. {
  6. /// <summary>
  7. /// CSV读表工具类
  8. /// </summary>
  9. public class CSVUtils : Singleton<CSVUtils>
  10. {
  11. public static string GetStreamingAssetPath() {
  12. string path =
  13. #if UNITY_ANDROID && !UNITY_EDITOR
  14. Application.streamingAssetsPath;
  15. #elif UNITY_IPHONE && !UNITY_EDITOR
  16. "file://" + Application.streamingAssetsPath;
  17. #elif UNITY_STANDLONE_WIN || UNITY_EDITOR
  18. "file://" + Application.streamingAssetsPath;
  19. #else
  20. string.Empty;
  21. #endif
  22. return path;
  23. }
  24. #region Members
  25. //利用二维数组存储对应行和列中的字符串
  26. public string[][] m_ArrayData;
  27. #endregion
  28. #region Functions
  29. public string[] LoadFile(string path, string fileName)
  30. {
  31. //读取新文件前保证之前的数据为空
  32. m_ArrayData = new string[0][];
  33. string fillPath = path + "/" + fileName;
  34. //解析每一行的数据
  35. string[] lineArray;
  36. try
  37. {
  38. //注意编码方式,这里用的是Windows系统自定义的编码方式Encoding.Default,其实也就是GB2312(简体中文),编码不对应的话解析出来很可能是乱码
  39. lineArray = File.ReadAllLines(fillPath, Encoding.GetEncoding("utf-8"));
  40. //Debug.Log("file finded!");
  41. }
  42. catch
  43. {
  44. //Debug.Log("file not find!");
  45. return null;
  46. }
  47. return lineArray;
  48. }
  49. // 下面两个API改协程实现 pc可直接调用 android端用协程
  50. //public void SaveData(string[] lineArr)
  51. //{
  52. // m_ArrayData = new string[lineArr.Length][];
  53. // for (int i = 0; i < lineArr.Length; i++)
  54. // {
  55. // //CSV格式的文件采用英文逗号作为分隔符
  56. // m_ArrayData[i] = lineArr[i].Split(',');
  57. // }
  58. //}
  59. //外部调用的取值接口,需要先LoadFile()后才能返回正确的数据
  60. //public string GetVaule(int row, int col)
  61. //{
  62. // return m_ArrayData[row][col];
  63. //}
  64. #endregion
  65. }
  66. }