123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- using System;
- using System.Collections;
- using UnityEngine;
- using UnityEngine.UI;
- namespace TapSDK.UI
- {
- /// <summary>
- /// base panel of TapSDK UI module
- /// </summary>
- [RequireComponent(typeof(Canvas))]
- [RequireComponent(typeof(CanvasGroup))]
- public abstract class BasePanelController : MonoBehaviour
- {
- /// <summary>
- /// the canvas related to this panel
- /// </summary>
- [HideInInspector]
- public Canvas canvas;
- /// <summary>
- /// the canvas group related to this panel
- /// </summary>
- [HideInInspector]
- public CanvasGroup canvasGroup;
- /// <summary>
- /// fade in/out time
- /// </summary>
- private const float FADE_TIME = 0.1f;
- /// <summary>
- /// animation elapse time
- /// </summary>
- private float _animationElapse;
-
- private Vector2 _screenSize;
- private Vector2 _cachedAnchorPos;
- private RectTransform _rectTransform;
- private Coroutine _animationCoroutine;
-
- /// <summary>
- /// open parameter
- /// </summary>
- protected IOpenPanelParameter openParam;
- /// <summary>
- /// sorting order of this panel
- /// </summary>
- public int openOrder;
- /// <summary>
- /// settings about this panel
- /// </summary>
- public BasePanelConfig panelConfig;
- /// <summary>
- /// the transform parent when created it would be attached to
- /// </summary>
- /// <value></value>
- public virtual Transform AttachedParent => UIManager.Instance.GetUIRootTransform();
- #region Load
- protected virtual void Awake()
- {
- canvas = GetComponent<Canvas>();
- canvasGroup = GetComponent<CanvasGroup>();
- _rectTransform = transform as RectTransform;
-
- _screenSize = new Vector2(Screen.width, Screen.height);
-
- #if UNITY_EDITOR
- if (canvas == null)
- {
- Debug.LogErrorFormat("[TapSDK UI] BasePanel Must Be Related To Canvas Component!");
- }
- #endif
- }
- /// <summary>
- /// bind ugui components for every panel
- /// </summary>
- protected virtual void BindComponents() {}
- /// <summary>
- /// create the prefab instance
- /// </summary>
- /// <param name="param"></param>
- public void OnLoaded(IOpenPanelParameter param = null)
- {
- openParam = param;
- // 寻找组件
- BindComponents();
- // 添加到控制层
- UIManager.Instance.AddUI(this);
- // 更新层级信息
- InitCanvasSetting();
- // 开始动画效果
- OnShowEffectStart();
- // 调用加载成功方法
- OnLoadSuccess();
- }
- private void InitCanvasSetting()
- {
- if (canvas.renderMode != RenderMode.ScreenSpaceOverlay)
- {
- var camera = UIManager.Instance.GetUICamera();
- if (camera != null)
- {
- canvas.worldCamera = camera;
- }
- }
- canvas.pixelPerfect = true;
- canvas.overrideSorting = true;
- }
- /// <summary>
- /// init panel logic here
- /// </summary>
- protected virtual void OnLoadSuccess()
- {
- }
- #endregion
- #region Animation
- protected virtual void OnShowEffectStart()
- {
- if (panelConfig.animationType == EAnimationMode.None)
- {
- return;
- }
- OnEffectStart();
- if ((panelConfig.animationType & EAnimationMode.Alpha) == EAnimationMode.Alpha)
- {
- canvasGroup.alpha = 0;
- }
- if ((panelConfig.animationType & EAnimationMode.Scale) == EAnimationMode.Scale)
- {
- transform.localScale = Vector3.zero;
- }
- if ((panelConfig.animationType & EAnimationMode.RightSlideIn) == EAnimationMode.RightSlideIn)
- {
- _cachedAnchorPos = _rectTransform.anchoredPosition;
- _rectTransform.anchoredPosition += new Vector2(_screenSize.x, 0);
- }
- _animationCoroutine = StartCoroutine(FadeInCoroutine(FADE_TIME));
- }
- protected virtual void OnShowEffectEnd()
- {
- OnEffectEnd();
- }
- protected virtual void OnCloseEffectStart()
- {
- OnEffectStart();
- if ((panelConfig.animationType & EAnimationMode.Alpha) == EAnimationMode.Alpha)
- {
- canvasGroup.alpha = 1;
- }
- if ((panelConfig.animationType & EAnimationMode.Scale) == EAnimationMode.Scale)
- {
- transform.localScale = Vector3.one;
- }
- if ((panelConfig.animationType & EAnimationMode.RightSlideIn) == EAnimationMode.RightSlideIn)
- {
- _rectTransform.anchoredPosition = _cachedAnchorPos;
- }
- _animationCoroutine = StartCoroutine(FadeOutCoroutine(FADE_TIME));
- }
- protected virtual void OnCloseEffectEnd()
- {
- OnEffectEnd();
- GameObject.Destroy(gameObject);
- }
- private void OnEffectStart()
- {
- _animationElapse = 0;
- if (_animationCoroutine != null)
- {
- StopCoroutine(_animationCoroutine);
- _animationCoroutine = null;
- }
- canvasGroup.interactable = false;
- }
- private void OnEffectEnd()
- {
- canvasGroup.interactable = true;
- _animationElapse = 0;
- _animationCoroutine = null;
- }
- private IEnumerator FadeInCoroutine(float time)
- {
- while (_animationElapse < time)
- {
- yield return null;
- _animationElapse += Time.deltaTime;
- float value = Mathf.Clamp01(_animationElapse / time);
- if ((panelConfig.animationType & EAnimationMode.Alpha) == EAnimationMode.Alpha)
- {
- canvasGroup.alpha = value;
- }
- if ((panelConfig.animationType & EAnimationMode.Scale) == EAnimationMode.Scale)
- {
- transform.localScale = new Vector3(value, value, value);
- }
- if ((panelConfig.animationType & EAnimationMode.RightSlideIn) == EAnimationMode.RightSlideIn)
- {
- var temp = (1 - value) * _screenSize.x;
- _rectTransform.anchoredPosition = new Vector2(_cachedAnchorPos.x + temp, _cachedAnchorPos.y);
- }
- }
- if ((panelConfig.animationType & EAnimationMode.Alpha) == EAnimationMode.Alpha)
- {
- canvasGroup.alpha = 1;
- }
- if ((panelConfig.animationType & EAnimationMode.Scale) == EAnimationMode.Scale)
- {
- transform.localScale = Vector3.one;
- }
- if ((panelConfig.animationType & EAnimationMode.RightSlideIn) == EAnimationMode.RightSlideIn)
- {
- _rectTransform.anchoredPosition = _cachedAnchorPos;
- }
-
- OnShowEffectEnd();
- }
- private IEnumerator FadeOutCoroutine(float time)
- {
- while (_animationElapse < time)
- {
- yield return null;
- _animationElapse += Time.deltaTime;
- float value = 1 - Mathf.Clamp01(_animationElapse / time);
- if ((panelConfig.animationType & EAnimationMode.Alpha) == EAnimationMode.Alpha)
- {
- canvasGroup.alpha = value;
- }
- if ((panelConfig.animationType & EAnimationMode.Scale) == EAnimationMode.Scale)
- {
- transform.localScale = new Vector3(value, value, value);
- }
- if ((panelConfig.animationType & EAnimationMode.RightSlideIn) == EAnimationMode.RightSlideIn)
- {
- var temp = (1 - value) * _screenSize.x;
- _rectTransform.anchoredPosition = new Vector2(_cachedAnchorPos.x + temp, _cachedAnchorPos.y);
- }
- }
- if ((panelConfig.animationType & EAnimationMode.Alpha) == EAnimationMode.Alpha)
- {
- canvasGroup.alpha = 0;
- }
- if ((panelConfig.animationType & EAnimationMode.Scale) == EAnimationMode.Scale)
- {
- transform.localScale = Vector3.zero;
- }
- if ((panelConfig.animationType & EAnimationMode.RightSlideIn) == EAnimationMode.RightSlideIn)
- {
- _rectTransform.anchoredPosition = new Vector2(_cachedAnchorPos.x + _screenSize.x, _cachedAnchorPos.y);
- }
- OnCloseEffectEnd();
- }
- #endregion
-
- /// <summary>
- /// on receive resolution change event
- /// </summary>
- /// <param name="res"></param>
- protected virtual void UIAdapt(Vector2Int res) {}
- /// <summary>
- /// common close api
- /// </summary>
- public virtual void Close()
- {
- UIManager.Instance.RemoveUI(this);
- }
- /// <summary>
- /// set canvas sorting order
- /// </summary>
- /// <param name="openOrder"></param>
- public void SetOpenOrder(int openOrder)
- {
- this.openOrder = openOrder;
- if (canvas != null)
- {
- canvas.sortingOrder = openOrder;
- }
- }
- /// <summary>
- /// also would destroy panel gameObject
- /// </summary>
- public virtual void Dispose()
- {
- if (panelConfig.animationType == EAnimationMode.None)
- {
- GameObject.Destroy(gameObject);
- return;
- }
-
- OnCloseEffectStart();
- }
- }
- }
|