1.长按按钮
将下面的代码挂载到按钮上,去掉按钮本射的Button组件
using System.Collections;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;public class LongPressButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{public UnityEvent OnPressDown;bool _isPressed = false;IEnumerator DetectPress(){while(_isPressed){if(OnPressDown != null){OnPressDown.Invoke();}yield return new WaitForSeconds(0.1f);}}public void OnPointerDown(PointerEventData eventData){_isPressed = true;StartCoroutine(DetectPress());}public void OnPointerUp(PointerEventData eventData){_isPressed = false;}
}
2.双击按钮
将下面的代码挂载到需要双击的图片上,如双击全屏
using UnityEngine;
using UnityEngine.EventSystems;public class DoubleClickHandler : MonoBehaviour, IPointerClickHandler
{public delegate void OnDoubleClick();public OnDoubleClick OnDoubleClickHandle;public float clickInterval = 0.3f; // 连续点击间隔阈值private float lastClickTime;public void OnPointerClick(PointerEventData eventData){float timeSinceLastClick = UnityEngine.Time.time - lastClickTime;if (timeSinceLastClick <= clickInterval){if(OnDoubleClickHandle != null){OnDoubleClickHandle();}}lastClickTime = UnityEngine.Time.time;}
}