using DG.Tweening; using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; using XMLTool; using System; using static XMLTool.Body3D; namespace QFramework.Example { public class UIBody3DData : UIPanelData { public Body3D.Body body = new Body3D.Body(); } public partial class UIBody3D : UIPanel { GameObject root; public Dictionary bodyList { get; set; } = new Dictionary(); public class BodyListItem { public Body3D.Body root; public List bodys = new List(); public int index = 0; public string GetCurName() { if (index > bodys.Count - 1 || index < 0) { return null; } return bodys[index]; } public void Add() { if (index < bodys.Count) { string name = bodys[index]; var body = root.subBody[name]; Utility.FindObj(body.Path)?.SetActive(true); index++; } } public void Sub() { if (index > 0) { index--; string name = bodys[index]; var body = root.subBody[name]; Utility.FindObj(body.Path)?.SetActive(false); } } } Dictionary bodyListIndex = new Dictionary(); float leftBgOriginalY = 0; ResLoader loader; Shader shader; protected override void OnInit(IUIData uiData = null) { TypeEventSystem.Global.Register(OnModuleQuithandler).UnRegisterWhenGameObjectDestroyed(this); loader = ResLoader.Allocate(); leftBgOriginalY = LeftBg.rectTransform.sizeDelta.y; DragBtn.onValueChanged.AddListener(isOn => { CheckHighUI(DragBtn.transform, isOn); DragBtn.transform.Find("SubBtns").gameObject.SetActive(isOn); Body3DController.Instance.allowDrag = isOn; Body3DController.Instance.SetStatus(Body3DController.Status.Drag, isOn); TypeEventSystem.Global.Send(); }); DragBack.onClick.AddListener(() => { GameObject obj = Body3DController.Instance.PopMoveObj(); if (obj != null) { obj.GetComponent().OnDoubleClick(); } }); ActiveBtn.onValueChanged.AddListener(isOn => { CheckHighUI(ActiveBtn.transform, isOn); ActiveBtn.transform.Find("SubBtns").gameObject.SetActive(isOn); Body3DController.Instance.SetStatus(Body3DController.Status.Active, isOn); }); ActiveBack.onClick.AddListener(() => { Body3DController.Instance.PopActiveObj()?.gameObject.SetActive(true); }); ResetBtn.onClick.AddListener(() => { ResetCamera(0.5f); }); MenuBtn.onClick.AddListener(() => { //StringEventSystem.Global.Register($"On{UIBody3DMenuTree.Name}Close", OnUIBody3DMenuTreeClose); UIKit.OpenPanelAsync(canvasLevel: UILevel.PopUI, new UIBody3DMenuTreeData() { body = mData.body }).ToAction().StartGlobal(); //HideSelf(true); }); DragReset.onClick.AddListener(() => { while (Body3DController.Instance.moveObjs.Count > 0) { GameObject obj = Body3DController.Instance.PopMoveObj(); if (obj != null) { obj.GetComponent().OnDoubleClick(); } } }); HideAll.onValueChanged.AddListener(isOn => { if (isOn) { LeftBg.GetComponent().enabled = true; DOTween.To(() => LeftBg.rectTransform.sizeDelta.y, (value) => { LeftBg.rectTransform.sizeDelta = new Vector2(LeftBg.rectTransform.sizeDelta.x, value); }, 52f, 0.5f).OnComplete(() => { LeftBg.transform.GetChild(0).gameObject.SetActive(false); }); RightContent.GetComponent().DORestart(); HideAll.transform.Find("Label").GetComponent().text = "显示"; } else { LeftBg.transform.Find("ResetBtn").gameObject.SetActive(true); DOTween.To(() => LeftBg.rectTransform.sizeDelta.y, (value) => { LeftBg.rectTransform.sizeDelta = new Vector2(LeftBg.rectTransform.sizeDelta.x, value); }, leftBgOriginalY, 0.5f).OnComplete(() => { LeftBg.GetComponent().enabled = false; }); RightContent.GetComponent().DOPlayBackwards(); HideAll.transform.Find("Label").GetComponent().text = "隐藏"; } CloseBtn.gameObject.SetActive(!isOn); }); MouseBtn.onValueChanged.AddListener(isOn => { CheckHighUI(MouseBtn.transform, isOn); if (isOn) { UIKit.OpenPanelAsync().ToAction().StartGlobal(); } else { UIKit.HidePanel(); } }); DrawBtn.onValueChanged.AddListener((isOn) => { CheckHighUI(DrawBtn.transform, isOn); if (isOn) { UIKit.OpenPanelAsync().ToAction().StartGlobal(); } else { UIKit.HidePanel(); } }); RotMode.onValueChanged.AddListener(isOn => { if (isOn) { RotMode.transform.Find("Normal").gameObject.SetActive(false); RotMode.transform.Find("High").gameObject.SetActive(true); RotMode.transform.Find("Label").GetComponent().text = "球型旋转"; RotMode.transform.Find("Label").GetComponent().color = Color.white; Show3DCamera.instance.ChangeMode(Show3DCamera.RotationType.Spherical); } else { RotMode.transform.Find("Normal").gameObject.SetActive(true); RotMode.transform.Find("High").gameObject.SetActive(false); RotMode.transform.Find("Label").GetComponent().text = "直立旋转"; RotMode.transform.Find("Label").GetComponent().color = new Color(74f / 255f, 91f / 255f, 116f / 255f); Show3DCamera.instance.ChangeMode(Show3DCamera.RotationType.Orbit); } }); LineMode.onValueChanged.AddListener(isOn => { if (isOn) { LineMode.transform.Find("Label").GetComponent().text = "取消线框"; LineMode.transform.Find("Label").GetComponent().color = Color.white; if (shader == null) { shader = Shader.Find("URP/WhiteOutline"); } TypeEventSystem.Global.Send(new OnChangeMat() { shader = shader }); } else { LineMode.transform.Find("Label").GetComponent().text = "线框模式"; LineMode.transform.Find("Label").GetComponent().color = new Color(74f / 255f, 91f / 255f, 116f / 255f); TypeEventSystem.Global.Send(new OnChangeMat() { shader = null }); } }); LockMode.onValueChanged.AddListener(isOn => { if (isOn) { LockMode.transform.Find("Normal").gameObject.SetActive(false); LockMode.transform.Find("High").gameObject.SetActive(true); LockMode.transform.Find("Label").GetComponent().text = "解锁屏幕"; LockMode.transform.Find("Label").GetComponent().color = Color.white; } else { LockMode.transform.Find("Normal").gameObject.SetActive(true); LockMode.transform.Find("High").gameObject.SetActive(false); LockMode.transform.Find("Label").GetComponent().text = "锁定屏幕"; LockMode.transform.Find("Label").GetComponent().color = new Color(74f / 255f, 91f / 255f, 116f / 255f); } Show3DCamera.instance.lockMove = isOn; }); CloseBtn.onClick.AddListener(() => { Hide(); TypeEventSystem.Global.Send(); UIKit.OpenPanelAsync().ToAction().StartGlobal(); }); BgBtn.onValueChanged.AddListener(isOn => { if (isOn) { BgBtn.transform.Find("High").gameObject.SetActive(true); BgSelect.GetComponent().DOPlayForward(); } else { BgBtn.transform.Find("High").gameObject.SetActive(false); BgSelect.GetComponent().DOPlayBackwards(); } }); White.onValueChanged.AddListener(isOn => { White.transform.Find("High").gameObject.SetActive(isOn); if (isOn) { StringEventSystem.Global.Send("BgChange", White.name); White.transform.Find("Label").GetComponent().color = Color.white; } else { White.transform.Find("Label").GetComponent().color = new Color(74f / 255f, 91f / 255f, 116f / 255f); } }); Black.onValueChanged.AddListener(isOn => { Black.transform.Find("High").gameObject.SetActive(isOn); if (isOn) { StringEventSystem.Global.Send("BgChange", Black.name); Black.transform.Find("Label").GetComponent().color = Color.white; } else { Black.transform.Find("Label").GetComponent().color = new Color(74f / 255f, 91f / 255f, 116f / 255f); } }); Grey.onValueChanged.AddListener(isOn => { Grey.transform.Find("High").gameObject.SetActive(isOn); if (isOn) { StringEventSystem.Global.Send("BgChange", Grey.name); Grey.transform.Find("Label").GetComponent().color = Color.white; } else { Grey.transform.Find("Label").GetComponent().color = new Color(74f / 255f, 91f / 255f, 116f / 255f); } }); Gradient.onValueChanged.AddListener(isOn => { Gradient.transform.Find("High").gameObject.SetActive(isOn); if (isOn) { StringEventSystem.Global.Send("BgChange", Gradient.name); Gradient.transform.Find("Label").GetComponent().color = Color.white; } else { Gradient.transform.Find("Label").GetComponent().color = new Color(74f / 255f, 91f / 255f, 116f / 255f); } }); PicBtn.onClick.AddListener(() => { string fileName = ChinarFileController.SaveProject(".png"); if (string.IsNullOrEmpty(fileName)) { return; } TypeEventSystem.Global.Send(new BeginScreenShot()); gameObject.SetActive(false); // 生成文件名,包含时间戳 //string fileName = "Screenshot_" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".png"; // 调用截屏方法 ScreenCapture.CaptureScreenshot(fileName); ActionKit.DelayFrame(1, () => { gameObject.SetActive(true); TypeEventSystem.Global.Send(new EndScreenShot()); }).StartGlobal(); }); help.onClick.AddListener(() => { UIKit.OpenPanelAsync(canvasLevel: UILevel.PopUI).ToAction().Start(this); }); TypeEventSystem.Global.Register((arg) => Hide()).UnRegisterWhenGameObjectDestroyed(gameObject); } /// /// 检查是否有相关UI /// 因为UI设计导致的臃肿代码 /// public void CheckHighUI(Transform trans, bool isOn) { Transform target = trans.Find("High"); if (target != null) { target.gameObject.SetActive(isOn); } } private void OnModuleQuithandler(OnModuleQuit quit) { Hide(); } //private void OnUIDrawClose() //{ // StringEventSystem.Global.UnRegister($"On{UIDraw.Name}Close", OnUIDrawClose); // HideSelf(false); //} //private void OnUIBody3DMenuTreeClose() //{ // StringEventSystem.Global.UnRegister($"On{UIBody3DMenuTree.Name}Close", OnUIBody3DMenuTreeClose); // HideSelf(false); //} public void HideSelf(bool isTrue) { if (isTrue) { HideAll.GetComponent().DORestart(); } else { HideAll.GetComponent().DOPlayBackwards(); } HideAll.isOn = isTrue; CloseBtn.gameObject.SetActive(!isTrue); } protected override void OnOpen(IUIData uiData = null) { if (uiData != null) { mData = uiData as UIBody3DData ?? new UIBody3DData(); } BodyContent.RemoveAllChildren(); root = Utility.FindObj(mData.body.Path); root.SetActive(true); bodyList.Clear(); TitleName.text = Global.Instance.curModule.ModuleName; foreach (var bodyData in mData.body.subBody) { if (bodyData.Value.isBodyList == true) { bodyList.Add(bodyData.Value.Name, bodyData.Value); } else { var body = bodyData.Value; var bodyItem = GameObject.Instantiate(BodyItem.gameObject, BodyContent); bodyItem.transform.Find("Content/Btns").gameObject.SetActive(false); bodyItem.transform.Find("Content/Label").GetComponent().text = body.Name; var bodyToggle = bodyItem.GetComponent(); bodyToggle.isOn = body.isShow; GameObject obj = Utility.FindObj(body.Path); obj.SetActive(body.isShow); bodyToggle.onValueChanged.AddListener(isOn => { obj.SetActive(isOn); }); var iconImg = BodyItem.transform.Find("Icon").GetComponent(); LoadIcon(body.Icon, body, iconImg); } } BodyList.RemoveAllChildren(); bodyListIndex.Clear(); foreach (var list in bodyList) { var body = list.Value; //var add = GameObject.Instantiate(BodyBtn.gameObject, BodyList); //var sub = GameObject.Instantiate(BodyBtn.gameObject, BodyList); //add.transform.Find("Label").GetComponent().text = body.Name + "+"; //sub.transform.Find("Label").GetComponent().text = body.Name + "-"; //sub.name = add.name = body.Name; bodyListIndex.Add(body.Name, new BodyListItem() { index = 0, root = body }); bodyListIndex[body.Name].bodys.Clear(); foreach (var subList in body.subBody) { bodyListIndex[body.Name].bodys.Add(subList.Value.Name); Utility.FindObj(subList.Value.Path)?.SetActive(false); } var bodyItem = GameObject.Instantiate(BodyItem.gameObject, BodyContent); bodyItem.transform.Find("Content/Label").GetComponent().text = body.Name; bodyItem.GetComponent().interactable = false; var add = bodyItem.transform.Find("Content/Btns/Add").gameObject; var sub = bodyItem.transform.Find("Content/Btns/Sub").gameObject; sub.name = add.name = body.Name; add.GetComponent