栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

Unity倒计时脚本(两种方法)

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity倒计时脚本(两种方法)

1.代码 1.1用deltatime计时
public class TimeDemo : MonoBehaviour
{
    private Text textTime;
    public int second = 120;
    public float totalTime;
    int M ;
    int S ;
    private void Start()
    {
        textTime = this.GetComponent();
    }
    private void Update()
    {
        Timer2();
    }

    private void Timer2()
    {
        totalTime += Time.deltaTime;
        if (totalTime >= 1)
        {
            second--;
            if (second >= 0)
            {
                totalTime-=1;
                M = second / 60;
                S = second % 60;
                textTime.text = string.Format("{0:d2}:{1:d2}", M, S);
                if (M == 0 && S == 10)//剩十秒变红
                    textTime.color = Color.red;
            }else Time.timeScale = 0;
            
        }
    }
}
1.2应用Invoke系列函数计时
public class TimeDemo : MonoBehaviour
{
    private Text textTime;
    public int second = 120;
    int M ;
    int S ;
    private void Start()
    {
        textTime = this.GetComponent();
        InvokeRepeating("Timer", 1, 1);
        
    }
    private void Timer()
    {
        second--;
        M = second / 60;
        S = second % 60;
        textTime.text = string.Format("{0:d2}:{1:d2}", M, S);
        if (M == 0 && S == 10)//剩十秒变红
            textTime.color = Color.red;
        if (second <= 0)
            CancelInvoke("Timer");
    }
    
}
2.出现NullReferenceException异常的原因

 翻译为空引用异常,顾名思义就是进行了null.方法 的用法。

大部分情况为

1.无初始化

2.没挂文本文件

3.没有这个组件。

4.脚本没对应

3.使用Animation的异常发生

当录制animation动画前物品没有amimation组件,便会在录制时自动创建一个animater组件影响animation动画引用的正常使用。

正常情况下:

 

 异常情况下:

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901330.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号