C# 如何在unity中检测单点和双击?

C# 如何在unity中检测单点和双击?,c#,unity3d,unity5,C#,Unity3d,Unity5,我编写了以下代码: void OnMouseUpAsButton() { if (Type == 0) { if (click && Time.time <= (clickTime + clickDelta)) { Debug.Log("Double"); Destroy(sp); c

我编写了以下代码:

void OnMouseUpAsButton()
    {
        if (Type == 0) {            
            if (click && Time.time <= (clickTime + clickDelta))
            {
                Debug.Log("Double");
                Destroy(sp);
                click = false;
            }
            else
            {
                Debug.Log("one");
                click = true;
                clickTime = Time.time;
                Destroy(sp);
            }
        }else if (Type == -1)
        {            
                Destroy(sp);                                 
        }

    }
void OnMouseUpAsButton()
{
如果(类型==0){

if(单击&&Time.Time您告诉它销毁if语句两半中的对象(重新读取内部的
else
块)。您需要设置更新方法或协同程序,以便在到达双击计时器后处理单次单击

简单示例如下:

void OnMouseUpAsButton()
{
    if(!clicked)
    {
        clicked = true;
        return;
    }

    if(Time.time <= (clickTime + clickDelta))
    {
        //Double Click occured
        clicked = false;
    }
}

void Update()
{
    if(clicked)
    {
       if(Time.time >= (clickTime + clickDelta))
       {
           //Handle single click
           clicked = false;
       }
    }
}
void OnMouseUpAsButton()
{
如果(!单击)
{
单击=真;
返回;
}
如果(Time.Time=(单击时间+单击增量))
{
//处理单点击
单击=假;
}
}
}
请注意,这只是为了演示一种使用您提供的大部分内容处理此问题的简单方法

您还可以在此问题中找到其他信息:

以下类可在编辑器或设备中使用

public class InputController : MonoBehaviour
{
    public event Action OnSingleTap;
    public event Action OnDoubleTap;
    [Tooltip("Defines the maximum time between two taps to make it double tap")]
    [SerializeField]private float tapThreshold = 0.25f;
    private Action updateDelegate;
    private float tapTimer = 0.0f;
    private bool tap = false;

    private void Awake()
    {
#if UNITY_EDITOR || UNITY_STANDALONE
        updateDelegate = UpdateEditor;
#elif UNITY_IOS || UNITY_ANDROID
        updateDelegate = UpdateMobile;
#endif
    }
    private void Update()
    {
        if(updateDelegate != null){ updateDelegate();}
    }
    private void OnDestroy()
    {
        OnSingleTap = null;
        OnDoubleTap = null;
    }
#if UNITY_EDITOR || UNITY_STANDALONE
    private void UpdateEditor()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (Time.time < this.tapTimer + this.tapThreshold)
            {
                if(OnDoubleTap != null){ OnDoubleTap(); }
                this.tap = false;
                return;
            }
            this.tap = true;
            this.tapTimer = Time.time;
        }
        if (this.tap == true && Time.time>this.tapTimer + this.tapThreshold)
        {
             this.tap = false;
             if(OnSingleTap != null){ OnSingleTap();}
    }
    }
#elif UNITY_IOS || UNITY_ANDROID
    private void UpdateMobile ()
    {
        for(int i = 0; i < Input.touchCount; i++)
        {
            if (Input.GetTouch(i).phase == TouchPhase.Began)
            {
                if(Input.GetTouch(i).tapCount == 2)
                {
                    if(OnDoubleTap != null){ OnDoubleTap();}
                }
                if(Input.GetTouch(i).tapCount == 1)
                {
                    if(OnSingleTap != null) { OnSingleTap(); }
                }
            }
        }
    }
#endif
}
公共类InputController:MonoBehavior
{
针对SingleTap的公共活动行动;
双塔公共事件行动;
[工具提示(“定义两次点击之间的最长时间,使其成为双击”)]
[SerializeField]专用浮点tapThreshold=0.25f;
私人行动更新公使;
专用浮点tapTimer=0.0f;
私有布尔抽头=假;
私人空间
{
#如果UNITY|U编辑器| UNITY|U独立
updateDelegate=UpdateEditor;
#elif UNITY|u IOS | UNITY|u ANDROID
updateDelegate=UpdateMobile;
#恩迪夫
}
私有void更新()
{
如果(updateLegate!=null){updateLegate();}
}
私有void OnDestroy()
{
OnSingleTap=null;
OnDoubleTap=null;
}
#如果UNITY|U编辑器| UNITY|U独立
私有void UpdateEditor()
{
if(Input.GetMouseButtonDown(0))
{
if(Time.Timethis.tapTimer+this.tapThreshold)
{
this.tap=false;
如果(OnSingleTap!=null){OnSingleTap();}
}
}
#elif UNITY|u IOS | UNITY|u ANDROID
私有void UpdateMobile()
{
对于(int i=0;i
除非您是此脚本的原始作者,否则请提供.Unitygems所属的适当属性。此外,如果您要将
OnSingleTap
更改为
OnSingleTaHandler
OnDoubleTap
更改为
OnDoubleTapHandler
,则整个脚本中的属性都应保持一致。我是源代码我是这篇文章的作者。不确定我如何证明它,但我是。好的,我改变了原始代码,所以它不再是原来的:)。