最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity計時器功能實現(xiàn)示例

 更新時間:2021年10月26日 09:08:00   作者:小紫蘇  
計時器在很多地方都可以使用,本文主要介紹了Unity計時器功能實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Unity計時器

Demo展示

介紹

游戲中有非常多的計時功能,比如:各種cd,以及需要延時調(diào)用的方法;

一般實現(xiàn)有一下幾種方式:

1.手動計時

float persistTime = 10f
float startTime = Time.time;
if(Time.time - startTime > persistTime)
{
 Debug.Log("計時結束");
}

float curTime = 0;
curTime += Time.deltaTime;
if(curTime > persistTime)
{
    Debug.Log("計時結束");
}

2.協(xié)程

private float persistTime = 10f;
IEnumerator DelayFunc()
{
    yield return persistTime;
    Debug.Log("計時結束");
}

private void Start()
{
    StartCoroutine(DelayFunc());
}

3.Invoke回調(diào)

private void Start()
{
    Invoke("DelayFunc", persistTime);
}

計時器功能

計時是為了到特定的時間,執(zhí)行某個功能或方法;

計時器(Timer):設計了計時暫停,計時重置,計時開始方法,計時中方法,計時結束方法,固定間隔調(diào)用方法,計時器可設置復用或單次;

計時管理器(TimerMa):負責倒計時,以及執(zhí)行計時器方法;

代碼:

using System;
using System.Collections.Generic;
using UnityEngine;
using Object = System.Object;

public class Timer
{
    public delegate void IntervalAct(Object args);
    //總時間和當前持續(xù)時間
    private float curtime = 0;
    private float totalTime = 0;

    //激活
    public bool isActive;
    //計時結束是否銷毀
    public bool isDestroy;
    //是否暫停
    public bool isPause;

    //間隔事件和間隔事件——Dot
    private float intervalTime = 0;
    private float curInterval = 0;
    private IntervalAct onInterval;
    private Object args;
 
    //進入事件
    public Action onEnter;
    private bool isOnEnter = false;
    //持續(xù)事件
    public Action onStay;
    //退出事件
    public Action onEnd;

    public Timer(float totalTime, bool isDestroy = true, bool isPause = false)
    {
        curtime = 0;
        this.totalTime = totalTime;
        isActive = true;
        this.isDestroy = isDestroy;
        this.isPause = isPause;
        TimerMa.I.AddTimer(this);
    }

    public void Run()
    {
        //暫停計時
        if (isPause || !isActive)
            return;

        if (onEnter != null)
        {
            if (!isOnEnter)
            {
                isOnEnter = true;
                onEnter();
            }
        }

        //持續(xù)事件
        if (onStay != null)
            onStay();
        
        curtime += Time.deltaTime;

        //間隔事件
        if (onInterval != null)
        {
            curInterval += Time.deltaTime;
            if (curInterval > intervalTime)
            {
                onInterval(args);
                curInterval = 0;
            }
        }

        //計時結束
        if (curtime > totalTime)
        {
            curtime = 0;
            isActive = false;
            if (onEnd != null)
            {
                onEnd();
            }
        }   
    }
    
    //設置間隔事件
    public void SetInterval(float interval, IntervalAct intervalFunc, Object args = null)
    {
        this.intervalTime = interval;
        onInterval = intervalFunc;
        curInterval = 0;
        this.args = args;
    }
    
    //重置計時器
    public void Reset()
    {
        curtime = 0;
        isActive = true;
        isPause = false;
        curInterval = 0;
        isOnEnter = false;
    }
    
    //獲取剩余時間
    public float GetRemainTime()
    {
        return totalTime - curtime;
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TimerMa : MonoBehaviour
{
    #region 單例

    private static TimerMa instance;
    TimerMa() {}

    public static TimerMa I
    {
        get
        {
            if (instance == null)
                instance = new TimerMa();
            return instance;
        }
    }

    #endregion
    private List<Timer> timerList;

    private void Awake()
    {
        instance = this;
        timerList = new List<Timer>();
    }

    public void AddTimer(Timer t)
    {
        timerList.Add(t);
    }

    void Update()
    {
        for (int i = 0; i < timerList.Count;)
        {
            timerList[i].Run();
            
            //計時結束,且需要銷毀
            if(!timerList[i].isActive && timerList[i].isDestroy)
                timerList.RemoveAt(i);
            else
                ++i;
        }
    }
}

測試計時器

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

public class Test : MonoBehaviour
{
    public Text mText1;
    public Text mText2;
    private Timer timer;
    private int count = 0;
    
    void Start()
    {
        timer = new Timer(5f,false);
        timer.SetInterval(1f, OnInterval);
        timer.onEnter = OnStart;
        timer.onEnd = OnExit;
    }
    
    void Update()
    {
        Debug.Log(count);
        mText1.text = timer.GetRemainTime().ToString("f2");

        if (Input.GetKeyDown(KeyCode.A))
        {
            if (!timer.isPause)
            {
                timer.isPause = true;
                mText2.text = "暫停計時";
            }
        }
        
        if (Input.GetKeyDown(KeyCode.S))
        {
            if (timer.isPause)
            {
                timer.isPause = false;
                mText2.text = "取消暫停計時";
            }
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            timer.Reset();
            mText2.text = "重置計時";
        }
    }

    private void OnStart()
    {
        mText2.text = "開始計時";
    }
    
    private void OnExit()
    {
        mText2.text = "結束計時";
    }

    private void OnInterval(Object value)
    {
        count++;
        mText2.text = $"間隔事件調(diào)用{count}";
    }
}

到此這篇關于Unity計時器功能實現(xiàn)示例的文章就介紹到這了,更多相關Unity計時器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 細說C#中的枚舉:轉(zhuǎn)換、標志和屬性

    細說C#中的枚舉:轉(zhuǎn)換、標志和屬性

    枚舉是 C# 中最有意思的一部分,大部分開發(fā)人員只了解其中的一小部分,甚至網(wǎng)上絕大多數(shù)的教程也只講解了枚舉的一部分。那么,我將通過這篇文章向大家具體講解一下枚舉的知識,需要的朋友可以參考下
    2020-02-02
  • C# 并行和多線程編程——認識和使用Task

    C# 并行和多線程編程——認識和使用Task

    這篇文章主要介紹了C# 并行和多線程編程——認識和使用Task的的相關資料,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2021-02-02
  • c# 網(wǎng)址壓縮簡單實現(xiàn)短網(wǎng)址

    c# 網(wǎng)址壓縮簡單實現(xiàn)短網(wǎng)址

    短網(wǎng)址,忽然一下子就冒出來的東西,長長的一個URL,提交過去,出來就只有短短的一個URL了,看起來似乎挺神奇,其實簡單分析一下,明白其中的原理,也是一件很簡單的事情,需要的朋友可以了解下
    2012-12-12
  • c# 如何實現(xiàn)圖片壓縮

    c# 如何實現(xiàn)圖片壓縮

    這篇文章主要介紹了c# 實現(xiàn)圖片壓縮的示例,幫助大家更好的理解和學習c#,感興趣的朋友可以了解下
    2020-11-11
  • C#七大經(jīng)典排序算法系列(上)

    C#七大經(jīng)典排序算法系列(上)

    這篇文章主要為大家詳細介紹了C#七大經(jīng)典排序算法系列上篇,冒泡排序,快速排序等,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • C#實現(xiàn)DataGridView控件行列互換的方法

    C#實現(xiàn)DataGridView控件行列互換的方法

    這篇文章主要介紹了C#實現(xiàn)DataGridView控件行列互換的方法,涉及C#中DataGridView控件元素遍歷與添加操作的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • C#讀寫txt文件的2種方法

    C#讀寫txt文件的2種方法

    這篇文章主要為大家詳細介紹了C#讀寫txt文本文檔數(shù)據(jù)的2種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • C# 數(shù)組中的 indexOf 方法及使用

    C# 數(shù)組中的 indexOf 方法及使用

    這篇文章主要介紹了C# 數(shù)組中的 indexOf 方法以及indexof方法的使用講解,需要的朋友可以參考下
    2018-02-02
  • 解決WPF附加屬性的Set函數(shù)不調(diào)用的問題

    解決WPF附加屬性的Set函數(shù)不調(diào)用的問題

    這篇文章介紹了解決WPF附加屬性的Set函數(shù)不調(diào)用的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • UGUI實現(xiàn)圖片拖拽功能

    UGUI實現(xiàn)圖片拖拽功能

    這篇文章主要為大家詳細介紹了UGUI實現(xiàn)圖片拖拽功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-02-02

最新評論

芜湖县| 革吉县| 万安县| 郯城县| 安西县| 囊谦县| 兴国县| 务川| 桓台县| 顺义区| 裕民县| 六安市| 中江县| 德格县| 西丰县| 濉溪县| 白水县| 墨竹工卡县| 海阳市| 全州县| 嘉禾县| 河东区| 宾川县| 沙河市| 高清| 峡江县| 辽阳市| 应用必备| 五寨县| 马尔康县| 平顶山市| 江源县| 金沙县| 汶上县| 甘南县| 盐津县| 阳西县| 行唐县| 常德市| 温州市| 铜川市|