使用Invoke代码实现321倒数

Posted CaiLin907

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Invoke代码实现321倒数相关的知识,希望对你有一定的参考价值。

Unity使用Invoke代码实现321倒数


Invoke(“aaa”, 2); //每2秒调用函数"aaa"

示例代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIScripts : MonoBehaviour


    public Text three;
    public Text two;
    public Text one;
    // Start is called before the first frame update
    void Start()
    
        three.gameObject.SetActive(false);
        two.gameObject.SetActive(false);
        one.gameObject.SetActive(false);
        OnThree();
        Invoke("OnTwo", 1);
        Invoke("OnOne", 2);
        Invoke("OnEnd", 3);
    

    public void OnThree()
    
        three.gameObject.SetActive(true);
    

    public void OnTwo()
    
        Destroy(three.gameObject);
        two.gameObject.SetActive(true);
    

    public void OnOne()
    
        one.gameObject.SetActive(true);
        Destroy(two.gameObject);
    
    public void OnEnd()
    
        Destroy(one.gameObject);
    


注:

这里是用字符串的方式调用,如果调用的函数出现错误则无法正常运行

使用UI类型时,代码需要有using UnityEngine.UI字样

示例为数字,如果倒计时是图片,则使用:

	Public Image three;
	Public Image two;
	Public Image one;

以上是关于使用Invoke代码实现321倒数的主要内容,如果未能解决你的问题,请参考以下文章