Reputation: 43
I am new here and I'm beginning my adventure with UNITY. I have problem with double click event. I'd like to buying or selling something in my shop. When I assign a button on unity (public Button button;
) earlier it works. But when i try this change to button on Start and Update methods:
void Start () {
button = GameObject.Find(EventSystem.current.currentSelectedGameObject.name).GetComponent<Button>();
button.onClick.AddListener(ButtonListner);
}
void Update()
{
button = GameObject.Find(EventSystem.current.currentSelectedGameObject.name).GetComponent<Button>();
}
private void ButtonListner()
{
counter++;
if (counter == 1)
{
StartCoroutine("doubleClickEvent");
}
}
IEnumerator doubleClickEvent()
{
yield return new WaitForSeconds(clickTimer);
if (counter > 1)
{...}
the method doubleClickEvent() doesnt work unfortunately... What shall I do? Regards ;)
Upvotes: 3
Views: 18022
Reputation: 31
it's a weird issue in Unity but...it's a simple issue...it can be solved by adding and removing the listener. This should work.
private void OnEnable()
{
button.onClick.AddListener(ButtonListner);
}
private void OnDisable()
{
button.onClick.RemoveListener(ButtonListner);
}
Upvotes: 0
Reputation: 41
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
[RequireComponent(typeof(Button))]
public class DoubleClickButton : MonoBehaviour
{
[Header("Click Intervar")]
[SerializeField] private float interval = 0.4f;
[Space(10)]
[SerializeField] private UnityEvent doubleTapEvent;
private void Awake()
{
GetComponent<Button>().onClick.AddListener(OnClick);
}
private float lastClick = 0f;
private void OnClick()
{
if ((lastClick + interval) > Time.time) doubleTapEvent?.Invoke();
lastClick = Time.time;
}
}
Upvotes: 0
Reputation: 13
You can detect double click without a coroutine like the following:
// Choose the time you want between clicks to consider it a double click
float doubleClickTime = .2f, lastClickTime;
void Update()
{
// Checking left mouse button click, you could choose the input you want here
if (Input.GetMouseButtonDown(0))
{
float timeSinceLastClick = Time.time - lastClickTime;
if (timeSinceLastClick <= doubleClickTime)
Debug.Log("Double click");
else
Debug.Log("Normal click");
lastClickTime = Time.time;
}
}
Upvotes: 1
Reputation: 125455
First thing I noticed was: button = GameObject.Find(EventSystem.current.currentSelectedGameObject.name).GetComponent<Button>();
The EventSystem.current.currentSelectedGameObject
property can be null
at anytime especially in the first frame which means that using it in the Start
function is not a good idea. Find the Button
GameObject then get the Button
component from it:
Button button;
void Start()
{
button = GameObject.Find("YourButtonName").GetComponent<Button>();
button.onClick.AddListener(ButtonListner);
}
Replace "YourButtonName"
with the name of your Button
GameObject.
You don't even need to do most of the stuff you did. You can get double click or click count with PointerEventData.clickCount
from the OnPointerClick
function. You must implement the IPointerClickHandler
interface for this to work.
Simply attach to the Button
GameObject:
public class ClickCountDetector : MonoBehaviour, IPointerClickHandler
{
public void OnPointerClick(PointerEventData eventData)
{
int clickCount = eventData.clickCount;
if (clickCount == 1)
OnSingleClick();
else if (clickCount == 2)
OnDoubleClick();
else if (clickCount > 2)
OnMultiClick();
}
void OnSingleClick()
{
Debug.Log("Single Clicked");
}
void OnDoubleClick()
{
Debug.Log("Double Clicked");
}
void OnMultiClick()
{
Debug.Log("MultiClick Clicked");
}
}
Upvotes: 2