1.实现double trigger
2.long press长按
紫色的部分(IsExteding)是要侦测用户是否按下第二次
1.double trigger:
新建MyTimer时间类:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyTimer{
/// <summary>
/// 状态
/// </summary>
public enum STATE
{
IDLE,
RUN,
FINISHED
}
public STATE state;
public float duration = 1.0f; //要算多久 缺省1s后停止
private float elapsedTime = 0; //过去的时间 初始值是0
public void Tick()
{
if(state == STATE.IDLE)
{
}
else if (state == STATE.RUN)
{
elapsedTime += Time.deltaTime;
if (elapsedTime >= duration)
{
state = STATE.FINISHED;
}
}
else if (state == STATE.FINISHED)
{
}
else
{
Debug.Log("Error");
}
}
public void Go()
{
elapsedTime = 0;
state = STATE.RUN;
}
}
利用MyButton得到状态:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyButton {
/// <summary>
/// 三种状态(缺省为false)
/// </summary>
public bool IsPressing = false;
public bool OnPressed = false;
public bool OnReleased = false;
public bool IsExtending = false;
public float extendingDuration = 0.15f;
private bool curState = false; //目前的状态
private bool lastState = false; //前一次状态
private MyTimer extTimer = new MyTimer();
public void Tick(bool input)
{
// StartTimer(extTimer, 1.0f);
extTimer.Tick();
curState = input;
IsPressing = curState;
OnPressed = false;
OnReleased = false;
if(curState != lastState)
{
if(curState ==true )
{
OnPressed = true;
}
else
{
OnReleased = true;
StartTimer(extTimer, extendingDuration);
}
}
lastState = curState;
if (extTimer.state == MyTimer.STATE.RUN)
{
IsExtending = true;
}
else
{
IsExtending = false;
}
}
private void StartTimer(MyTimer timer,float duration)
{
timer.duration = duration;
timer.Go();
}
}
最后在joystick里面显示出来:
//print(buttonA.IsExtending);//延长的时间
//print(buttonA.IsExtending || buttonA.IsPressing);//按键的时间加延长的时间为true
print(buttonA.IsExtending&&buttonA.OnPressed );
这样首先实现了double trigger判断。