using UnityEngine; using UnityEngine.UI; using QFramework; using System.Collections.Generic; using UniRx; using System.Linq; using System.IO; using System; using LitJson; namespace QFramework.MREditor { public enum UIEditorEvent { Start = QMgrID.UI, OnUIOpenElenemt, OnAddSpoitObj, OnSelectObjName, OnSaveDataSetting, OnRemoveDataSetting, OnAddNewMaterial, } public class OnUIOpenElenemt : QMsg { public bool state; public OnUIOpenElenemt(bool state) : base((int)UIEditorEvent.OnUIOpenElenemt) { this.state = state; } } public class OnAddSpoitObj : QMsg { public string path; public OnAddSpoitObj(string path) : base((int)UIEditorEvent.OnAddSpoitObj) { this.path = path; } } public class OnSelectObjName : QMsg { public string spoitName; public string Name; public string CMVName; public OnSelectObjName(string Name, string CMVName, string spoitName) : base((int)UIEditorEvent.OnSelectObjName) { this.Name = Name; this.CMVName = CMVName; this.spoitName = spoitName; } } public class OnSaveDataSetting : QMsg { public Vector3 pos; public Vector3 rot; public Vector3 scale; public string name; public OnSaveDataSetting(string name, Vector3 pos, Vector3 rot, Vector3 scale) : base((int)UIEditorEvent.OnSaveDataSetting) { this.name = name; this.pos = pos; this.rot = rot; this.scale = scale; } } public class OnRemoveDataSetting : QMsg { public string name; public OnRemoveDataSetting(string name) : base((int)UIEditorEvent.OnRemoveDataSetting) { this.name = name; } } public class OnAddNewMaterial : QMsg { public CompositeMaterialValue newMaterial; public OnAddNewMaterial(CompositeMaterialValue objValue) : base((int)UIEditorEvent.OnAddNewMaterial) { this.newMaterial = objValue; } } public class EditorPanelData : UIPanelData { public SceneValue SceneValue = new SceneValue(); public List listLocalMaterials = new List(); public List listUpLoadMaterials = new List(); } public partial class EditorPanel : UIPanel { private Transform selectObj; private Transform selectCMVObj; private GameObject selectSpoit; public Dictionary> dicObjs; public Dictionary> dicCMVOjbes; public Dictionary dicSelectBackImage; protected override void ProcessMsg(int eventId, QMsg msg) { Debug.Log(eventId); switch ((UIEditorEvent)eventId) { case UIEditorEvent.Start: break; case UIEditorEvent.OnUIOpenElenemt: var onUIOpenElenemt = msg as OnUIOpenElenemt; Debug.Log(onUIOpenElenemt.state); OpenElenemt(onUIOpenElenemt.state); break; case UIEditorEvent.OnAddSpoitObj: var OnAddSpoitObj = msg as OnAddSpoitObj; Debug.Log(OnAddSpoitObj.path +" ?????"); break; case UIEditorEvent.OnSelectObjName: var OnSelectObjName = msg as OnSelectObjName; Debug.Log(OnSelectObjName.Name); SelectedObj(OnSelectObjName.Name, OnSelectObjName.spoitName); Debug.Log("OnSelectObjName"); break; case UIEditorEvent.OnSaveDataSetting: var OnSaveDataSetting = msg as OnSaveDataSetting; Debug.Log(OnSaveDataSetting.name); // SaveData(OnSaveDataSetting.name, OnSaveDataSetting.pos, OnSaveDataSetting.rot, OnSaveDataSetting.scale); SaveData(OnSaveDataSetting.name); break; case UIEditorEvent.OnRemoveDataSetting: var OnRemoveDataSetting = msg as OnRemoveDataSetting; RemoveData(); break; case UIEditorEvent.OnAddNewMaterial: var OnAddNewMaterial = msg as OnAddNewMaterial; Debug.Log(OnAddNewMaterial.newMaterial); // AddMaterialObj(OnAddNewMaterial.newMaterial); AddCMaterialObj(OnAddNewMaterial.newMaterial); break; } } protected override void OnInit(IUIData uiData = null) { mData = uiData as EditorPanelData ?? new EditorPanelData(); selectObj = null; selectCMVObj = null; selectSpoit = null; dicObjs = new Dictionary>(); // dicCMVOjbes = new Dictionary>(); dicSelectBackImage = new Dictionary(); RegisterEvent(UIEditorEvent.OnUIOpenElenemt); RegisterEvent(UIEditorEvent.OnAddSpoitObj); RegisterEvent(UIEditorEvent.OnSelectObjName); RegisterEvent(UIEditorEvent.OnSaveDataSetting); RegisterEvent(UIEditorEvent.OnRemoveDataSetting); RegisterEvent(UIEditorEvent.OnAddNewMaterial); Debug.Log(mData.listUpLoadMaterials.Count); try { UIAddMaterial.Initialized(mData.listLocalMaterials, mData.listUpLoadMaterials, SingleMaterial); UIEditor.Initialized(mData.SceneValue.listSpoit); UISpoits.Initialized(mData.SceneValue.listSpoit, SpoitBtn, MaterialBtn, UIMaterials); UISpoits.Hide(); } catch (Exception ) { ErrorLogPanel.Instance.Show(" UI模块出现未知错误 "); } //UIMaterials.Hide(); try { List listSpoit = new List(); mData.SceneValue.listSpoit.ForEach(item => { // Debug.Log(item.name); listSpoit.Add(item); // LoadSpoitObj(item.name, item); }); SpoitValue spoit = null; for (int i = 0; i < listSpoit.Count; i++) { for (int j = 0; j < listSpoit.Count; j++) { if (listSpoit[i].id < listSpoit[j].id) { spoit = listSpoit[i]; listSpoit[i] = listSpoit[j]; listSpoit[j] = spoit; spoit = null; } } } listSpoit.ForEach(item => { Debug.Log(item.name + " " + item.id); LoadSpoitObj(item.name, item); }); } catch (Exception) { ErrorLogPanel.Instance.Show(" 初始化场景信息出现错误"); } } protected override void OnOpen(IUIData uiData = null) { //transform.parent = GameObject.Find("Canvas").transform; //RectTransform rect = transform.GetComponent(); //rect.localPosition = Vector3.zero; //rect.localScale = Vector3.one; transform.parent.eulerAngles = new Vector3(30, 0, 0); } protected override void OnShow() { if (selectSpoit != null) selectSpoit.SetActive(true); gameObject.SetActive(true); } protected override void OnHide() { foreach (var item in dicObjs) { item.Value[0].SetActive(false); } if (selectSpoit != null) selectSpoit.SetActive(false); gameObject.SetActive(false); } protected override void OnClose() { } public void OpenElenemt(bool state) { if (state) { UIEditor.Show(); UIAddMaterial.Close(); } else { UIEditor.Close(); UIAddMaterial.Show(); } } /// /// 加载景点 /// /// /// public void LoadSpoitObj(string name, SpoitValue spoit) { dicObjs.Add(spoit.name, new List()); GameObject target = new GameObject(spoit.name + "Target"); GameObject targetShow = new GameObject("TargetShowObj"); GameObject spoitObj = new GameObject(spoit.name); spoitObj.transform.parent = target.transform; spoitObj.transform.LocalIdentity(); targetShow.transform.parent = target.transform; targetShow.transform.LocalIdentity(); VuforialTargetMove vuforialTarget = target.AddComponent(); vuforialTarget.isClose = true; vuforialTarget.showObjs.Add(spoitObj.transform); vuforialTarget.listObjs.Add(targetShow.AddComponent()); VuforialControl.Instance.list_Target.Add(vuforialTarget); dicObjs[spoit.name].Add(spoitObj); List allMaterial = new List(); spoit.material.ForEach(item => { Debug.Log(item.name); LoadCMaterialObj(item, spoitObj); }); spoitObj.SetActive(false); Debug.Log(spoit.name+"AAA"); //SelectedObj(name, spoit.Name); SetObjMaterial(spoitObj, spoit.spoitTf); } /// /// 加载组合素材 /// /// /// public void LoadCMaterialObj(CompositeMaterialValue cMValue, GameObject spoitObj) { GameObject cMVObj = new GameObject(cMValue.name); cMVObj.transform.parent = spoitObj.transform; cMVObj.transform.LocalIdentity(); CMaterialValueShow cMVShow = cMVObj.AddComponent(); cMVShow.spoitname = spoitObj.name; cMVShow.cMValue = cMValue; Debug.Log(cMVShow.name); dicObjs[spoitObj.name].Add(cMVObj); SetObjMaterial(cMVObj, cMValue.objectTransform); //if (cMValue.objectTransform != null) // cMValue.objectTransform.SetObjectTransform(cMVObj.transform); //else // Debug.LogError(cMValue.name +" ObjectTransform is NULL "); } public void SetObjMaterial(GameObject obj, ObjectTransform objectData) { if (objectData == null) return; if (objectData.nowScale == Vector3.zero) { if (objectData.startScale == Vector3.zero) objectData.startScale = Vector3.one; obj.transform.localPosition = Vector3.zero; obj.transform.localEulerAngles = Vector3.zero; obj.transform.localScale = Vector3.one; } else { obj.transform.localPosition = objectData.nowPos; obj.transform.localEulerAngles = objectData.nowRot; obj.transform.localScale = objectData.nowScale; } } /// /// 选择物体 /// /// /// public void SelectedObj(string name, string spoitName) { if (!dicObjs.ContainsKey(spoitName)) mData.SceneValue.listSpoit.ForEach(item => { Debug.LogError("未找到 景点 "+ spoitName); //if (item.Name == spoitName) // Loader(name, item); }); else { //int i = 0; //foreach (var item in dicObjs) //{ // item.Value[0].SetActive(false); // if (item.Key == spoitName) // { // item.Value[0].SetActive(true); // item.Value[0].transform.parent.position = GameManager.Instance.listEditorBJs[0].transform.position += new Vector3(0, 0, -0.1f); // item.Value[0].transform.parent.eulerAngles = new Vector3(item.Value[0].transform.parent.eulerAngles.x, GameManager.Instance.listEditorBJs[0].transform.eulerAngles.y, item.Value[0].transform.parent.eulerAngles.z); // GameManager.Instance.SetImageBJ(i); // } // i++; //} dicObjs[spoitName].ForEach(item => { Debug.Log(item.name + " "+name); if (item.name == name) { //Debug.Log(selectObj.name); //if (selectObj != null && selectObj.name != selectSpoit.name) //{ //// selectObj.GetComponent().SelectBj.SetActive(false); //} if(selectObj != null&&dicSelectBackImage.ContainsKey(selectSpoit.name+ selectObj.name)) { dicSelectBackImage[selectSpoit.name + selectObj.name].SelectObj(false); } selectSpoit = dicObjs[spoitName][0]; selectObj = item.transform; if (selectObj.parent.name == selectSpoit.name) selectCMVObj = item.transform; else selectCMVObj = selectObj.parent; Debug.Log(selectSpoit.name + " " + selectObj.name); UIEditor.SelectMaterial(item.transform); if (dicSelectBackImage.ContainsKey(selectSpoit.name + selectObj.name)) { dicSelectBackImage[selectSpoit.name + selectObj.name].SelectObj(true); } //if (selectObj.name != selectSpoit.name) //{ // // selectObj.GetComponent().SelectBj.SetActive(true); //} } }); //dicObjs[spoitName].ForEach(item => //{ // if (item.name == name) // { // selectSpoit = dicObjs[spoitName][0]; // selectObj = item.transform; // UIEditor.SelectMaterial(item.transform); // } //}); } } public void SelectSpoitObj(string name) { // Debug.Log(name + "@@@@@@"); UISpoits.SelectSpoitObj(name); SelectedObj(name, name); HttpSocket.Instance.SendIpToPointTrigger(name); } /// /// 添加组合素材 /// /// public void AddCMaterialObj(CompositeMaterialValue value) { if (selectSpoit.IsNull()) return; UIMaterials.AddMaterial(selectSpoit.name, value, MaterialBtn); AddData(value); LoadCMaterialObj(value, selectSpoit); } /// /// 保存数据 /// /// 选中的物体名 /// 位置 /// 旋转 /// 尺寸 public void SaveData(string name, Vector3 pos, Vector3 rot, Vector3 scale) { if (selectObj == null || selectSpoit==null) return; // selectCMVObj = new GameObject("样式测试").transform; GameManager.Instance.text2.text = selectObj.name + " " + name + " " + selectCMVObj.name + " " + selectSpoit.name; GameManager.Instance.text4.text = "准备保存"; Debug.Log(name + " " + selectObj.name + " "+selectCMVObj.name +" "+selectSpoit.name); SpoitValue spoitValue = null; //for (int i = 0; i < mData.SceneValue.listSpoit.Count; i++) //{ // if (mData.SceneValue.listSpoit[i].name == selectSpoit.name) // { // if (selectSpoit.name == selectObj.name) // 保存的是景点属性 // { // Debug.Log(" 景点已保存 "); // mData.SceneValue.listSpoit[i].spoitTf.nowPos = pos; // mData.SceneValue.listSpoit[i].spoitTf.nowRot = rot; // if (scale == Vector3.zero) // scale = mData.SceneValue.listSpoit[i].spoitTf.startScale; // mData.SceneValue.listSpoit[i].spoitTf.nowScale = scale; // // mData.SceneValue.listSpoit[i].updateTime = DateTime.Now; // break; // } // spoitValue = mData.SceneValue.listSpoit[i]; // break; // } //} //if (spoitValue != null) //{ // for (int i = 0; i < spoitValue.material.Count; i++) // { // if (spoitValue.material[i].name == selectCMVObj.name) // { // if (spoitValue.material[i].name == selectObj.name) // { // Debug.Log(" 组合素材属性已保存 "); // spoitValue.material[i].objectTransform.nowPos = pos; // spoitValue.material[i].objectTransform.nowRot = rot; // if (scale == Vector3.zero) // scale = spoitValue.material[i].objectTransform.startScale; // spoitValue.material[i].objectTransform.nowScale = scale; // break; // } // for (int j = 0; j < spoitValue.material[i].materialList.Count; j++) // { // if (spoitValue.material[i].materialList[j].name == selectObj.name) // { // Debug.Log(" 素材已保存 "); // spoitValue.material[i].materialList[j].ObjectTransform.nowPos = pos; // spoitValue.material[i].materialList[j].ObjectTransform.nowRot = rot; // if (scale == Vector3.zero) // scale = spoitValue.material[i].materialList[j].ObjectTransform.startScale; // spoitValue.material[i].materialList[j].ObjectTransform.nowScale = scale; // break; // } // } // break; // } // } // for (int i = 0; i < mData.SceneValue.listSpoit.Count; i++) // { // if (mData.SceneValue.listSpoit[i].name == spoitValue.name) // { // mData.SceneValue.listSpoit[i] = spoitValue; // break; // } // } //} for (int i = 0; i < mData.SceneValue.listSpoit.Count; i++) { if (mData.SceneValue.listSpoit[i].name == selectSpoit.name) { Debug.Log(" 景点已保存 "); mData.SceneValue.listSpoit[i].spoitTf.nowPos = pos; mData.SceneValue.listSpoit[i].spoitTf.nowRot = rot; if (scale == Vector3.zero) scale = mData.SceneValue.listSpoit[i].spoitTf.startScale; mData.SceneValue.listSpoit[i].spoitTf.nowScale = scale; // mData.SceneValue.listSpoit[i].updateTime = DateTime.Now; spoitValue = mData.SceneValue.listSpoit[i]; break; } } if (spoitValue != null) { for (int i = 0; i < spoitValue.material.Count; i++) { if (spoitValue.material[i].name == selectCMVObj.name) { Debug.Log(" 组合素材属性已保存 "); spoitValue.material[i].objectTransform.nowPos = pos; spoitValue.material[i].objectTransform.nowRot = rot; if (scale == Vector3.zero) scale = spoitValue.material[i].objectTransform.startScale; spoitValue.material[i].objectTransform.nowScale = scale; break; for (int j = 0; j < spoitValue.material[i].materialList.Count; j++) { Debug.Log(" 素材已保存 "); spoitValue.material[i].materialList[j].ObjectTransform.nowPos = pos; spoitValue.material[i].materialList[j].ObjectTransform.nowRot = rot; if (scale == Vector3.zero) scale = spoitValue.material[i].materialList[j].ObjectTransform.startScale; spoitValue.material[i].materialList[j].ObjectTransform.nowScale = scale; break; } break; } } for (int i = 0; i < mData.SceneValue.listSpoit.Count; i++) { if (mData.SceneValue.listSpoit[i].name == spoitValue.name) { mData.SceneValue.listSpoit[i] = spoitValue; break; } } } } public void SaveData(string name) { List list_Material; list_Material = dicObjs[selectSpoit.name]; Debug.Log(" SelectSpoit Name : " + selectSpoit.name); // list_Material.ForEach(item => //{ // SaveData(item.name, item.transform.localPosition, item.transform.localEulerAngles, item.transform.localScale); //}); SaveData(list_Material); GameManager.Instance.Save(mData.SceneValue); } private void SaveData(List list_Material) { SpoitValue spoitValue = null; for (int i = 0; i < mData.SceneValue.listSpoit.Count; i++) { if (mData.SceneValue.listSpoit[i].name == selectSpoit.name) { Debug.Log(" 景点已保存 "); mData.SceneValue.listSpoit[i].spoitTf.nowPos = list_Material[0].transform.localPosition; mData.SceneValue.listSpoit[i].spoitTf.nowRot = list_Material[0].transform.localEulerAngles; if (list_Material[0].transform.localScale == Vector3.zero) list_Material[0].transform.localScale = mData.SceneValue.listSpoit[i].spoitTf.startScale; mData.SceneValue.listSpoit[i].spoitTf.nowScale = list_Material[0].transform.localScale; // mData.SceneValue.listSpoit[i].updateTime = DateTime.Now; spoitValue = mData.SceneValue.listSpoit[i]; break; } } Transform material; Debug.Log(" ****** "+spoitValue.name); if(spoitValue != null) { for (int i = 0; i < spoitValue.material.Count; i++) { material = FindMaterial(list_Material, spoitValue.material[i].name); if(material!=null) { Debug.Log(" 组合素材已保存 "); spoitValue.material[i].objectTransform.nowPos = material.localPosition; spoitValue.material[i].objectTransform.nowRot = material.localEulerAngles; spoitValue.material[i].objectTransform.nowScale = material.localScale; } for (int j = 0; j < spoitValue.material[i].materialList.Count; j++) { material = FindMaterial(list_Material, spoitValue.material[i].materialList[j].name); if (material != null) { Debug.Log(" 素材已保存 "); spoitValue.material[i].materialList[j].ObjectTransform.nowPos = material.localPosition; spoitValue.material[i].materialList[j].ObjectTransform.nowRot = material.localEulerAngles; spoitValue.material[i].materialList[j].ObjectTransform.nowScale = material.localScale; } } } } } private Transform FindMaterial(List list_Material, string name ) { for (int i = 1; i < list_Material.Count; i++) { Debug.Log(list_Material[i].name + " " + name); if (list_Material[i].name == name) return list_Material[i].transform; } return null; } /// /// 移除选中数据 /// public void RemoveData() { if (selectObj == null) return; if (selectSpoit.name == selectObj.name) return; SpoitValue spoitValue = null; for (int i = 0; i < mData.SceneValue.listSpoit.Count; i++) { if (mData.SceneValue.listSpoit[i].name == selectSpoit.name) { //if (selectSpoit.name == selectObj.name) //{ // mData.SceneValue.listSpoit.Remove(mData.SceneValue.listSpoit[i]); // break; //} spoitValue = mData.SceneValue.listSpoit[i]; break; } } if (spoitValue != null) { for (int i = 0; i < spoitValue.material.Count; i++) { if (spoitValue.material[i].name == selectObj.name) { spoitValue.material.Remove(spoitValue.material[i]); break; } for (int j = 0; j < spoitValue.material[i].materialList.Count; j++) { if (spoitValue.material[i].materialList[j].name == selectObj.name) { spoitValue.material[i].materialList.Remove(spoitValue.material[i].materialList[j]); break; } } } for (int i = 0; i < mData.SceneValue.listSpoit.Count; i++) { if (mData.SceneValue.listSpoit[i].name == spoitValue.name) { mData.SceneValue.listSpoit[i] = spoitValue; break; } } } UIMaterials.RemoveMaterials(selectSpoit.name+ selectObj.name); selectObj.gameObject.SetActive(false); selectObj = null; GameManager.Instance.Save(mData.SceneValue); } //public void AddData(MaterialObjValue value) //{ // for (int i = 0; i < mData.SceneValue.listSpoit.Count; i++) // { // if(mData.SceneValue.listSpoit[i].name == selectObj.name) // { // mData.SceneValue.listSpoit[i].listMaterial.Add(value); // GameManager.Instance.Save(mData.SceneValue); // break; // } // } //} /// /// 新添加数据 /// /// public void AddData(CompositeMaterialValue value) { for (int i = 0; i < mData.SceneValue.listSpoit.Count; i++) { if (mData.SceneValue.listSpoit[i].name == selectObj.name) { Debug.Log(JsonMapper.ToJson( mData.SceneValue.listSpoit[i])); mData.SceneValue.listSpoit[i].material.Add(value); Debug.Log(JsonMapper.ToJson(mData.SceneValue.listSpoit[i])); GameManager.Instance.Save(mData.SceneValue); break; } } } } }