DataManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using LitJson;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using SC.XR.Unity;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using UnityEngine;
  9. public class DataManager : SingletonMono<DataManager>
  10. {
  11. /// <summary>
  12. /// 当前账号下所有场景信息
  13. /// </summary>
  14. private List<SceneValue> m_SceneData;
  15. /// <summary>
  16. /// 当前场景数据
  17. /// </summary>
  18. private SceneValue m_CurrentSceen;
  19. /// <summary>
  20. /// 当前场景ID
  21. /// </summary>
  22. private int m_ProjectID;
  23. /// <summary>
  24. /// 当前场景的所有景点
  25. /// </summary>
  26. private List<SpoitValue> m_Spoits;
  27. /// <summary>
  28. /// 场景所对应的物体
  29. /// </summary>
  30. private Dictionary<string, ObjectValue> m_SpoitObjs;
  31. /// <summary>
  32. /// Web端素材库
  33. /// </summary>
  34. private Dictionary<string, List<MaterialObl>> m_AllMaterials;
  35. public bool IsSavedData { get; set; }
  36. public List<SceneValue> SceneData
  37. {
  38. get { return m_SceneData; }
  39. set { m_SceneData = value; }
  40. }
  41. public SceneValue CurrentScene
  42. {
  43. get { return m_CurrentSceen; }
  44. set { m_CurrentSceen = value; }
  45. }
  46. public int ProjectID
  47. {
  48. get { return m_ProjectID; }
  49. set { m_ProjectID = value; }
  50. }
  51. public List<SpoitValue> Spoits
  52. {
  53. get { return m_Spoits; }
  54. set { m_Spoits = value; }
  55. }
  56. public Dictionary<string, List<MaterialObl>> AllMaterials
  57. {
  58. get { return m_AllMaterials; }
  59. set { m_AllMaterials = value; }
  60. }
  61. public void Awake()
  62. {
  63. IsSavedData = false;
  64. m_SpoitObjs = new Dictionary<string, ObjectValue>();
  65. m_SceneData = new List<SceneValue>();
  66. m_Spoits = new List<SpoitValue>();
  67. m_AllMaterials = new Dictionary<string, List<MaterialObl>>();
  68. }
  69. /// <summary>
  70. /// 获取景点属性
  71. /// </summary>
  72. /// <param name="spoitName">景点名称</param>
  73. public SpoitValue GetSpoitValue(string spoitName)
  74. {
  75. SpoitValue spoit = null;
  76. for (int i = 0; i < m_Spoits.Count; i++)
  77. {
  78. if (m_Spoits[i].name == spoitName)
  79. {
  80. spoit = m_Spoits[i];
  81. break;
  82. }
  83. }
  84. return spoit;
  85. }
  86. /// <summary>
  87. /// 改变当前场景中的景点信息
  88. /// </summary>
  89. /// <param name="value">景点数据</param>
  90. public void ChangeSpoitValue(SpoitValue value)
  91. {
  92. for (int i = 0; i < m_Spoits.Count; i++)
  93. {
  94. if (value.name == m_Spoits[i].name)
  95. {
  96. m_Spoits[i] = value;
  97. }
  98. }
  99. }
  100. /// <summary>
  101. /// 获取当前景点所对应的物体
  102. /// </summary>
  103. /// <param name="spoitName">景点名称</param>
  104. /// <returns></returns>
  105. public ObjectValue GetSpoitObj(string spoitName)
  106. {
  107. return m_SpoitObjs[spoitName];
  108. }
  109. public void SaveSpoits()
  110. {
  111. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.SaveSpoitData);
  112. Spoits = GameManager.Instance.SpoitValues;
  113. List<SendSaveData> saveData = new List<SendSaveData>();
  114. for (int i = 0; i < Spoits.Count; i++)
  115. {
  116. var spoit = Spoits[i];
  117. SendSaveData data = new SendSaveData();
  118. data.id = spoit.id;
  119. data.name = spoit.name;
  120. data.projectId = ProjectID;
  121. data.material = spoit.material;
  122. data.spoittf = spoit.spoitTf;
  123. data.triggerRange = spoit.SpoitTransform[0];
  124. saveData.Add(data);
  125. }
  126. string sendData = JsonConvert.SerializeObject(saveData);
  127. Debug.Log("更新景点数据: " + sendData);
  128. HttpTool.Instance.PostTest("/viewpoint/update", sendData, SaveCallBack);
  129. //string str = JsonMapper.ToJson(Spoits);
  130. FileManager.WriteFile(sendData, Application.persistentDataPath + "/a.json");
  131. //Debug.Log(str);
  132. }
  133. private void SaveCallBack(string message)
  134. {
  135. if (message == "UnityWebRequest Error") return;
  136. Debug.Log("SaveCallBack: " + message);
  137. JObject jObject = JObject.Parse(message);
  138. string str = jObject["message"].ToString();
  139. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.SaveSpoitDataEnd);
  140. ((LoadingPanel)UIManager.Instance.GetUI(UINameConfig.LoadingPanel)).TextStr = str;
  141. }
  142. private void OnApplicationQuit()
  143. {
  144. //if (!IsSavedData)
  145. //{
  146. // SaveSpoits();
  147. //}
  148. }
  149. }
  150. public class SendSaveAnchor
  151. {
  152. public int id { get; set; }
  153. public ObjectTransform objectTransform { get; set; }
  154. }
  155. public class SendSaveData
  156. {
  157. /// <summary>
  158. /// 景点ID
  159. /// </summary>
  160. public int id;
  161. /// <summary>
  162. /// 景点名称
  163. /// </summary>
  164. public string name { get; set; }
  165. /// <summary>
  166. /// 场景id
  167. /// </summary>
  168. public int projectId;
  169. /// <summary>
  170. /// 素材
  171. /// </summary>
  172. public List<MaterialObl> material { get; set; }
  173. /// <summary>
  174. /// 坐标 旋转 尺寸 如果没有数据的话默认为NULL
  175. /// </summary>
  176. public ObjectTransform spoittf { get; set; }
  177. /// <summary>
  178. /// 触发范围
  179. /// </summary>
  180. public SpoitPos triggerRange { get; set; }
  181. }