123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- using System.Collections.Generic;
- using UnityEngine;
- namespace XGame.Framework.UI
- {
- public class UILayerBehaviour : MonoBehaviour
- {
- [SerializeField]
- private UILayer _layer;
- [SerializeField]
- private Canvas _uiCanvas;
- public Canvas UiCanvas => _uiCanvas;
- public Transform UIRoot => _uiCanvas.transform;
- private List<IUIPanel> _panels = new List<IUIPanel>();
- public UILayer Layer {
- get => _layer;
- set
- {
- _layer = value;
- _uiCanvas.sortingOrder = (byte)value * 100;
- gameObject.name = value.ToString();
- }
- }
- /// <summary>
- /// 加入子节点
- /// 将Transform移动到本地变换列表的末尾
- /// </summary>
- /// <param name="child"></param>
- public void AddPanel(IUIPanel panel)
- {
- var panelTr = (panel as UIPanel).transform;
- if (!_panels.Contains(panel))
- {
- panelTr.SetParent(UIRoot, false);
- panelTr.position = Vector3.zero;
- _panels.Add(panel);
- OnChildrenChanged();
- }
- //将Transform移动到本地变换列表的末尾
- panelTr.SetAsLastSibling();
- }
- /// <summary>
- /// 移除子节点
- /// </summary>
- /// <param name="child"></param>
- public void RemovePanel(IUIPanel panel)
- {
- if (!_panels.Contains(panel))
- {
- return;
- }
- _panels.Remove(panel);
- (panel as UIPanel).transform.SetParent(null, false);
- OnChildrenChanged();
- }
- private void OnChildrenChanged()
- {
- _uiCanvas.gameObject.SetActive(_uiCanvas.transform.childCount > 0);
- }
- }
- }
|