//先拿到prefab的Clone的物体:
GameObject go = Resources.Load<GameObject>(MainContainer.PrefabFolder+ "cueText");
cueText= Instantiate(go,GameObject.Find("Canvas").transform);
//开启协程
StartCoroutine(cueTextFade());
生成文字物体后,先把它的透明度置为0,然后让它在0.5秒钟内渐现。
然后停顿1秒。
最后在0.5秒内渐隐。
渐隐结束后,销毁物体。
IEnumerator cueTextFade()
{
//初始化文字,透明度为0
cueText.SetActive(true);
Text text= cueText.GetComponent<Text>();
text.color = new Color(text.color.r,text.color.g,text.color.b,0);
//文字渐现
text.DOFade(1f, 0.5f);
//文字停顿1秒
yield return new WaitForSeconds(1f);
//文字渐隐
text.DOFade(0f, 0.5f);
//渐隐结束后销毁物体
yield return new WaitForSeconds(0.5f);
cueText.SetActive(false);
}
本文介绍如何在Unity中使用协程实现淡入淡出的文字提示效果。首先通过克隆预制件生成文字物体,并将其透明度设置为0。接着,通过调用DOFade方法在0.5秒内将文字逐渐显示,随后暂停1秒,再次通过DOFade方法在0.5秒内将文字逐渐隐藏。最后,在文字完全隐藏后销毁该物体。
1624





