Для длинного клика вам просто нужно измерить количество времени, прошедшего с первого клика.Вот пример:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class LongClick : MonoBehaviour
{
public float ClickDuration = 2;
public UnityEvent OnLongClick;
bool clicking = false;
float totalDownTime = 0;
// Update is called once per frame
void Update()
{
// Detect the first click
if (Input.GetMouseButtonDown(0))
{
totalDownTime = 0;
clicking = true;
}
// If a first click detected, and still clicking,
// measure the total click time, and fire an event
// if we exceed the duration specified
if (clicking && Input.GetMouseButton(0))
{
totalDownTime += Time.deltaTime;
if (totalDownTime >= ClickDuration)
{
Debug.Log("Long click");
clicking = false;
OnLongClick.Invoke();
}
}
// If a first click detected, and we release before the
// duraction, do nothing, just cancel the click
if (clicking && Input.GetMouseButtonUp(0))
{
clicking = false;
}
}
}
Для двойного щелчка вам просто нужно проверить, происходит ли второй щелчок в течение указанного (короткого) интервала:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class DoubleClick : MonoBehaviour
{
public float DoubleClickInterval = 0.5f;
public UnityEvent OnDoubleClick;
float secondClickTimeout = -1;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (secondClickTimeout < 0)
{
// This is the first click, calculate the timeout
secondClickTimeout = Time.time + DoubleClickInterval;
}
else
{
// This is the second click, is it within the interval
if (Time.time < secondClickTimeout)
{
Debug.Log("Double click!");
// Invoke the event
OnDoubleClick.Invoke();
// Reset the timeout
secondClickTimeout = -1;
}
}
}
// If we wait too long for a second click, just cancel the double click
if (secondClickTimeout > 0 && Time.time >= secondClickTimeout)
{
secondClickTimeout = -1;
}
}
}