2017-05-03 98 views
0

我已經寫了下面的代碼:如何檢測統一中的單擊和雙擊?

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

    } 

它可以檢測雙擊,但有一個問題!

兩個

,對象將被刪除

當你第一次點擊情況,對象被刪除,第二次點擊也不會承認!

我想要安卓系統

請幫幫我。謝謝

回答

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

1

下面的類可以在編輯器或設備來使用。

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 
} 
+0

除非您是該腳本的原始作者,請爲[原始來源](https://unitygem.wordpress.com/single-double-tap/)提供適當的歸屬。 Unitygems屬於[創意共享](https://creativecommons.org/licenses/by/4.0/)。此外,如果您要將'OnSingleTap'更改爲'OnSingleTapHandler'並將'OnDoubleTap'更改爲'OnDoubleTapHandler',則它應該在整個腳本中保持一致。 – Jerdak

+1

我是這篇文章的原始作者。不知道我怎麼能證明它,但是我是。 – Everts

+0

好吧,我改變了原來的代碼,所以它不再是一樣的:)。 – Everts