Unity3d协程实现倒数计时

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赋值,直接将图上拖上去就可以了。


运行效果:


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值