UIBase.cs 3.25 KB
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIBase : MonoBehaviour, IUIBase
{
    private UIManager uiManger;

    /// <summary>
    /// 是否是可以删除的UI
    /// </summary>
    public bool isCanDestroy = false;

    /// <summary>
    /// 是否打开
    /// </summary>
    public bool IsOpen;

    /// <summary>
    /// 是否正在播放动画
    /// </summary>
    private bool isAnimation = false;

    /// <summary>
    /// UI打开动画枚举
    /// </summary>
    public enum EUiAnimationType
    {
        None,
        Fade,
        Zoom
    }

    /// <summary>
    /// UI打开得方式选择
    /// </summary>
    public EUiAnimationType animationType = EUiAnimationType.None;


    #region 注册与删除

    private void Awake()
    {
        IUIBase uiBase = this;
        uiBase.RegisterUI();
    }

    private void OnDestroy()
    {
        IUIBase uiBase = this;
        uiBase.UnRegisterUI();
    }

    #endregion

    public void Open(UIManager uiManager)
    {
        uiManger = uiManager;
        IsOpen = true;
        gameObject.transform.SetAsFirstSibling();
        SwitchOpenAnimation();
    }

    public void Close(UIManager uiManager)
    {
        uiManger = uiManager;
        IsOpen = false;
        SwitchCloseAnimation();
    }

    public UIBase GetUI()
    {
        return this;
    }

    #region 基类打开与关闭回调

    /// <summary>
    /// 打开后完成回调 子类重写
    /// </summary>
    protected virtual void OpenComplete()
    {
        isAnimation = false;
        gameObject.transform.SetAsLastSibling();
    }

    /// <summary> 
    /// 关闭后的完成回调 子类重写
    /// </summary>
    protected virtual void CloseComplete()
    {
        isAnimation = false;
        if (isCanDestroy)
        {
            uiManger.UnRegisterUI(this);
            Destroy(gameObject);
        }
    }

    #endregion


    #region 打开方式与动画选择

    private void SwitchOpenAnimation()
    {
        // 动画没放完 return
        if (isAnimation)
        {
            return;
        }

        isAnimation = true;
        switch (animationType)
        {
            case EUiAnimationType.None:
                gameObject.SetActive(true);
                OpenComplete();
                break;
            case EUiAnimationType.Zoom:
                UIAnimation.ZoomIn(gameObject, OpenComplete);
                break;
            case EUiAnimationType.Fade:
                gameObject.transform.localScale = Vector3.one;
                UIAnimation.FadeIn(gameObject, OpenComplete);
                break;
        }
    }

    private void SwitchCloseAnimation()
    {
        // 动画没放完 return
        if (isAnimation)
        {
            return;
        }

        isAnimation = true;
        switch (animationType)
        {
            case EUiAnimationType.None:
                gameObject.SetActive(false);
                CloseComplete();
                break;
            case EUiAnimationType.Zoom:
                UIAnimation.ZoomOut(gameObject, CloseComplete);
                break;
            case EUiAnimationType.Fade:
                UIAnimation.FadeOut(gameObject, CloseComplete);
                break;
        }
    }

    #endregion
}