217 lines
8.1 KiB
C#
217 lines
8.1 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using XMLTool;
|
||
using System.Collections.Generic;
|
||
using TMPro;
|
||
|
||
namespace QFramework.Example
|
||
{
|
||
public class UIBody3DMenuTreeData : UIPanelData
|
||
{
|
||
public Body3D.Body body;
|
||
}
|
||
public partial class UIBody3DMenuTree : UIPanel
|
||
{
|
||
|
||
|
||
protected override void OnInit(IUIData uiData = null)
|
||
{
|
||
// please add init code here
|
||
Close.onClick.AddListener(() =>
|
||
{
|
||
Hide();
|
||
UIKit.OpenPanelAsync<UIBody3D>().ToAction().StartGlobal();
|
||
});
|
||
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);
|
||
}
|
||
});
|
||
}
|
||
|
||
|
||
|
||
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;
|
||
|
||
// È·¶¨¸¸ÈÝÆ÷
|
||
Transform targetParent = parent != null ? parent : Content;
|
||
|
||
// ´´½¨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>();
|
||
TextMeshProUGUI label = toggleObj.transform.Find("ToggleContent/Label").GetComponentInChildren<TextMeshProUGUI>();
|
||
label.text = body.Name; // ¸ù¾ÝÉî¶ÈÌí¼ÓËõ½ø
|
||
toggleObj.transform.Find("ToggleContent").GetComponent<HorizontalLayoutGroup>().padding = new RectOffset(depth * 15, 5, 2, 2);
|
||
if (depth != 0)
|
||
{
|
||
toggleObj.SetActive(false);
|
||
}
|
||
|
||
// µÝ¹é¹¹½¨×Ó½Úµã
|
||
Transform subContent = toggleObj.transform.Find("SubContent");
|
||
|
||
// ÉèÖÃToggleµÄ¼àÌýʼþ
|
||
uiToggle.onValueChanged.AddListener((isOn) =>
|
||
{
|
||
for (int i = 0; i < subContent.childCount; i++)
|
||
{
|
||
subContent.GetChild(i).gameObject.SetActive(isOn);
|
||
}
|
||
});
|
||
|
||
// ΪobjToggleÌí¼Óµã»÷ʼþ´¦ÀíÂß¼
|
||
objToggle.onValueChanged.AddListener((isOn) =>
|
||
{
|
||
SetObjectVisibility(body, isOn, subContent);
|
||
});
|
||
|
||
if (body.subBody.Count > 0)
|
||
{
|
||
uiToggle.gameObject.SetActive(true);
|
||
BuildTreeUI(body, depth + 1, subContent);
|
||
//HideSubBody(body); // ³õʼʱÒþ²Ø×Ó½Úµã
|
||
}
|
||
else
|
||
{
|
||
uiToggle.gameObject.SetActive(false);
|
||
Debug.Log($"½Úµã {body.Name} ûÓÐ×ӽڵ㡣");
|
||
}
|
||
}
|
||
}
|
||
|
||
// ¸ù¾ÝbodyºÍsubbodyÖеÄpathÉèÖÃÎïÌåµÄÏÔÒþ£¬Í¬Ê±ÉèÖÃ×ÓUIÖеÄobjToggle״̬
|
||
private void SetObjectVisibility(Body3D.Body body, bool isOn, Transform subContent)
|
||
{
|
||
// ´¦Àíµ±Ç°bodyµÄpath¶ÔÓ¦µÄÎïÌå
|
||
GameObject targetObj = Utility.FindObj(body.Path);
|
||
if (targetObj != null)
|
||
{
|
||
targetObj.SetActive(isOn);
|
||
}
|
||
|
||
// µÝ¹é´¦ÀísubbodyÖеÄpath¶ÔÓ¦µÄÎïÌ壬²¢ÉèÖÃ×ÓUIÖеÄ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; // ÉèÖÃ×ÓUIÖеÄobjToggle״̬
|
||
Transform childSubContent = childToggleObj.transform.Find("SubContent");
|
||
SetObjectVisibility(subBody, isOn, childSubContent);
|
||
}
|
||
index++;
|
||
}
|
||
}
|
||
|
||
// ʵÏÖRefreshSearchContentº¯Êý
|
||
private void RefreshSearchContent(string str)
|
||
{
|
||
// Çå¿ÕSearchContentϵÄËùÓÐ×ÓÎïÌå
|
||
SearchContent.RemoveAllChildren();
|
||
|
||
// µÝ¹é¼ì²é×ÓÎïÌåµÄ×ÓÎïÌå
|
||
CheckChildren(Content, str);
|
||
|
||
}
|
||
|
||
// µÝ¹é¼ì²é×ÓÎïÌåµÄ×ÓÎïÌå
|
||
private void CheckChildren(Transform parent, string str)
|
||
{
|
||
// ±éÀúµ±Ç°¸¸ÎïÌåϵÄËùÓÐ×ÓÎïÌå
|
||
for (int i = 0; i < parent.childCount; i++)
|
||
{
|
||
Transform child = parent.GetChild(i);
|
||
// ³¢ÊÔ²éÕÒ ToggleContent/Label ×é¼þ
|
||
Transform labelTransform = child.Find("ToggleContent/Label");
|
||
if (labelTransform != null)
|
||
{
|
||
TextMeshProUGUI textComponent = labelTransform.GetComponent<TextMeshProUGUI>();
|
||
if (textComponent != null)
|
||
{
|
||
string name = textComponent.text;
|
||
// ¼ì²é×ÓÎïÌåµÄÃû×ÖÊÇ·ñ°üº¬ËÑË÷×Ö·û´®
|
||
if (name.Contains(str))
|
||
{
|
||
// ¸´ÖƸÃ×ÓÎïÌåµ½ SearchContent ÖÐ
|
||
GameObject clone = GameObject.Instantiate(SearchItem.gameObject, SearchContent);
|
||
Transform subContent = child.Find("SubContent");
|
||
// ¼ì²éÊÇ·ñÓÐ×ÓÄÚÈÝ
|
||
if (subContent.childCount > 0)
|
||
{
|
||
Transform buttonLabel = clone.transform.Find("Button/Label");
|
||
if (buttonLabel != null)
|
||
{
|
||
TextMeshProUGUI buttonText = buttonLabel.GetComponent<TextMeshProUGUI>();
|
||
if (buttonText != null)
|
||
{
|
||
buttonText.text = "£¨×飩" + name;
|
||
}
|
||
}
|
||
// µÝ¹é¼ì²éµ±Ç°×ÓÎïÌåµÄ×ÓÎïÌå
|
||
CheckChildren(subContent, str);
|
||
}
|
||
else
|
||
{
|
||
Transform buttonLabel = clone.transform.Find("Button/Label");
|
||
if (buttonLabel != null)
|
||
{
|
||
TextMeshProUGUI buttonText = buttonLabel.GetComponent<TextMeshProUGUI>();
|
||
if (buttonText != null)
|
||
{
|
||
buttonText.text = name;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
protected override void OnShow()
|
||
{
|
||
}
|
||
|
||
protected override void OnHide()
|
||
{
|
||
}
|
||
|
||
protected override void OnClose()
|
||
{
|
||
}
|
||
}
|
||
} |