繁体   English   中英

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

[英]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.

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