基本上是标题要求的内容。我想播放我的动画,然后在动画播放完毕后,我希望能有一点延迟,使其在大约2秒钟内消失在场景之外。淡出后,应将其禁用并重置。
到目前为止,这是我的代码,但是当我将解锁设置为true时,它什么都不做。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationController : MonoBehaviour
{
public Animator anim;
public SpriteRenderer sr;
public bool unlocking;
Sprite unlockSprite;
Sprite lockSprite;
Color temp;
// Start is called before the first frame update
void Start()
{
unlocking = false;
unlockSprite = Resources.Load<Sprite>("unlock");
lockSprite = Resources.Load<Sprite>("lock");
}
// Update is called once per frame
void Update()
{
if (unlocking) {
anim.Play("unlock");
unlocking = false;
sr.sprite = unlockSprite;
System.Threading.Thread.Sleep(1000);
StartCoroutine(FadeTo(0.0f, 2.0f));
temp = new Color(sr.color.r, sr.color.g, sr.color.b, 0f);
sr.color = temp;
gameObject.SetActive(false);
sr.sprite = lockSprite;
temp = new Color(sr.color.r, sr.color.g, sr.color.b, 1f);
sr.color = temp;
}
}
IEnumerator FadeTo(float aValue, float aTime)
{
float alpha = sr.color.a;
for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
{
Color newColor = new Color(sr.color.r, sr.color.g, sr.color.b, Mathf.Lerp(alpha, aValue, t));
sr.color = newColor;
yield return null;
}
}
}
更新的代码:
anim.Play("unlock");
unlocking = false;
if (animInfo.normalizedTime >= 1)
{
anim.enabled = false;
sr.sprite = unlockSprite;
StartCoroutine(FadeTo(0.0f, 2.0f));
temp = new Color(sr.color.r, sr.color.g, sr.color.b, 0f);
sr.color = temp;
gameObject.SetActive(false);
sr.sprite = lockSprite;
temp = new Color(sr.color.r, sr.color.g, sr.color.b, 1f);
sr.color = temp;
}
实现它的一种方法是制作另一个协程,像这样:
IEnumerator UnlockSequence()
{
while (animInfo.normalizedTime < 1.0f)
{
yield return null;
}
anim.enabled = false;
sr.sprite = unlockSprite;
yield return new WaitForSeconds(1.0f);
yield return StartCoroutine(FadeTo(0.0f, 2.0f));
// everything else
}