使用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;

猜你喜欢

转载自blog.csdn.net/xichi_12396/article/details/116026522