using UnityEngine; using UnityEngine.UI; using QFramework; using TMPro; using System.Collections.Generic; using System; namespace QFramework.Example { public class UIKnowledgeData : UIPanelData { public XMLTool.Knowledge knowledge; } public partial class UIKnowledge : UIPanel { ResLoader loader; Dictionary sprites = new Dictionary(); IAction curAction; protected override void OnInit(IUIData uiData = null) { mData = uiData as UIKnowledgeData ?? new UIKnowledgeData(); // please add init code here loader = ResLoader.Allocate(); CloseBtn.onClick.AddListener(() => { Hide(); }); } protected override void OnOpen(IUIData uiData = null) { if (Global.Instance.curModule.knowledge != null && Global.Instance.curModule.knowledge.items != null) { LeftContent.RemoveAllChildren(); string bg = Global.Instance.curModule.knowledge.bgImage; LoadBgImage(bg, () => { SetRightContentPos(""); RefreshComponents(Global.Instance.curModule.knowledge.components, GlobalComs); }); foreach (var item in Global.Instance.curModule.knowledge.items) { ItemFactory(item); } } } public void ItemFactory(XMLTool.Knowledge.Item itemData, Transform parent = null) { Transform content = parent == null ? LeftContent : parent; GameObject leftObj = GameObject.Instantiate(LeftItem.gameObject, content); Transform subContent = leftObj.transform.Find("SubContent"); Toggle toggle = leftObj.transform.Find("Toggle").GetComponent(); TextMeshProUGUI label = toggle.transform.Find("Name").GetComponent(); label.text = itemData.title; toggle.onValueChanged.AddListener(isOn => { if (isOn) { LoadBgImage(itemData.bgImage, () => { SetRightContentPos(itemData.setPos); }); ItemComs.RemoveAllChildren(); RefreshComponents(itemData.components, ItemComs); } if (subContent.childCount > 0) { subContent.gameObject.SetActive(isOn); if (isOn) { subContent.GetChild(0).GetComponentInChildren().isOn = true; } } else { label.color = isOn == true ? Color.black : Color.white; } }); if (itemData.subs != null && itemData.subs.Count > 0) { foreach (var sub in itemData.subs) { ItemFactory(sub, subContent); } } else { toggle.group = LeftContent.GetComponent(); } } /// /// 刷新当前页面的挂件 /// public void RefreshComponents(List coms, Transform content) { if (coms != null && coms.Count > 0) { foreach (var ComData in coms) { switch (ComData.type) { case "Button": GameObject btn = GameObject.Instantiate(BtnPrefab.gameObject, content); btn.GetComponent().sizeDelta = Utility.GetVector2FromStrArray(ComData.size); btn.transform.localPosition = Utility.GetVector2FromStrArray(ComData.pos); btn.GetComponent