DataManager.cs 5.9 KB

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