简体   繁体   English

Unity:需要帮助如何在c#中双击检测?

[英]Unity: Need help how to double click detection in c#?

I wanna create mouse click that detect single click, hold click, and double click. 我想创建鼠标单击,检测单击,按住单击,然后双击。 When I do single click, the character will faster (moveSpeed = 15), when I hold click and double click there is no action, the character still constant with it speed (moveSpeed = 3). 当我单击时,角色会更快(moveSpeed = 15),当我按住单击并双击没有动作时,角色仍然保持恒定速度(moveSpeed = 3)。

Here is my code: 这是我的代码:

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;   }
    }
}

Have you tried googling? 你试过谷歌吗? - See the second answer here: http://answers.unity3d.com/questions/331545/double-click-mouse-detection-.html - 请参阅第二个答案: http//answers.unity3d.com/questions/331545/double-click-mouse-detection-.html

In C#: 在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;
    }
}

I replied here: http://answers.unity3d.com/answers/1133702/view.html 我在这里回复: http//answers.unity3d.com/answers/1133702/view.html

Basically: create a new class with the doubleClicked checker inside. 基本上:使用里面的doubleClicked检查器创建一个新类。 Then use it in an if clause. 然后在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 is a base class that will take care of double click for you. DoubleClickBehaviorBase是一个基类,可以为您进行双击。 You inherit from DoubleClickBehaviorBase instead of MonoBehavior All you need to do is override OnDoubleClickOverride in your class. 您继承自DoubleClickBehaviorBase而不是MonoBehavior您需要在类中重写OnDoubleClickOverride。 I've also included a MonoBehaviourExtension that makes it easier to call and implement StartCoroutine: 我还添加了一个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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM