CSVUtils.cs 2.4 KB

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