Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity:如何在c#中双击检测需要帮助吗?_C#_Unity3d_Mouseevent - Fatal编程技术网

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

Unity:如何在c#中双击检测需要帮助吗?,c#,unity3d,mouseevent,C#,Unity3d,Mouseevent,我想创建鼠标点击检测单点击,按住点击,双击。 当我单击时,角色速度会更快(moveSpeed=15),当我按住click并双击时,没有动作,角色速度仍然保持不变(moveSpeed=3) 这是我的密码: private float t0, moveSpeed; private bool longClick, shortClick; void Start () { t0 = 0f; longClick = false; shortClick = false; } void update()

我想创建鼠标点击检测单点击,按住点击,双击。 当我单击时,角色速度会更快(moveSpeed=15),当我按住click并双击时,没有动作,角色速度仍然保持不变(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;   }
    }
}
private float t0,移动速度;
私有布尔长点击,短点击;
无效开始()
{t0=0f;longClick=false;shortClick=false;}
无效更新()
{
//如何添加用于检测双击的代码以及在何处编写?
if(Input.GetMouseButtonDown(0))
{
t0=0;t0+=Time.deltaTime;
longClick=false;shortClick=false;
移动速度=0;
}
if(输入。GetMouseButton(0))
{         
t0+=时间增量时间;
如果(t0<0.2)
{moveSpeed=15;longClick=false;shortClick=true;}//这是一次单击!
如果(t0>0.2)
{moveSpeed=3;longClick=true;shortClick=false;}//这是按住单击!
}
if(Input.GetMouseButtonUp(0))
{
如果(longClick==true)
{moveSpeed=3;}
else if(shortClick=true)
{moveSpeed=3;}
}
}

你尝试过谷歌搜索吗?-请参见第二个答案:

在C#中:

private float lastClickTime;
公共浮球捕集时间=0.25f;
无效更新()
{
if(Input.GetButtonDown(“Fire1”))
{
if(Time.Time-lastClickTime
我在这里回答:

基本上:创建一个包含双击检查器的新类。然后在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。 我还包括了一个MonoBehavior扩展,它使呼叫和 执行启动例行程序:

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());
            }
        }
    }
}
名称空间双击
{
公共静态类扩展
{
公共静态void startcroutine(此函数为mb,参数Action[]funcs)
{ 
mb.start例程(coroutinernersimple(funcs));
}
私有静态System.Collections.IEnumerator CoroutineRunerSimple(操作[]函数)
{
foreach(funcs中的var func)
{
如果(func!=null)
func();
返回新的WaitForSeconds(.01f);
}
}
}
抽象公共类DoubleClickBehaviorBase:MonoBehavior
{
浮动_双击定时器=0.0f;
浮动_双击延迟=0.5f;
bool\u WasClicked=false;
//每帧调用一次更新
受保护的虚拟无效更新()
{
//这将在单击发生时开始计时
//
if(this.\u WasClicked==true)
{
这是。_双击定时器+=Time.deltaTime;
}
//这必须在更新中,因为它根据时间而不是单击过期
//
如果(此.\u双击计时器>此.\u双击延迟)
{
这一点。_WasClicked=false;
这是。_双击定时器=0.0f;
}
}
受保护的虚拟void onMouseBooleClick()
{
}
受保护的虚拟void OnMouseDown()
{
如果(this.\u WasClicked==false和this.\u DoubleClickTimer<\u DoubleClickDelay)
{
这是。_WasClicked=true;
}
如果(this.\u已单击==true&&
此。_双击计时器<此。_双击延迟)
{
this.startcroutine(()=>this.onmousedubleclick());
}
}
}
}
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());
            }
        }
    }
}