[英]Unity: Need help how to double click detection in c#?
我想創建鼠標單擊,檢測單擊,按住單擊,然后雙擊。 當我單擊時,角色會更快(moveSpeed = 15),當我按住單擊並雙擊沒有動作時,角色仍然保持恆定速度(moveSpeed = 3)。
這是我的代碼:
private float t0, moveSpeed;
private bool longClick, shortClick;
void Start ()
{ t0 = 0f; longClick = false; shortClick = false; }
void update()
{
// how to add the code for detect double click and where I write it?
if (Input.GetMouseButtonDown (0))
{
t0 = 0; t0 += Time.deltaTime;
longClick = false; shortClick = false;
moveSpeed = 0;
}
if (Input.GetMouseButton(0))
{
t0 += Time.deltaTime;
if (t0 < 0.2)
{ moveSpeed = 15;longClick = false; shortClick = true; } // this is single click!
if (t0 > 0.2)
{ moveSpeed = 3; longClick = true; shortClick = false; } // this is hold click!
}
if (Input.GetMouseButtonUp(0))
{
if (longClick == true)
{ moveSpeed = 3; }
else if (shortClick = true)
{ moveSpeed = 3; }
}
}
你試過谷歌嗎? - 請參閱第二個答案: http : //answers.unity3d.com/questions/331545/double-click-mouse-detection-.html
在C#中:
private float lastClickTime;
public float catchTime = 0.25f;
void Update ()
{
if(Input.GetButtonDown("Fire1"))
{
if(Time.time - lastClickTime < catchTime)
{
//double click
print("Double click");
}
else
{
//normal click
}
lastClickTime = Time.time;
}
}
我在這里回復: http : //answers.unity3d.com/answers/1133702/view.html
基本上:使用里面的doubleClicked檢查器創建一個新類。 然后在if子句中使用它。
class DoubleClickListener {
bool firstClick = false;
float runningTimerSecond;
float delay = 0.25F;
public DoubleClickListener() { }
public DoubleClickListener(float delay) {
this.delay = delay;
}
public bool isDoubleClicked() {
// If the time is too long we reset first click variable
if (firstClick && (Time.time - runningTimerSecond) > delay) {
firstClick = false;
}
if (!firstClick) {
firstClick = true;
runningTimerSecond = Time.time;
} else {
firstClick = false;
return true;
}
return false;
}
}
DoubleClickBehaviorBase是一個基類,可以為您進行雙擊。 您繼承自DoubleClickBehaviorBase而不是MonoBehavior您需要在類中重寫OnDoubleClickOverride。 我還添加了一個MonoBehaviourExtension,可以更輕松地調用和實現StartCoroutine:
namespace DoubleClick
{
public static class MonoBehaviourExtension
{
public static void StartCoroutine(this MonoBehaviour mb, params Action[] funcs)
{
mb.StartCoroutine(CoroutineRunnerSimple(funcs));
}
private static System.Collections.IEnumerator CoroutineRunnerSimple(Action[] funcs)
{
foreach (var func in funcs)
{
if (func != null)
func();
yield return new WaitForSeconds(.01f);
}
}
}
abstract public class DoubleClickBehaviorBase : MonoBehaviour
{
float _DoubleClickTimer = 0.0f;
float _DoubleClickDelay = 0.5f;
bool _WasClicked = false;
// Update is called once per frame
protected virtual void Update()
{
// this starts timing when a click occurs
//
if (this._WasClicked == true)
{
this._DoubleClickTimer += Time.deltaTime;
}
// this must be in update because it expires based on time and not clicks
//
if (this._DoubleClickTimer > this._DoubleClickDelay)
{
this._WasClicked = false;
this._DoubleClickTimer = 0.0f;
}
}
protected virtual void OnMouseDoubleClick()
{
}
protected virtual void OnMouseDown()
{
if (this._WasClicked == false && this._DoubleClickTimer < _DoubleClickDelay)
{
this._WasClicked = true;
}
else if (this._WasClicked == true &&
this._DoubleClickTimer < this._DoubleClickDelay)
{
this.StartCoroutine(() => this.OnMouseDoubleClick());
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.