美文网首页
Unity-黑暗之魂复刻-计时类

Unity-黑暗之魂复刻-计时类

作者: 祝你万事顺利 | 来源:发表于2019-06-03 19:35 被阅读0次

    1.在按钮按下后有一段时间可以在接受一次按钮
    变量:
    duration 延长时间长度
    elapsedTime已经延长时间
    STATE状态

    public class MyTimer  {
        //Timer计时多长
        public float duration = 1.0f;
    
        private float elapsedTime = 0;
        public enum STATE
        {
            IDLE,
            RUN,
            FINISHED
        }
        public STATE state;
    
        public void Tick()
        {
            switch (state)
            {
                case STATE.IDLE:
    
                    break;
                case STATE.RUN:
                    elapsedTime += Time.deltaTime;
                    if (elapsedTime >= duration)
                    {
                        state = STATE.FINISHED;
                    }
                    break;
                case STATE.FINISHED:
    
                    break;
                default:
                    Debug.Log("MyTimer Error");
                    break;
            }
        }
    
        public void Go()
        {
            elapsedTime = 0;
            state = STATE.RUN;
        }
    }
    

    2.DoubleTigger在上面的基础上给Button加一个计时器,在ButtonOnReleased的时候,开始计时,直到extendingDurartion结束,加上OnPressing时间都属于DoubleTrigger检测时间范围,在extendingDuration中&&检测到了onPressed,那就是触发了DoubleTrigger。

    3.LongPress在按下后进行计时,在一段时间后才将STATE设置为RUN。

    在Button中设置IsExtending、IsDelaying两个信号

    public class MyButton {
        public bool IsPressing = false;
        public bool OnPressed = false;
        public bool OnReleased = false;
        public bool IsExtending = false;
        public bool IsDelaying = false;
    
        public float extendingDuration = 0.15f;
        public float delayingDuration = 0.15f;
    
        private bool curState = false;
        private bool lastState = false;
    
        private MyTimer extTimer = new MyTimer();
        private MyTimer delayTimer = new MyTimer();
    
        public void Tick(bool input)
        {
            extTimer.Tick();
            delayTimer.Tick();
    
            curState = input;
            IsPressing = curState;
            OnPressed = false;
            OnReleased = false;
            IsExtending = false;
            IsDelaying = false;
    
            if (curState != lastState)
            {
                if(curState == true)
                {
                    OnPressed = true;
                    StartTimer(delayTimer, delayingDuration);
                }
                else
                {
                    OnReleased = true;
                    StartTimer(extTimer, extendingDuration);
                }
            }
            lastState = curState;
            if(extTimer.state == MyTimer.STATE.RUN)
            {
                IsExtending = true;
            }
            if(delayTimer.state == MyTimer.STATE.RUN)
            {
                IsDelaying = true;
            }
        }
    
        private void StartTimer(MyTimer myTimer,float duration)
        {
            myTimer.duration = duration;
            myTimer.Go();
        }
    }
    

    以下将3个动作融合到了一个按键上:
    跑的设置:
    A按下并且不在Delaying,或者在Extending

     run = (buttonA.IsPressing && !buttonA.IsDelaying) || buttonA.IsExtending;
    

    翻滚设置:
    A松开并且A在Delaying范围

    roll = buttonA.OnReleased && buttonA.IsDelaying;
    

    跳跃设置:
    在Extending并且按下A

    jump = buttonA.OnPressed && buttonA.IsExtending;
    

    相关文章

      网友评论

          本文标题:Unity-黑暗之魂复刻-计时类

          本文链接:https://www.haomeiwen.com/subject/nkgnaqtx.html