Unity 单击按钮按钮逐渐消失_Dotween插件,使用协程,Timer 实现
使用unity的协程实现
点击按钮的时候会启动协程,然后触发点击的时候的事件(onClickCallback),并逐渐消失,当隐藏的时候触发完全隐藏的事件(onFadeCompleteCallback)。
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class FadeButtonCoroutine : MonoBehaviour
{
public Button button;//要触发事件的按钮
public float fadeDuration = 1f;//消失时间
public Action onClickCallback;
public Action onFadeCompleteCallback;
private Image buttonImage;
private TMP_Text buttonText;
private void Start()
{
if (button != null)
{
button.onClick.AddListener(OnButtonClick);
buttonImage = button.GetComponent<Image>();
buttonText = button.GetComponentInChildren<TMP_Text>();
}
}
private void OnButtonClick()
{
onClickCallback?.Invoke();
StartCoroutine(FadeButtonOut());
}
private IEnumerator FadeButtonOut()
{
if (buttonImage != null && buttonText != null)
{
float elapsedTime = 0f;
Color startImageColor = buttonImage.color;
Color startTextColor = buttonText.color;
while (elapsedTime < fadeDuration)
{
elapsedTime += Time.deltaTime;
float alpha = Mathf.Lerp(startImageColor.a, 0f, elapsedTime / fadeDuration);
// 同步更新 Image 和 TMP_Text 的透明度
buttonImage.color = new Color(startImageColor.r, startImageColor.g, startImageColor.b, alpha);
buttonText.color = new Color(startTextColor.r, startTextColor.g, startTextColor.b, alpha);
yield return null;
}
onFadeCompleteCallback?.Invoke();
}
}
//在其他code 中 为action 赋值
private void Start()
{
// 获取 FadeButtonCoroutine 组件
fadeButton = GetComponent<FadeButtonCoroutine >();
// 为 onClickCallback 赋值
fadeButton.onClickCallback = OnClickHandler;
// 为 onFadeCompleteCallback 赋值
fadeButton.onFadeCompleteCallback = OnFadeCompleteHandler;
}
private void OnClickHandler()
{
Debug.Log("按钮被点击了!");
}
private void OnFadeCompleteHandler()
{
Debug.Log("按钮完全隐藏了!");
}
使用Timer
使用timer可能在web平台不兼容会出错,可以使用unity的携程或者其他方式。
// 方式2: 使用Timer
using UnityEngine;
using UnityEngine.UI;
public class FadeButtonTimer : MonoBehaviour
{
public Button button;
public float fadeDuration = 1f;
public Action onClickCallback;
public Action onFadeCompleteCallback;
private float timer;
private bool isFading;
private Image buttonImage;
private TMP_Text buttonText;
private Color startImageColor;
private Color startTextColor;
private void Start()
{
if (button != null)
{
button.onClick.AddListener(OnButtonClick);
buttonImage = button.GetComponent<Image>();
buttonText = button.GetComponentInChildren<TMP_Text>();
}
}
private void OnButtonClick()
{
onClickCallback?.Invoke();
if (buttonImage != null && buttonText != null)
{
startImageColor = buttonImage.color;
startTextColor = buttonText.color;
isFading = true;
timer = 0f;
}
}
private void Update()
{
if (isFading)
{
timer += Time.deltaTime;
float alpha = Mathf.Lerp(startImageColor.a, 0f, timer / fadeDuration);
// 同步更新 Image 和 TMP_Text 的透明度
buttonImage.color = new Color(startImageColor.r, startImageColor.g, startImageColor.b, alpha);
buttonText.color = new Color(startTextColor.r, startTextColor.g, startTextColor.b, alpha);
if (timer >= fadeDuration)
{
isFading = false;
onFadeCompleteCallback?.Invoke();
}
}
}
}