FguiTree.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using FairyGUI;
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using XGame.Framework.UI;
  6. namespace XGame.Framework.FGUI
  7. {
  8. public class FguiTree : MonoBehaviour, IUITree, IDisposable
  9. {
  10. private Dictionary<UILayer, GComponent> _layersMap;
  11. private void Start()
  12. {
  13. _layersMap = new Dictionary<UILayer, GComponent>();
  14. Init();
  15. }
  16. private void Init()
  17. {
  18. var size = CalculateResolution();
  19. Log.Debug($"Screen Width:{Screen.width} Height:{Screen.height} orientation:{Screen.orientation} Size:{size}");
  20. GRoot.inst.SetContentScaleFactor(size.x, size.y, UIContentScaler.ScreenMatchMode.MatchWidthOrHeight); //(1136, 640);
  21. // 屏蔽FGUI自动卸载bundle
  22. UIPackage.unloadBundleByFGUI = false;
  23. // 设置UILayer节点
  24. var layerVals = Enum.GetValues(typeof(UILayer));
  25. foreach (var layerVal in layerVals)
  26. {
  27. UILayer layer = (UILayer)layerVal;
  28. //go.layer = LayerMask.NameToLayer("UI");
  29. var component = new GComponent();
  30. component.name = layer.ToString();
  31. component.gameObjectName = layer.ToString();
  32. component.rootContainer._panelOrder = (int)layer * 100;
  33. component.rootContainer.fairyBatching = true;
  34. GRoot.inst.AddChild(component);
  35. _layersMap.Add(layer, component);
  36. }
  37. }
  38. private Vector2Int CalculateResolution()
  39. {
  40. var size = new Vector2Int(756, 1344);
  41. if (Screen.orientation == ScreenOrientation.Portrait)
  42. { // 竖屏
  43. size.y = (int)(Screen.height * size.x * 1f / Screen.width);
  44. }
  45. else
  46. { // 横屏
  47. size.x = (int)(Screen.width * size.y * 1f / Screen.height);
  48. }
  49. return size;
  50. }
  51. #region IUITree 接口实现
  52. Camera IUITree.Camera => StageCamera.main;
  53. Canvas IUITree.GetCanvas(UILayer layer)
  54. {
  55. /// TODO FGUI不需要Canvas
  56. throw new System.NotImplementedException();
  57. }
  58. void IUITree.AddPanel(IUIPanel panel)
  59. {
  60. var parent = _layersMap[panel.Layer];
  61. var fPanel = (panel as FguiPanel).Panel;
  62. parent.AddChild(fPanel);
  63. //全屏UI自适应
  64. fPanel.MakeFullScreen();
  65. //fPanel.Center();
  66. }
  67. void IUITree.RemovePanel(IUIPanel panel)
  68. {
  69. var parent = _layersMap[panel.Layer];
  70. var gComponent = (panel as FguiPanel).Panel;
  71. parent.RemoveChild(gComponent);
  72. // 关闭但未销毁的UI,父节点改为UITree
  73. #if UNITY_EDITOR
  74. gComponent.displayObject.gameObject.transform.SetParent(transform, false);
  75. #endif
  76. }
  77. void IUITree.SetAsLastSibling(IUIPanel panel)
  78. {
  79. var parent = _layersMap[panel.Layer];
  80. var fPanel = (panel as FguiPanel).Panel;
  81. parent.AddChild(fPanel);
  82. //全屏UI自适应
  83. fPanel.MakeFullScreen();
  84. }
  85. void IDisposable.Dispose()
  86. {
  87. GameObject.Destroy(gameObject);
  88. }
  89. #endregion
  90. }
  91. }