GameModule.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 = Application.streamingAssetsPath + "/" + 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.ContainsKey(key))
  50. {
  51. if (key == "startMonsterXZ")
  52. {
  53. return -1;
  54. }
  55. if (key.Contains("RGB"))
  56. {
  57. return -1;
  58. }
  59. return float.Parse(globalDataDic[key]);
  60. }
  61. else
  62. {
  63. return -1;
  64. }
  65. }
  66. /// <summary>
  67. /// 双参数
  68. /// </summary>
  69. /// <param name="key"></param>
  70. /// <returns></returns>
  71. public Vector2 GetDoubleParams(string key)
  72. {
  73. if (globalDataDic.ContainsKey(key) && globalDataDic[key].Contains("|"))
  74. {
  75. string[] strs = globalDataDic[key].Split('|');
  76. return new Vector2(float.Parse(strs[0]), float.Parse(strs[1]));
  77. }
  78. else
  79. {
  80. return Vector2.zero;
  81. }
  82. }
  83. /// <summary>
  84. /// RGB值
  85. /// </summary>
  86. /// <param name="key"></param>
  87. /// <returns></returns>
  88. public string GetRGB(string key)
  89. {
  90. if (globalDataDic.ContainsKey(key) && key.Contains("RGB"))
  91. {
  92. return globalDataDic[key];
  93. }
  94. else
  95. {
  96. return "";
  97. }
  98. }
  99. #endregion
  100. }
  101. }