123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using FairyGUI;
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using XGame.Framework.UI;
- namespace XGame.Framework.FGUI
- {
- public class FguiTree : MonoBehaviour, IUITree, IDisposable
- {
- private Dictionary<UILayer, GComponent> _layersMap;
- private void Start()
- {
- _layersMap = new Dictionary<UILayer, GComponent>();
- Init();
- }
- private void Init()
- {
- var size = CalculateResolution();
- Log.Debug($"Screen Width:{Screen.width} Height:{Screen.height} orientation:{Screen.orientation} Size:{size}");
- GRoot.inst.SetContentScaleFactor(size.x, size.y, UIContentScaler.ScreenMatchMode.MatchWidthOrHeight); //(1136, 640);
- // 屏蔽FGUI自动卸载bundle
- UIPackage.unloadBundleByFGUI = false;
- // 设置UILayer节点
- var layerVals = Enum.GetValues(typeof(UILayer));
- foreach (var layerVal in layerVals)
- {
- UILayer layer = (UILayer)layerVal;
- //go.layer = LayerMask.NameToLayer("UI");
- var component = new GComponent();
- component.name = layer.ToString();
- component.gameObjectName = layer.ToString();
- component.rootContainer._panelOrder = (int)layer * 100;
- component.rootContainer.fairyBatching = true;
- GRoot.inst.AddChild(component);
- _layersMap.Add(layer, component);
- }
- }
- private Vector2Int CalculateResolution()
- {
- var size = new Vector2Int(756, 1344);
- if (Screen.orientation == ScreenOrientation.Portrait)
- { // 竖屏
- size.y = (int)(Screen.height * size.x * 1f / Screen.width);
- }
- else
- { // 横屏
- size.x = (int)(Screen.width * size.y * 1f / Screen.height);
- }
- return size;
- }
- #region IUITree 接口实现
- Camera IUITree.Camera => StageCamera.main;
- Canvas IUITree.GetCanvas(UILayer layer)
- {
- /// TODO FGUI不需要Canvas
- throw new System.NotImplementedException();
- }
- void IUITree.AddPanel(IUIPanel panel)
- {
- var parent = _layersMap[panel.Layer];
- var fPanel = (panel as FguiPanel).Panel;
- parent.AddChild(fPanel);
- //全屏UI自适应
- fPanel.MakeFullScreen();
- //fPanel.Center();
- }
- void IUITree.RemovePanel(IUIPanel panel)
- {
- var parent = _layersMap[panel.Layer];
- var gComponent = (panel as FguiPanel).Panel;
- parent.RemoveChild(gComponent);
- // 关闭但未销毁的UI,父节点改为UITree
- #if UNITY_EDITOR
- gComponent.displayObject.gameObject.transform.SetParent(transform, false);
- #endif
- }
- void IUITree.SetAsLastSibling(IUIPanel panel)
- {
- var parent = _layersMap[panel.Layer];
- var fPanel = (panel as FguiPanel).Panel;
- parent.AddChild(fPanel);
- //全屏UI自适应
- fPanel.MakeFullScreen();
- }
- void IDisposable.Dispose()
- {
- GameObject.Destroy(gameObject);
- }
- #endregion
- }
- }
|