使用了IEnumerator 和 WaitForSeconds
以下是示例代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimatePanel : MonoBehaviour
{
public AnimationCurve showCurve;
public AnimationCurve hideCurve;
public float animationSpeed;
public GameObject panel;
IEnumerator ShowPanel(GameObject gameObject)
{
float timer = 0;
while (timer <= 1)
{
gameObject.transform.localScale = Vector3.one * showCurve.Evaluate(timer);
timer += Time.deltaTime * animationSpeed;
yield return null;
}
}
IEnumerator HidePanel(GameObject gameObject)
{
float timer = 0;
while (timer <= 1)
{
gameObject.transform.localScale = Vector3.one * hideCurve.Evaluate(timer);
timer += Time.deltaTime * animationSpeed;
yield return null;
}
if (gameObject.transform.localScale.x > 0)
{
gameObject.transform.localScale = new Vector3(0, 0, 0);
}
}
IEnumerator DelayHidePanel(GameObject gameObject)
{
yield return new WaitForSeconds(3.0f);
StartCoroutine(HidePanel(gameObject));
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
StartCoroutine(ShowPanel(panel));
StartCoroutine(DelayHidePanel(panel));
}
else if (Input.GetMouseButtonDown(1))
{
StartCoroutine(HidePanel(panel));
}
}
}