Unity3d协程实现倒数计时

来源:互联网 发布:购金蝶软件会计分录 编辑:程序博客网 时间:2024/04/29 23:11

Unity3d协程的知识,不了解的同学可以在网上查找一下相关资料或者看一下Unity3D协程介绍 以及 使用。

下面介绍Unity3d协程实现倒数计时,实现代码:

public class GameManager : MonoBehaviour{    private bool _BoolIsDisplayNumber = false;//是否显示数字    private bool _BoolIsDisplayGo = false;//是否显示go    private float _IntDisplayNumber;    private string _StringDisplayNumber;    public Texture TextNum3;//要显示的图片3    public Texture TextNum2;    public Texture TextNum1;    public Texture TextGo;    void Start()    {        StartCoroutine(DisplayGameStartCountDown());    }    IEnumerator DisplayGameStartCountDown()    {        yield return new WaitForEndOfFrame();        //显示3        _BoolIsDisplayNumber = true;        _IntDisplayNumber = 3;        yield return new WaitForSeconds(1f);        _BoolIsDisplayNumber = false;        yield return new WaitForSeconds(0.5f);        //显示2        _BoolIsDisplayNumber = true;        _IntDisplayNumber = 2;        yield return new WaitForSeconds(1f);        _BoolIsDisplayNumber = false;        yield return new WaitForSeconds(0.5f);        //显示1        _BoolIsDisplayNumber = true;        _IntDisplayNumber = 1;        yield return new WaitForSeconds(1f);        _BoolIsDisplayNumber = false;        yield return new WaitForSeconds(0.5f);        //显示go        _BoolIsDisplayGo = true;        _StringDisplayNumber = "go";        yield return new WaitForSeconds(1f);        _BoolIsDisplayGo = false;        yield return new WaitForSeconds(0.5f);    }    void OnGUI()    {        //使用GUI显示图片        if (_BoolIsDisplayNumber)        {            if (_IntDisplayNumber == 3)   //显示3            {                GUI.DrawTexture(new Rect(Screen.width / 2 - TextNum3.width / 2, Screen.height / 2 - TextNum3.height / 2,                                         TextNum3.width, TextNum3.height), TextNum3);            }            else if (_IntDisplayNumber == 2)   //显示2            {                GUI.DrawTexture(new Rect(Screen.width / 2 - TextNum2.width / 2, Screen.height / 2 - TextNum2.height / 2,                                         TextNum2.width, TextNum2.height), TextNum2);            }            else if (_IntDisplayNumber == 1)   //显示1            {                GUI.DrawTexture(new Rect(Screen.width / 2 - TextNum1.width / 2, Screen.height / 2 - TextNum1.height / 2,                                         TextNum1.width, TextNum1.height), TextNum1);            }        }        if (_BoolIsDisplayGo)        {            if (_StringDisplayNumber == "go")   //显示go            {                GUI.DrawTexture(new Rect(Screen.width / 2 - TextGo.width / 2, Screen.height / 2 - TextGo.height / 2,                                         TextGo.width, TextGo.height), TextGo);            }        }    }}
具体实现代码如上所示,并不复杂。

在Inspector面板为Texture赋值,直接将图上拖上去就可以了。


运行效果:


这样就完成了协程实现倒数计时。不懂的同学可以私聊哦。

0 0
原创粉丝点击