123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- using LitJson;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using SC.XR.Unity;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- public class DataManager : SingletonMono<DataManager>
- {
- /// <summary>
- /// 当前账号下所有场景信息
- /// </summary>
- private List<SceneValue> m_SceneData;
- /// <summary>
- /// 当前场景数据
- /// </summary>
- private SceneValue m_CurrentSceen;
- /// <summary>
- /// 当前场景ID
- /// </summary>
- private int m_ProjectID;
- /// <summary>
- /// 当前场景的所有景点
- /// </summary>
- private List<SpoitValue> m_Spoits;
- /// <summary>
- /// 场景所对应的物体
- /// </summary>
- private Dictionary<string, ObjectValue> m_SpoitObjs;
- /// <summary>
- /// Web端素材库
- /// </summary>
- private Dictionary<string, List<MaterialObl>> m_AllMaterials;
- public bool IsSavedData { get; set; }
- public List<SceneValue> SceneData
- {
- get { return m_SceneData; }
- set { m_SceneData = value; }
- }
- public SceneValue CurrentScene
- {
- get { return m_CurrentSceen; }
- set { m_CurrentSceen = value; }
- }
- public int ProjectID
- {
- get { return m_ProjectID; }
- set { m_ProjectID = value; }
- }
- public List<SpoitValue> Spoits
- {
- get { return m_Spoits; }
- set { m_Spoits = value; }
- }
- public Dictionary<string, List<MaterialObl>> AllMaterials
- {
- get { return m_AllMaterials; }
- set { m_AllMaterials = value; }
- }
- public void Awake()
- {
- IsSavedData = false;
- m_SpoitObjs = new Dictionary<string, ObjectValue>();
- m_SceneData = new List<SceneValue>();
- m_Spoits = new List<SpoitValue>();
- m_AllMaterials = new Dictionary<string, List<MaterialObl>>();
- }
- /// <summary>
- /// 获取景点属性
- /// </summary>
- /// <param name="spoitName">景点名称</param>
- public SpoitValue GetSpoitValue(string spoitName)
- {
- SpoitValue spoit = null;
- for (int i = 0; i < m_Spoits.Count; i++)
- {
- if (m_Spoits[i].name == spoitName)
- {
- spoit = m_Spoits[i];
- break;
- }
- }
- return spoit;
- }
- /// <summary>
- /// 改变当前场景中的景点信息
- /// </summary>
- /// <param name="value">景点数据</param>
- public void ChangeSpoitValue(SpoitValue value)
- {
- for (int i = 0; i < m_Spoits.Count; i++)
- {
- if (value.name == m_Spoits[i].name)
- {
- m_Spoits[i] = value;
- }
- }
- }
- /// <summary>
- /// 获取当前景点所对应的物体
- /// </summary>
- /// <param name="spoitName">景点名称</param>
- /// <returns></returns>
- public ObjectValue GetSpoitObj(string spoitName)
- {
- return m_SpoitObjs[spoitName];
- }
- public void SaveSpoits()
- {
- UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.SaveSpoitData);
- Spoits = GameManager.Instance.SpoitValues;
- List<SendSaveData> saveData = new List<SendSaveData>();
- for (int i = 0; i < Spoits.Count; i++)
- {
- var spoit = Spoits[i];
- SendSaveData data = new SendSaveData();
- data.id = spoit.id;
- data.name = spoit.name;
- data.projectId = ProjectID;
- data.material = spoit.material;
- data.spoittf = spoit.spoitTf;
- data.triggerRange = spoit.SpoitTransform[0];
- saveData.Add(data);
- }
- string sendData = JsonConvert.SerializeObject(saveData);
- Debug.Log("更新景点数据: " + sendData);
- HttpTool.Instance.PostTest("/viewpoint/update", sendData, SaveCallBack);
- //string str = JsonMapper.ToJson(Spoits);
- FileManager.WriteFile(sendData, Application.persistentDataPath + "/a.json");
- //Debug.Log(str);
- }
- private void SaveCallBack(string message)
- {
- if (message == "UnityWebRequest Error") return;
- Debug.Log("SaveCallBack: " + message);
- JObject jObject = JObject.Parse(message);
- string str = jObject["message"].ToString();
- UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.SaveSpoitDataEnd);
- ((LoadingPanel)UIManager.Instance.GetUI(UINameConfig.LoadingPanel)).TextStr = str;
- }
- private void OnApplicationQuit()
- {
- //if (!IsSavedData)
- //{
- // SaveSpoits();
- //}
- }
- }
- public class SendSaveAnchor
- {
- public int id { get; set; }
- public ObjectTransform objectTransform { get; set; }
-
- }
- public class SendSaveData
- {
- /// <summary>
- /// 景点ID
- /// </summary>
- public int id;
- /// <summary>
- /// 景点名称
- /// </summary>
- public string name { get; set; }
- /// <summary>
- /// 场景id
- /// </summary>
- public int projectId;
- /// <summary>
- /// 素材
- /// </summary>
- public List<MaterialObl> material { get; set; }
- /// <summary>
- /// 坐标 旋转 尺寸 如果没有数据的话默认为NULL
- /// </summary>
- public ObjectTransform spoittf { get; set; }
- /// <summary>
- /// 触发范围
- /// </summary>
- public SpoitPos triggerRange { get; set; }
- }
|