using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering; using UnityEngine.UI; namespace SkierFramework { public class UIView : MonoBehaviour, IBindableUI { private UIViewController _controller; private GameObject _lastSelect; private Canvas _canvas; public GameObject DefaultSelect; public UIViewController Controller => _controller; public virtual void OnInit(UIControlData uIControlData, UIViewController controller) { if (uIControlData != null) { uIControlData.BindDataTo(this); } _controller = controller; _canvas = gameObject.GetOrAddComponent(); gameObject.GetOrAddComponent(); gameObject.GetOrAddComponent(); } /// /// 事件监听 /// public virtual void OnAddListener() { } /// /// 事件移除 /// public virtual void OnRemoveListener() { } /// /// 打开 /// public virtual void OnOpen(object userData) { SortOrder(); _canvas.overrideSorting = true; _canvas.sortingOrder = _controller.order; OnAddListener(); _lastSelect = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject; } protected virtual void SortOrder() { SortOrder(transform, _controller.order + 1); } /// /// 递归的将所有孩子层级设置正确:一些默认摆在UI上的特效等正确分配层级 /// protected int SortOrder(Transform target, int order) { var canvas = target.GetComponent(); if (canvas != null && canvas != _canvas) { canvas.overrideSorting = true; canvas.sortingOrder = order++; canvas.gameObject.layer = Layer.UI; } var psr = target.GetComponent(); if (psr != null) { psr.sortingOrder = order++; psr.gameObject.layer = Layer.UI; } var sortGroup = target.GetComponent(); if (sortGroup != null) { sortGroup.sortingOrder = order++; sortGroup.gameObject.SetLayerRecursively(Layer.UI); } var sr = target.GetComponent(); if (sr != null) { sr.sortingOrder = order++; sr.gameObject.layer = Layer.UI; } for (int i = 0; i < target.childCount; i++) { order = SortOrder(target.GetChild(i), order); } return order; } /// /// 恢复 /// public virtual void OnResume() { if (DefaultSelect != null) { UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject(DefaultSelect); } } /// /// 被覆盖 /// public virtual void OnPause() { } /// /// 被关闭 /// public virtual void OnClose() { OnRemoveListener(); if (_lastSelect != null && _lastSelect.activeInHierarchy) { UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject(_lastSelect); } } /// /// 取消按钮响应 /// public virtual void OnCancel() { UIManager.Instance.Close(this); } /// /// 被卸载释放 /// public virtual void OnRelease() { } } }