123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- using Blue;
- using LitJson;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- /// <summary>
- /// 素材显示面板
- /// </summary>
- public class UIMaterialsPanel : UIPanelBase, EventObserver
- {
- /// <summary>
- /// 当前按钮代表景点父物体
- /// </summary>
- private GameObject m_ShowObj;
- private bool m_IsSelect;
- private ObjectValue m_ShowObjValues;
- private Transform m_Content;
- private Toggle m_BtnPrefab;
- /// <summary>
- /// 添加其他素材按钮
- /// </summary>
- private Toggle m_AddMaterBtn;
- /// <summary>
- /// 整体调整按钮
- /// </summary>
- private Toggle m_AllBtn;
- private ToggleGroup m_ToggleGroup;
- /// <summary>
- /// 当前景点信息
- /// </summary>
- private SpoitValue m_CurrentValue;
- private List<MaterialBtnItem> m_Btns;
- public EditorPanel EditorPanel { get; set; }
- protected override void OnAwake()
- {
- base.OnAwake();
- m_IsSelect = false;
- m_Btns = new List<MaterialBtnItem>();
- m_Content = CacheTransform.Find("Scroll View/Viewport/UIMaterialContent");
- m_ToggleGroup = m_Content.GetComponent<ToggleGroup>();
- m_AddMaterBtn = m_Content.Find("AddMaterialBtn").GetComponent<Toggle>();
- m_AllBtn = m_Content.Find("ALLBtn").GetComponent<Toggle>();
- m_BtnPrefab = m_Content.Find("MaterialBtn").gameObject.GetComponent<Toggle>();
- m_AddMaterBtn.onValueChanged.AddListener(OnClickAddMaterialBtn);
- m_AllBtn.onValueChanged.AddListener(OnClickAllBtn);
- }
- private void OnClickAddMaterialBtn(bool arg0)
- {
- EventData1 data1 = new EventData1(EventID.EVENT_1, null);
- data1.Send();
- EditorPanel.ChangeShowUI(false);
- }
- private void OnClickAllBtn(bool b)
- {
- if (b)
- {
- EventData1 data1 = new EventData1(EventID.EVENT_1, m_ShowObj);
- data1.Send();
- for (int i = 0; i < m_Btns.Count; i++)
- {
- if (m_Btns[i].gameObject.activeSelf && !m_Btns[i].IsCollider)
- {
- m_Btns[i].SelectObj(true);
- }
- }
- }
- }
- public override void Hide()
- {
- base.Hide();
- }
- /// <summary>
- /// 隐藏所有按钮,并清除数据
- /// </summary>
- private void HideBtns()
- {
- for (int i = 0; i < m_Btns.Count; i++)
- {
- m_Btns[i].ClearData();
- m_Btns[i].gameObject.SetActive(false);
- }
- }
- /// <summary>
- /// 当前场景的素材
- /// </summary>
- /// <param name="materialObjs"></param>
- public void SetMaterial(SpoitValue spoitValue)
- {
- m_ShowObj = null;
- m_ShowObjValues = null;
- m_AllBtn.gameObject.SetActive(false);
- HideBtns();
- m_CurrentValue = spoitValue;
- if (m_CurrentValue == null)
- {
- return;
- }
- ObjectValue objvalues = GameManager.Instance.GetSpoitObjs(spoitValue.id);
- if (objvalues != null)
- {
- m_ShowObj = objvalues.Object;
- m_ShowObjValues = objvalues;
- m_ShowObj.transform.localPosition = m_ShowObjValues.nowPos;
- m_ShowObj.transform.localEulerAngles = m_ShowObjValues.nowRot;
- m_ShowObj.transform.localScale = m_ShowObjValues.nowScale;
- if (m_ShowObjValues.ChildrenObj.Count > 0)
- {
- m_AllBtn.gameObject.SetActive(true);
- }
- }
- ////先设置物体触发器
- //MaterialBtnItem collider = null;
- //if (m_Btns.Count >= 1)
- //{
- // collider = m_Btns[0];
- // m_Btns.RemoveAt(0);
- //}
- //else
- //{
- // collider = Instantiate(m_BtnPrefab, m_Content).gameObject.AddComponent<MaterialBtnItem>();
- //}
- //if (collider != null)
- //{
- // collider.gameObject.SetActive(true);
- // collider.SetData(GameManager.Instance.GetCurrentColliderValue());
- // collider.transform.localScale = new Vector3(1, 1, 1);
- // collider.transform.localRotation = Quaternion.identity;
- //}
- var sucais = m_CurrentValue.material;
- var objValue = objvalues.ChildrenObj;
- int num = 0;
- for (int i = 0; i < sucais.Count; i++)
- {
- var matObj = sucais[i].materialList;
- var objvalue = objValue[i];
- for (int m = 0; m < matObj.Count; m++)
- {
- MaterialBtnItem item = null;
- num++;
- if (num < m_Btns.Count)
- {
- item = m_Btns[num];
- }
- else
- {
- item = Instantiate(m_BtnPrefab, m_Content).gameObject.GetComponent<MaterialBtnItem>();
- m_Btns.Add(item);
- }
- item.gameObject.SetActive(true);
- item.transform.localScale = new Vector3(1, 1, 1);
- item.transform.localRotation = Quaternion.identity;
- item.SetData(matObj[m], objvalue.ChildrenObj[m]);
- }
- }
- //if (collider != null)
- //{
- // m_Btns.Add(collider);
- // collider.transform.SetAsLastSibling();
- //}
- m_AddMaterBtn.transform.SetAsLastSibling();
- }
- public void ClosePanel()
- {
- m_ShowObj = null;
- m_ShowObjValues = null;
- m_CurrentValue = null;
- HideBtns();
- }
- /// <summary>
- /// 初始化ObjectValue数据
- /// </summary>
- public void InitObjTransForm()
- {
- m_ShowObjValues.InitObjTransform();
- for (int i = 0; i < m_Btns.Count; i++)
- {
- m_Btns[i].InitMatObj();
- }
- for (int i = 0; i < m_ShowObjValues.ChildrenObj.Count; i++)
- {
- var obj1 = m_ShowObjValues.ChildrenObj[i];
- for (int m = 0; m < obj1.ChildrenObj.Count; m++)
- {
- var obj2 = obj1.ChildrenObj[m];
- obj2.InitObjTransform();
- }
- }
- }
- /// <summary>
- /// 保存ObjectValue数据
- /// </summary>
- public void SaveMatObj()
- {
- m_ShowObjValues.SaveObjTransfoem();
- for (int i = 0; i < m_Btns.Count; i++)
- {
- m_Btns[i].SaveMatObj();
- }
- for (int i = 0; i < m_ShowObjValues.ChildrenObj.Count; i++)
- {
- var obj1 = m_ShowObjValues.ChildrenObj[i];
- for (int m = 0; m < obj1.ChildrenObj.Count; m++)
- {
- var obj2 = obj1.ChildrenObj[m];
- obj2.SaveObjTransfoem();
- }
- }
- GameManager.Instance.SaveSpoitObjectValue(m_ShowObjValues);
- GameManager.Instance.UpdateSpoitValue();
- SaveSpoits();
- }
- public void SaveSpoits()
- {
- Debug.Log("向服务器发送景点更新信息");
- if (m_ShowObj == null)
- {
- return;
- }
- Debug.Log("m_ShowObj存在");
- UIManager.Instance.ChangeCanvasPos();
- UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.SaveSpoitData);
- var spoit = GameManager.Instance.SpoitValues[GameManager.Instance.SpoitIndex];
- List<SendSaveData> saveData = new List<SendSaveData>();
- SendSaveData data = new SendSaveData();
- data.id = spoit.id;
- data.name = spoit.name;
- data.projectId = DataManager.Instance.ProjectID;
- data.material = spoit.material;
- data.spoittf = spoit.spoitTf;
- //data.triggerRange = spoit.SpoitTransform[0];
- saveData.Add(data);
- string sendData = JsonConvert.SerializeObject(saveData);
- //string sendData = JsonMapper.ToJson(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")
- {
- InstantiateCommand Command =new InstantiateCommand(
- InstantiateSystem.Instance.BlueObject.WarningPopUp,
- InstantiateSystem.Instance.BlueObject.NetErrorText);
- CommandSystem.Instance.Send(Command);
- return;
- }
- //Debug.LogError("SaveCallBack: " + message);
- JObject jObject = JObject.Parse(message);
- string str = jObject["message"].ToString();
- if(str!="更新成功")
- {
- InstantiateCommand Command =new InstantiateCommand(
- InstantiateSystem.Instance.BlueObject.WarningPopUp,
- InstantiateSystem.Instance.BlueObject.NetErrorText);
- CommandSystem.Instance.Send(Command);
- return;
- }
- else
- {
- Debug.Log("更新成功");
- }
- UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.SaveSpoitDataEnd);
- ((LoadingPanel)UIManager.Instance.GetUI(UINameConfig.LoadingPanel)).TextStr = str;
- }
- public void RemoveMat(GameObject obj)
- {
- StartCoroutine(WaitRemove(obj));
- }
- IEnumerator WaitRemove(GameObject obj)
- {
- Debug.Log("删除整个景点数据");
- //删除整个景点数据
- if (obj == m_ShowObj)
- {
- for (int i = 0; i < obj.transform.childCount; i++)
- {
- if (obj.transform.GetChild(i) != obj)
- {
- Destroy(obj.transform.GetChild(i).gameObject);
- }
- }
- yield return new WaitForEndOfFrame();
- //m_ShowObj = null;
- m_ShowObjValues.ChildrenObj.Clear();
- m_IsSelect = false;
- var spoit = GameManager.Instance.SpoitValues[GameManager.Instance.SpoitIndex];
- spoit.material = new List<MaterialObl>();
- GameManager.Instance.SaveSpoitObjectValue(m_ShowObjValues);
- GameManager.Instance.SpoitValues[GameManager.Instance.SpoitIndex] = spoit;
- SaveSpoits();
- }
- else
- {
- for (int i = 0; i < m_Btns.Count; i++)
- {
- m_Btns[i].SaveMatObj();
- if (m_Btns[i].MaterialObj == obj)
- {
- m_Btns[i].IsSelect = false;
- //var objvalue = m_Btns[i].ObjValue;
- Destroy(obj);
- yield return new WaitForEndOfFrame();
- }
- }
- SaveMatObj();
- }
- GameManager.Instance.UpdateEditorUI();
- }
- public void HandleEvent(EventData data)
- {
- switch (data.ID)
- {
- case EventID.EVENT_1:
- EventData1 data1 = (EventData1)data;
- if (data1.SelectObj == m_ShowObj)
- {
- m_IsSelect = true;
- }
- else
- {
- m_IsSelect = false;
- }
- break;
- case EventID.EVENT_2:
- break;
- default:
- break;
- }
- }
- }
|