FguiTree.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. parent.RemoveChild((panel as FguiPanel).Panel);
  71. }
  72. void IUITree.SetAsLastSibling(IUIPanel panel)
  73. {
  74. var parent = _layersMap[panel.Layer];
  75. var fPanel = (panel as FguiPanel).Panel;
  76. parent.AddChild(fPanel);
  77. //全屏UI自适应
  78. fPanel.MakeFullScreen();
  79. }
  80. void IDisposable.Dispose()
  81. {
  82. GameObject.Destroy(gameObject);
  83. }
  84. #endregion
  85. }
  86. }