DataManager.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 sspos
  172. {
  173. public float x;
  174. public float y;
  175. public float z;
  176. }
  177. public class SendSaveDataNew
  178. {
  179. public List<sspos> position;
  180. /// <summary>
  181. /// 景点ID
  182. /// </summary>
  183. public int id;
  184. /// <summary>
  185. /// 景点名称
  186. /// </summary>
  187. public string name { get; set; }
  188. /// <summary>
  189. /// 场景id
  190. /// </summary>
  191. public int projectId;
  192. /// <summary>
  193. /// 素材
  194. /// </summary>
  195. public List<MaterialOblNew> material { get; set; }
  196. /// <summary>
  197. /// 坐标 旋转 尺寸 如果没有数据的话默认为NULL
  198. /// </summary>
  199. public ObjectTransform spoittf { get; set; }
  200. /// <summary>
  201. /// 触发范围
  202. /// </summary>
  203. public SpoitPos triggerRange { get; set; }
  204. public int triggerImageId;
  205. }
  206. public class SendSaveData
  207. {
  208. public List<SpoitPos> position { get; set; }
  209. public int triggerImageId { get; set; }
  210. /// <summary>
  211. /// 景点ID
  212. /// </summary>
  213. public int id;
  214. /// <summary>
  215. /// 景点名称
  216. /// </summary>
  217. public string name { get; set; }
  218. /// <summary>
  219. /// 场景id
  220. /// </summary>
  221. public int projectId;
  222. /// <summary>
  223. /// 素材
  224. /// </summary>
  225. public List<MaterialObl> material { get; set; }
  226. /// <summary>
  227. /// 坐标 旋转 尺寸 如果没有数据的话默认为NULL
  228. /// </summary>
  229. public ObjectTransform spoittf { get; set; }
  230. /// <summary>
  231. /// 触发范围
  232. /// </summary>
  233. public SpoitPos triggerRange { get; set; }
  234. }