unity游戏的开始和暂停 计时器

Posted 那个妹子留步

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity游戏的开始和暂停 计时器相关的知识,希望对你有一定的参考价值。

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

/// <summary>
/// 实现控制 游戏的暂停和继续功能
/// 对于动画暂停可以 mMoveTween.timeScale = timeScale
/// </summary>
public class Timer : MonoBehaviour

    public event Action TimerUpdate;
    public MyTime MyTime1;
    public MyTime MyTime2;
    // Start is called before the first frame update
    void Start()
    
        int count = 1;
        MyTime myTime1 = new MyTime(() =>  print(1); count++; , 5f, -1, false);
        TimerUpdate += myTime1.Update;
        myTime1.Start();
        MyTime1 =myTime1;
        //int count2 = 1;
        //MyTime myTime2 = new MyTime(() =>  print(2); count2--; , 5, 4, false);
        //TimerUpdate += myTime2.Update;
        //myTime2.Start();
        //MyTime2 = myTime2;
    

    // Update is called once per frame
    void Update()
    
        TimerUpdate();

        if (Input.GetKeyDown(KeyCode.W))
        
            MyTime1.Stop(); //MyTime2.Stop();
        

        if (Input.GetKeyDown(KeyCode.S))
        
            MyTime1.Continue();// MyTime2.Continue();
        
    


public class MyTime

    private Action _fun;//需要执行的函数
    private float _duration;//时间间隔
    private float _time;//存储时间间隔  注意最小间隔
    private int _loop;//循环次数
    private bool _unscaled;//是否时间缩放
    public MyTime(Action fun,float duration,int loop,bool unscaled)
    
        _fun = fun;
        _duration = _time = duration;
        _loop = loop;
        _unscaled = unscaled;
    

    public void Reset(Action fun, float duration, int loop, bool unscaled)
    
        _fun = fun;
        _duration = _time = duration;
        _loop = loop;
        _unscaled = unscaled;
        _isEnding = false;
        _running = true;
    

    private bool _isEnding = false;//是否结束
    private bool _running = false;//是否运行
    public void Start()
    
        _running = true;
    

    public void End()
    
        _isEnding = true;
        _running = false;
    

    public void Stop()
    
        if (!_isEnding)
        
            _running = false;
        
    

    public void Continue()
    
        if (!_isEnding)
        
            _running = true;
            
    


    public void Update()
    
        if (!_running) return;

        float delta = _unscaled ? Time.unscaledDeltaTime : Time.deltaTime;
        _time -= delta;
        if (_time <= 0)
        
            
            if (_loop > 0)
            
                _loop--;
                _time += _duration;
            

            if (_loop == 0)
            
                End();
            
            else if (_loop < 0)
            
                _time += _duration;
            
            _fun();
        

    

以上是关于unity游戏的开始和暂停 计时器的主要内容,如果未能解决你的问题,请参考以下文章

Unity3D日常开发Unity3D中实现计时器工具类-正计时倒计时暂停计时加速计时

Unity3D日常开发Unity3D中实现计时器工具类-正计时倒计时暂停计时加速计时

Unity3D日常开发Unity3D中实现计时器工具类-正计时倒计时暂停计时加速计时

unity3d 中的能源效率 - 暂停应用程序等待用户交互

Unity常用类—Time类

[Unity计时器倒计时,当应用杀死Android时不起作用