VirtualFramework/Assets/Scripts/UI/UIBody3DMenuTree.cs

126 lines
4.4 KiB
C#
Raw Normal View History

2025-02-17 17:03:48 +08:00
using UnityEngine;
using UnityEngine.UI;
using XMLTool;
using System.Collections.Generic;
namespace QFramework.Example
{
public class UIBody3DMenuTreeData : UIPanelData
{
public Body3D.Body body;
}
public partial class UIBody3DMenuTree : UIPanel
{
// <20><><EFBFBD>ڴ洢 Body3D.Body <20>Ͷ<EFBFBD>Ӧ<EFBFBD><D3A6> Toggle <20><>ӳ<EFBFBD><D3B3><EFBFBD><EFBFBD>ϵ
protected override void OnInit(IUIData uiData = null)
{
// please add init code here
Close.onClick.AddListener(() =>
{
Hide();
UIKit.OpenPanelAsync<UIBody3D>().ToAction().StartGlobal();
});
}
protected override void OnOpen(IUIData uiData = null)
{
mData = uiData as UIBody3DMenuTreeData ?? new UIBody3DMenuTreeData();
Content.RemoveAllChildren();
BuildTreeUI(mData.body);
}
private void BuildTreeUI(Body3D.Body data, int depth = 0, Transform parent = null)
{
foreach (var bodyPair in data.subBody)
{
Body3D.Body body = bodyPair.Value;
// ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Transform targetParent = parent != null ? parent : Content;
// <20><><EFBFBD><EFBFBD>Toggle
GameObject toggleObj = GameObject.Instantiate(Item.gameObject, targetParent);
toggleObj.name = body.Name;
Toggle uiToggle = toggleObj.transform.Find("ToggleContent/UI").GetComponent<Toggle>();
Toggle objToggle = toggleObj.transform.Find("ToggleContent/Obj").GetComponent<Toggle>();
Text label = toggleObj.transform.Find("ToggleContent/Label").GetComponentInChildren<Text>();
label.text = body.Name; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
toggleObj.transform.Find("ToggleContent").GetComponent<HorizontalLayoutGroup>().padding = new RectOffset(depth * 15, 5, 2, 2);
if (depth != 0)
{
toggleObj.SetActive(false);
}
// <20>ݹ鹹<DDB9><E9B9B9><EFBFBD>ӽڵ<D3BD>
Transform subContent = toggleObj.transform.Find("SubContent");
// <20><><EFBFBD><EFBFBD>Toggle<6C>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>¼<EFBFBD>
uiToggle.onValueChanged.AddListener((isOn) =>
{
for (int i = 0; i < subContent.childCount; i++)
{
subContent.GetChild(i).gameObject.SetActive(isOn);
}
});
// ΪobjToggle<6C><65><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߼<EFBFBD>
objToggle.onValueChanged.AddListener((isOn) =>
{
SetObjectVisibility(body, isOn, subContent);
});
if (body.subBody.Count > 0)
{
uiToggle.gameObject.SetActive(true);
BuildTreeUI(body, depth + 1, subContent);
//HideSubBody(body); // <20><>ʼʱ<CABC><CAB1><EFBFBD><EFBFBD><EFBFBD>ӽڵ<D3BD>
}
else
{
uiToggle.gameObject.SetActive(false);
Debug.Log($"<22>ڵ<EFBFBD> {body.Name} û<><C3BB><EFBFBD>ӽڵ㡣");
}
}
}
// <20><><EFBFBD><EFBFBD>body<64><79>subbody<64>е<EFBFBD>path<74><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬʱ<CDAC><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>UI<55>е<EFBFBD>objToggle״̬
private void SetObjectVisibility(Body3D.Body body, bool isOn, Transform subContent)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰbody<64><79>path<74><68>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GameObject targetObj = Utility.FindObj(body.Path);
if (targetObj != null)
{
targetObj.SetActive(isOn);
}
// <20>ݹ鴦<DDB9><E9B4A6>subbody<64>е<EFBFBD>path<74><68>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E5A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>UI<55>е<EFBFBD>objToggle״̬
int index = 0;
foreach (var subBodyPair in body.subBody)
{
Body3D.Body subBody = subBodyPair.Value;
if (subContent.childCount > index)
{
Transform childToggleObj = subContent.GetChild(index);
Toggle childObjToggle = childToggleObj.transform.Find("ToggleContent/Obj").GetComponent<Toggle>();
childObjToggle.isOn = isOn; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>UI<55>е<EFBFBD>objToggle״̬
Transform childSubContent = childToggleObj.transform.Find("SubContent");
SetObjectVisibility(subBody, isOn, childSubContent);
}
index++;
}
}
protected override void OnShow()
{
}
protected override void OnHide()
{
}
protected override void OnClose()
{
}
}
}