GameModule.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using UnityEngine;
  7. using UnityEngine.Networking;
  8. namespace WildAttack
  9. {
  10. /// <summary>
  11. /// Global.csv表
  12. /// </summary>
  13. public class GameModule : Singleton<GameModule>, IModule
  14. {
  15. #region Member
  16. Dictionary<string, string> globalDataDic;
  17. #endregion
  18. #region Override
  19. public void SaveData(string[] lineArr)
  20. {
  21. globalDataDic = new Dictionary<string, string>();
  22. for (int i = 1; i < lineArr.Length; i++)
  23. {
  24. string[] data = lineArr[i].Split(',');
  25. globalDataDic[data[0]] = data[1];
  26. }
  27. }
  28. public IEnumerator LoadCSV(string fileName, Action callback)
  29. {
  30. //刻意延迟,避免过快加载完成,因为遇到过加载完了,但其它GameObject脚本的Start()还没执行
  31. yield return new WaitForSeconds(0.2f);
  32. string sPath = CSVUtils.GetStreamingAssetPath() + "/" + fileName;
  33. UnityWebRequest www = UnityWebRequest.Get(sPath);
  34. yield return www.SendWebRequest();
  35. File.WriteAllText(Application.persistentDataPath + "/" + fileName, www.downloadHandler.text, Encoding.GetEncoding("utf-8"));
  36. www.Dispose();
  37. SaveData(CSVUtils.GetInstance().LoadFile(Application.persistentDataPath, fileName));
  38. callback();
  39. }
  40. #endregion
  41. #region Functions
  42. /// <summary>
  43. /// string类型value
  44. /// </summary>
  45. /// <param name="key"></param>
  46. /// <returns></returns>
  47. public float GetData(string key)
  48. {
  49. if (globalDataDic == null) return -1;
  50. if (globalDataDic.ContainsKey(key))
  51. {
  52. if (key == "startMonsterXZ")
  53. {
  54. return -1;
  55. }
  56. if (key.Contains("RGB"))
  57. {
  58. return -1;
  59. }
  60. return float.Parse(globalDataDic[key]);
  61. }
  62. else
  63. {
  64. return -1;
  65. }
  66. }
  67. /// <summary>
  68. /// 双参数
  69. /// </summary>
  70. /// <param name="key"></param>
  71. /// <returns></returns>
  72. public Vector2 GetDoubleParams(string key)
  73. {
  74. if (globalDataDic.ContainsKey(key) && globalDataDic[key].Contains("|"))
  75. {
  76. string[] strs = globalDataDic[key].Split('|');
  77. return new Vector2(float.Parse(strs[0]), float.Parse(strs[1]));
  78. }
  79. else
  80. {
  81. return Vector2.zero;
  82. }
  83. }
  84. /// <summary>
  85. /// RGB值
  86. /// </summary>
  87. /// <param name="key"></param>
  88. /// <returns></returns>
  89. public string GetRGB(string key)
  90. {
  91. if (globalDataDic.ContainsKey(key) && key.Contains("RGB"))
  92. {
  93. return globalDataDic[key];
  94. }
  95. else
  96. {
  97. return "";
  98. }
  99. }
  100. #endregion
  101. }
  102. }