UITree.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace XGame.Framework.UI
  5. {
  6. /// <summary>
  7. /// UGUI的实现
  8. /// </summary>
  9. public class UITree : MonoBehaviour, IUITree, IDisposable
  10. {
  11. [SerializeField]
  12. private UILayerBehaviour _sample;
  13. [SerializeField]
  14. private Camera _uiCamera;
  15. private Dictionary<UILayer, UILayerBehaviour> _layerBehaviourMap = new Dictionary<UILayer, UILayerBehaviour>();
  16. private void Start()
  17. {
  18. InitLayers();
  19. }
  20. private void InitLayers()
  21. {
  22. Assert.IsNotNull(_sample);
  23. var layerVals = Enum.GetValues(typeof(UILayer));
  24. for (int i = 0; i < layerVals.Length; i++)
  25. {
  26. var layer = (UILayer)layerVals.GetValue(i);
  27. var behaviour = Instantiate(_sample);
  28. behaviour.transform.SetParent(transform, false);
  29. behaviour.Layer = layer;
  30. behaviour.gameObject.SetActive(true);
  31. _layerBehaviourMap[layer] = behaviour;
  32. }
  33. }
  34. #region 接口实现
  35. public Camera Camera => _uiCamera;
  36. public Canvas GetCanvas(UILayer layer)
  37. {
  38. var layerBehaviour = _layerBehaviourMap[layer];
  39. return layerBehaviour.UiCanvas;
  40. }
  41. public void AddPanel(IUIPanel panel)
  42. {
  43. var layerBehaviour = _layerBehaviourMap[panel.Layer];
  44. layerBehaviour.AddPanel(panel);
  45. }
  46. public void SetAsLastSibling(IUIPanel panel)
  47. {
  48. (panel as UIPanel).transform.SetAsLastSibling();
  49. }
  50. public void RemovePanel(IUIPanel panel)
  51. {
  52. var layerBehaviour = _layerBehaviourMap[panel.Layer];
  53. layerBehaviour.RemovePanel(panel);
  54. }
  55. void IDisposable.Dispose()
  56. {
  57. GameObject.Destroy(gameObject);
  58. }
  59. #endregion
  60. }
  61. }