每个UI面板都需要管理,以前有个UImanager统一管理,现在说一下,每个面板需要怎么管理,以HeroPanel为例
所有的点击事件统一放在一个类中,所有点击事件通过传入按钮名字控制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HeroPanel : BasePanel {
[SerializeField]
private HeroItem heroItem;
[SerializeField]
private ToggleGroup toggleGroup;
[SerializeField]
private RawImage heroImage = null;
private GameObject boat;
private List<HeroItem> _heroItems = new List<HeroItem>();
private void Awake()
{
boat = FindObjectOfType<BoatManager>().gameObject;
}
//传入按钮名字
public void OnBtnListener(GameObject btn) {
switch (btn.name)
{
case "BackBtn":
heroImage.enabled = false;
MusicMgr.Instance.PlayEffect(MusicType.Feeds.PutFeed);
//boat.transform.position = new Vector3(-489.3f, -502.35f, -483.5f);
UIManager.GetInstance().PopPanel();
break;
case "nvjing":
break;
default:
break;
}
}
public override void OnEnter()
{
base.OnEnter();
heroImage.enabled = true;
for (int i = 0; i < Config.Instance.heroInfos.Count; i++)
{
if (i >= _heroItems.Count)
{
HeroItem item = Instantiate(heroItem.gameObject).GetComponent<HeroItem>();
item.transform.SetParent(heroItem.transform.parent);
item.transform.localScale = Vector3.one;
item.gameObject.SetActive(true);
toggleGroup.RegisterToggle(item.toggle);
item.OnShow(Config.Instance.heroInfos[i]);
_heroItems.Add(item);
if (Config.Instance.heroInfos[i].id == UserData.Instance.selectHero)
{
item.toggle.isOn = true;
}
}
else {
_heroItems[i].OnShow(Config.Instance.heroInfos[i]);
}
}
}
public void OnRefresh() {
foreach (var item in _heroItems)
{
item.OnRefresh();
}
}
public override void OnExit()
{
base.OnExit();
}
}