UNITY - 如何对长按和单击 click/double 执行不同的操作?
UNITY - How to perform a different action for Long click and a single click/double click?
所以我正在制作一个 2D 角色控制器,如果按住鼠标(长按),玩家可以设置他想要发射射弹的方向,以及射弹何时发射并与物体发生碰撞玩家可以单击一次(如果更好,则双击)以将角色的位置交换到射弹的位置。我正在尝试使用 Input.onMouseButton 和 Input.onMouseButtonDown 来做到这一点,但我真的想不通。感谢所有帮助我的人!
据我从文档和论坛中得知,您需要使用 Input.GetMouseButtonDown
和自第一次按下后经过的时间来检查,例如 so.
除了获取带有按钮的 GetMouseButtonDown(int) 之外,您要跟踪请记住检查按钮处于哪个阶段。
对于长时间点击,您只需测量自第一次点击后经过的时间。这是一个例子:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class LongClick : MonoBehaviour
{
public float ClickDuration = 2;
public UnityEvent OnLongClick;
bool clicking = false;
float totalDownTime = 0;
// Update is called once per frame
void Update()
{
// Detect the first click
if (Input.GetMouseButtonDown(0))
{
totalDownTime = 0;
clicking = true;
}
// If a first click detected, and still clicking,
// measure the total click time, and fire an event
// if we exceed the duration specified
if (clicking && Input.GetMouseButton(0))
{
totalDownTime += Time.deltaTime;
if (totalDownTime >= ClickDuration)
{
Debug.Log("Long click");
clicking = false;
OnLongClick.Invoke();
}
}
// If a first click detected, and we release before the
// duraction, do nothing, just cancel the click
if (clicking && Input.GetMouseButtonUp(0))
{
clicking = false;
}
}
}
对于双击,您只需要检查是否在指定的(短)间隔内发生第二次点击:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class DoubleClick : MonoBehaviour
{
public float DoubleClickInterval = 0.5f;
public UnityEvent OnDoubleClick;
float secondClickTimeout = -1;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (secondClickTimeout < 0)
{
// This is the first click, calculate the timeout
secondClickTimeout = Time.time + DoubleClickInterval;
}
else
{
// This is the second click, is it within the interval
if (Time.time < secondClickTimeout)
{
Debug.Log("Double click!");
// Invoke the event
OnDoubleClick.Invoke();
// Reset the timeout
secondClickTimeout = -1;
}
}
}
// If we wait too long for a second click, just cancel the double click
if (secondClickTimeout > 0 && Time.time >= secondClickTimeout)
{
secondClickTimeout = -1;
}
}
}
所以我正在制作一个 2D 角色控制器,如果按住鼠标(长按),玩家可以设置他想要发射射弹的方向,以及射弹何时发射并与物体发生碰撞玩家可以单击一次(如果更好,则双击)以将角色的位置交换到射弹的位置。我正在尝试使用 Input.onMouseButton 和 Input.onMouseButtonDown 来做到这一点,但我真的想不通。感谢所有帮助我的人!
据我从文档和论坛中得知,您需要使用 Input.GetMouseButtonDown
和自第一次按下后经过的时间来检查,例如 so.
除了获取带有按钮的 GetMouseButtonDown(int) 之外,您要跟踪请记住检查按钮处于哪个阶段。
对于长时间点击,您只需测量自第一次点击后经过的时间。这是一个例子:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class LongClick : MonoBehaviour
{
public float ClickDuration = 2;
public UnityEvent OnLongClick;
bool clicking = false;
float totalDownTime = 0;
// Update is called once per frame
void Update()
{
// Detect the first click
if (Input.GetMouseButtonDown(0))
{
totalDownTime = 0;
clicking = true;
}
// If a first click detected, and still clicking,
// measure the total click time, and fire an event
// if we exceed the duration specified
if (clicking && Input.GetMouseButton(0))
{
totalDownTime += Time.deltaTime;
if (totalDownTime >= ClickDuration)
{
Debug.Log("Long click");
clicking = false;
OnLongClick.Invoke();
}
}
// If a first click detected, and we release before the
// duraction, do nothing, just cancel the click
if (clicking && Input.GetMouseButtonUp(0))
{
clicking = false;
}
}
}
对于双击,您只需要检查是否在指定的(短)间隔内发生第二次点击:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class DoubleClick : MonoBehaviour
{
public float DoubleClickInterval = 0.5f;
public UnityEvent OnDoubleClick;
float secondClickTimeout = -1;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (secondClickTimeout < 0)
{
// This is the first click, calculate the timeout
secondClickTimeout = Time.time + DoubleClickInterval;
}
else
{
// This is the second click, is it within the interval
if (Time.time < secondClickTimeout)
{
Debug.Log("Double click!");
// Invoke the event
OnDoubleClick.Invoke();
// Reset the timeout
secondClickTimeout = -1;
}
}
}
// If we wait too long for a second click, just cancel the double click
if (secondClickTimeout > 0 && Time.time >= secondClickTimeout)
{
secondClickTimeout = -1;
}
}
}