您的位置:首页 > 移动开发 > Unity3D

unity使用协程实现打字效果

2018-01-12 19:18 811 查看
    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());

            }

        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ui unity