using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Events;
public abstract class UIBase : MonoBehaviour
{
// Start is called before the first frame update
private UnityAction HideBack;
private CanvasGroup canvasGroup;
//淡入淡出的标记
private bool isShow;
//过场时间
private int switchTime = 10;
private void Awake()
{
canvasGroup = this.gameObject.GetComponent<CanvasGroup>();
if (canvasGroup == null)
{
canvasGroup = this.gameObject.AddComponent<CanvasGroup>();
}
}
protected virtual void Start()
{
Init();
}
// Update is called once per frame
public virtual void Update()
{
//淡入
if (isShow && canvasGroup.alpha != 1)
{
canvasGroup.alpha += Time.deltaTime*switchTime;
if (canvasGroup.alpha > 1)
{
canvasGroup.alpha = 1;
}
}
//淡出
else if(!isShow)
{
canvasGroup.alpha -= Time.deltaTime*switchTime;
if (canvasGroup.alpha <= 0)
{
canvasGroup.alpha = 0;
//执行委托函数
HideBack?.Invoke();
}
}
}
protected abstract void Init();
public virtual void ShowMe()
{
isShow = true;
//从0开始显示
canvasGroup.alpha = 0;
}
public virtual void HideMe(UnityAction callback)
{
isShow = false;
//从1开始隐藏
canvasGroup.alpha = 1;
//传入一个在隐藏时的委托函数
HideBack = callback;
}
}