VirtualFramework/Assets/Scripts/UI/UIBody3DMenuTree.cs

196 lines
6.9 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;
2025-02-17 17:32:59 +08:00
using TMPro;
2025-02-17 17:03:48 +08:00
namespace QFramework.Example
{
public class UIBody3DMenuTreeData : UIPanelData
{
public Body3D.Body body;
}
public partial class UIBody3DMenuTree : UIPanel
{
2025-02-17 17:32:59 +08:00
2025-02-17 17:03:48 +08:00
protected override void OnInit(IUIData uiData = null)
{
// please add init code here
Close.onClick.AddListener(() =>
{
Hide();
UIKit.OpenPanelAsync<UIBody3D>().ToAction().StartGlobal();
});
2025-02-17 17:32:59 +08:00
Input.onSelect.AddListener(str =>
{
Debug.LogError("onSelect:" + str);
SearchContent.gameObject.SetActive(true);
Content.gameObject.SetActive(false);
});
Input.onEndEdit.AddListener(str =>
{
Debug.LogError("onEndEdit:" + str);
if (string.IsNullOrEmpty(str))
{
SearchContent.gameObject.SetActive(false);
Content.gameObject.SetActive(true);
SearchContent.RemoveAllChildren();
}
else
{
SearchContent.gameObject.SetActive(true);
Content.gameObject.SetActive(false);
RefreshSearchContent(str);
}
});
2025-02-17 17:03:48 +08:00
}
2025-02-17 17:32:59 +08:00
2025-02-17 17:03:48 +08:00
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>();
2025-02-17 17:32:59 +08:00
TextMeshProUGUI label = toggleObj.transform.Find("ToggleContent/Label").GetComponentInChildren<TextMeshProUGUI>();
2025-02-17 17:03:48 +08:00
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++;
}
}
2025-02-17 17:32:59 +08:00
// ʵ<><CAB5>RefreshSearchContent<6E><74><EFBFBD><EFBFBD>
private void RefreshSearchContent(string str)
{
// <20><><EFBFBD><EFBFBD>SearchContent<6E>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SearchContent.RemoveAllChildren();
// <20><><EFBFBD><EFBFBD>Content<6E>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 0; i < Content.childCount; i++)
{
Transform child = Content.GetChild(i);
string name = child.Find("ToggleContent/Label").GetComponent<TextMeshProUGUI>().text;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
if (name.Contains(str))
{
// <20><><EFBFBD>Ƹ<EFBFBD><C6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>嵽SearchContent<6E><74>
GameObject clone = GameObject.Instantiate(SearchItem.gameObject, SearchContent);
if (child.Find("SubContent").childCount > 0)
{
clone.transform.Find("Button/Label").GetComponent<TextMeshProUGUI>().text = "<22><><EFBFBD>飩" + name;
}
else
{
clone.transform.Find("Button/Label").GetComponent<TextMeshProUGUI>().text = name;
}
}
// <20>ݹ<EFBFBD><DDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
CheckChildren(child, str);
}
}
// <20>ݹ<EFBFBD><DDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private void CheckChildren(Transform parent, string str)
{
for (int i = 0; i < parent.childCount; i++)
{
Transform child = parent.GetChild(i);
if (child.name.Contains(str))
{
GameObject clone = GameObject.Instantiate(child.gameObject, SearchContent);
}
CheckChildren(child, str);
}
}
2025-02-17 17:03:48 +08:00
protected override void OnShow()
{
}
protected override void OnHide()
{
}
protected override void OnClose()
{
}
}
}