int m_tempIndex = -1;// 字幕数组的index
bool m_finishOne = false;// 完成一列字
bool m_isFontPlaying = true;// 正在播放字
string[] m_tempContent = null;// 临时存储表中的字幕
IEnumerator PlayText()
{// 打字
for (int i=0;i< m_tempContent[m_tempIndex].Length;++i)
{
if (m_tempIndex < m_contentArral.Length && m_tempIndex >= 0)
{
m_contentArral[m_tempIndex].text += m_tempContent[m_tempIndex][i];
yield return new WaitForSeconds(0.12f);
}
}
m_finishOne = true;
}
public void Update()
{
if (m_finishOne)
{// 完成一个
m_finishOne = false;
m_isFontPlaying = false;
}
if (!m_isFontPlaying)
{// 没有正在写字
if (m_tempIndex < m_contentArral.Length - 1)
{
m_isFontPlaying = true;
m_tempIndex++;
StartCoroutine(this.PlayText());
}
else
{
this.Destroy();
}
}
if (m_isPlayAnimation)
{// 开始播放动画
m_isPlayAnimation = false;
if (m_anim.clip != null)
{
m_isPlaying = true;
m_hideTime = TimeWrap.time + m_anim.clip.length;
}
}
if (m_isPlaying && TimeWrap.time > m_hideTime)
{// 正在播放动画,时间结束,
m_isPlaying = false;
//this.Destroy();
m_tempIndex = 0;
if (m_tempContent != null && m_tempIndex < m_tempContent.Length)
{// m_tempIndex符合数组长度,开启协程打字
StartCoroutine(this.PlayText());
}
}
}
unity使用协程实现打字效果
于 2018-01-12 19:18:13 首次发布
本文介绍如何在Unity中使用协程实现文字打字效果。通过遍历字符串并逐字符显示,配合WaitForSeconds函数控制延迟,实现字幕逐字出现的动画。在Update方法中检查当前状态,以便在文字播放完成后开始新的字幕或者播放动画。

1684

被折叠的 条评论
为什么被折叠?



