如何检测统一单击和双击?

时间:2017-05-03 22:45:38

标签: c# unity3d unity5

我已经写了以下代码:

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

    }

它检测到双击但有问题!

在两者中,对象将被删除

首次点击时,会删除对象并且无法识别第二次点击!

我想要Android

请帮帮我。感谢

2 个答案:

答案 0 :(得分:2)

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

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
}

答案 1 :(得分:1)

你告诉它在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;
       }
    }
}

请注意,这只是为了演示一种使用您提供的大部分内容来处理此问题的简单方法。

您还可以在此问题中找到其他信息: http://answers.unity3d.com/questions/331545/double-click-mouse-detection-.html