Reputation: 45
I've written the following code:
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);
}
}
It detects double-click But has a problem!
in Both, objects will be removed
When you first click happens, Object is deleted And the second click is not recognized!
I want for Android
please help me. thanks
Upvotes: 1
Views: 5340
Reputation: 159
You're telling it to destroy the object in both halves of your if-statement (re-read your inner else
block). You would need to set up your update method or coroutine to process a single click after the Double Click timer has been reached.
Simple sample below:
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;
}
}
}
Note this is just to demonstrate a simple way to handle this using most of what you provided.
You can also find additional information at this question: http://answers.unity3d.com/questions/331545/double-click-mouse-detection-.html
Upvotes: 1
Reputation: 10721
The following class can be used in Editor or device.
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
}
Upvotes: 2