| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- namespace ProjectBase.UI
- {
- public class BaseUIView : MonoBehaviour
- {
- public SOUIViewConfig uiViewConfig;
- [HideInInspector]
- public UIViewLayerController viewLayerController;
- private UIViewState m_ViewState;
- protected Canvas canvas;
- private bool refreshed = false;
- public UIViewState ViewState
- {
- get
- {
- return m_ViewState;
- }
- set
- {
- m_ViewState = value;
- #if UNITY_EDITOR
- name = string.Format("{0}{1}", uiViewConfig.viewName, System.Enum.GetName(typeof(UIViewState), ViewState));
- #endif
- }
- }
- public int ViewSortOrder
- {
- get
- {
- return canvas.sortingOrder;
- }
- set
- {
- canvas.overrideSorting = true;
- canvas.sortingOrder = value;
- }
- }
- #region Unity
- #endregion
- public void Init()
- {
- InitCanvas();
- InitUIObjects();
- }
- private void InitCanvas()
- {
- canvas = GetComponent<Canvas>();
- if (canvas == null)
- {
- canvas = gameObject.AddComponent<Canvas>();
- }
- GraphicRaycaster caster = GetComponent<GraphicRaycaster>();
- if (caster == null)
- {
- gameObject.AddComponent<GraphicRaycaster>();
- }
- }
- protected virtual void InitUIObjects() { }
- public virtual void OnPush()
- {
- ViewState = UIViewState.Nonvisible;
- UpdateSortLayer();
- }
- public void OnShow()
- {
- if (ViewState != UIViewState.Visible)
- {
- if (!gameObject.activeSelf)
- {
- gameObject.SetActive(true);
- }
- Vector3 pos = transform.localPosition;
- pos.z = 0;
- transform.localPosition = pos;
- OnShowCallBack();
- ViewState = UIViewState.Visible;
- if (refreshed)
- {
- UpdateView();
- }
- }
- }
- protected virtual void OnShowCallBack() { }
- public void OnHide()
- {
- if (ViewState != UIViewState.Nonvisible)
- {
- Vector3 pos = transform.localPosition;
- pos.z = int.MaxValue;
- transform.localPosition = pos;
- OnHideCallBack();
- ViewState = UIViewState.Nonvisible;
- }
- }
- protected virtual void OnHideCallBack() { }
- public virtual void OnPopup()
- {
- if (ViewState == UIViewState.Cache)
- {
- return;
- }
- if (ViewState == UIViewState.Visible)
- {
- OnHide();
- }
- ViewState = UIViewState.Cache;
- }
- public virtual void OnExit()
- {
- if (ViewState != UIViewState.Cache)
- {
- OnPopup();
- }
- }
- public void SetArguments(params object[] args)
- {
- refreshed = true;
- UpdateArguments(args);
- if (uiViewConfig.alwaysUpdate || ViewState == UIViewState.Visible)
- {
- UpdateView();
- }
- }
- protected virtual void UpdateArguments(params object[] args) { }
- public void UpdateView()
- {
- refreshed = false;
- UpdateViewCallBack();
- }
- protected virtual void UpdateViewCallBack() { }
- private void UpdateSortLayer()
- {
- canvas.overrideSorting = true;
- switch (uiViewConfig.viewLayer)
- {
- case UIViewLayer.BackGround:
- canvas.sortingLayerID = SortingLayer.NameToID("BackGround");
- break;
- case UIViewLayer.Base:
- canvas.sortingLayerID = SortingLayer.NameToID("Base");
- break;
- case UIViewLayer.Popup:
- canvas.sortingLayerID = SortingLayer.NameToID("Popup");
- break;
- case UIViewLayer.Top:
- canvas.sortingLayerID = SortingLayer.NameToID("Top");
- break;
- case UIViewLayer.Debug:
- canvas.sortingLayerID = SortingLayer.NameToID("Debug");
- break;
- default:
- break;
- }
- }
- public void ResetUIView()
- {
- ViewSortOrder = 0;
- viewLayerController = null;
- }
- }
- }
|