博客将会介绍Unity3D实现UI的单击、双击、拖动状态判断 希望这篇博客对Unity的开发者有所帮助。 大家好,我是心疼你的一切,不定时更新Unity开发技巧,觉得有用记得一键三连哦。 欢迎点赞评论哦.下面就让我们进入正文吧 ! 这篇文章就来实现UI的单击、双击、按压、拖动的不同状态判断。不定时更新Unity开发技巧,觉得有用记得一键三连哦。
示例、
if (Input.GetMouseButtonDown (0)) //左键点击
{
}
if (Input.GetMouseButtonDown(1)) //右键点击
{
}
if (Input.GetMouseButtonDown(2)) //中键点击
{
}判断单击和双击,主要是判断点击的次数。
UI的点击事件,需要继承UI的点击事件接口,重写点击事件即可。 UI点击事件接口:
public class Btn_OnClick : MonoBehaviour,IPointerClickHandler,IPointerDownHandler,IPointerUpHandler,IPointerExitHandlerusing UnityEngine.EventSystems;示例:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Btn_OnClick : MonoBehaviour,IPointerClickHandler,IPointerDownHandler,IPointerUpHandler,IPointerExitHandler
{
public void OnPointerClick(PointerEventData eventData) //鼠标点击UI
{
}
public void OnPointerDown(PointerEventData eventData) //鼠标按下UI
{
}
public void OnPointerExit(PointerEventData eventData) //鼠标离开UI
{
}
public void OnPointerUp(PointerEventData eventData)//鼠标点击UI后抬起
{
}
}知道了API,下面就在这个基础上进行修改
代码如下(示例):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
public class Btn_OnClick : MonoBehaviour,IPointerClickHandler,IPointerDownHandler,IPointerUpHandler,IPointerExitHandler
{
// 按压的持续时间
public float pressDurationTime = 1;
// 按压的响应次数
public bool responseOnceByPress = false;
// 双击的间隔时间
public float doubleClickIntervalTime = 0.2f;
// 拖动的间隔时间
public float dragIntervalTime = 0.2f;
// 拖动的鼠标间隔距离
public float dragIntervalPos = 0.01f;
public UnityEvent onDoubleClick;
public UnityEvent onPress;
public UnityEvent onClick;
public UnityEvent onDrag;
private bool isDown = false;
private bool isPress = false;
private bool isDrag = false;
private float downTime = 0;
private float clickIntervalTime = 0;
private int clickTimes = 0;
private Vector3 mousePosLast = Vector3.zero;//点击后的拖动位置
Btn_OnClick btn;
void Start()
{
btn = GetComponent<Btn_OnClick>();
btn.onClick.AddListener(Click);
btn.onPress.AddListener(Press);
btn.onDoubleClick.AddListener(DoubleClick);
btn.onDrag.AddListener(Drag);
}
void Click()
{
Debug.Log("单击");
}
void Press()
{
Debug.Log("按压");
}
void DoubleClick()
{
Debug.Log("双击");
}
void Drag()
{
Debug.Log("拖动");
}
void Update()
{
if (isDown)
{
if (responseOnceByPress && isPress)
{
return;
}
downTime += Time.deltaTime;
isDrag = Vector3.Distance(Input.mousePosition, mousePosLast) > dragIntervalPos;
if (downTime > pressDurationTime && !isDrag)
{
isPress = true;
onPress.Invoke();
}
if (downTime > dragIntervalTime && isDrag)
{
onDrag.Invoke();
}
}
if (clickTimes >= 1)
{
clickIntervalTime += Time.deltaTime;
if (clickIntervalTime >= doubleClickIntervalTime)
{
if (clickTimes >= 2)
{
onDoubleClick.Invoke();
}
else
{
onClick.Invoke();
}
clickTimes = 0;
clickIntervalTime = 0;
}
}
}
public void OnPointerClick(PointerEventData eventData)
{
if (!isPress)
clickTimes += 1;
else
isPress = false;
}
public void OnPointerDown(PointerEventData eventData)
{
isDown = true;
downTime = 0;
mousePosLast = Input.mousePosition;
}
public void OnPointerExit(PointerEventData eventData)
{
isDown = false;
isPress = false;
}
public void OnPointerUp(PointerEventData eventData)
{
isDown = false;
}
}



完整代码示例如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 脚本挂到模型上,模型必须有碰撞盒
/// </summary>
public class Cube_OnClick : MonoBehaviour
{
/// <summary>
/// 当鼠标进入碰撞器的时候触发
/// </summary>
private void OnMouseEnter()
{
Debug.Log("进入了");
}
/// <summary>
///当鼠标离开碰撞盒的时候触发
/// </summary>
private void OnMouseExit()
{
Debug.Log("离开了");
}
/// <summary>
/// 当鼠标在碰撞器上按下并松开的时候触发
/// </summary>
private void OnMouseUpAsButton()
{
Debug.Log("点击了模型");
}
}UnityUI点击模型点击
补充: 注意事项
private IEnumerator ClickCheck() {
yield return new WaitForSeconds(0.3f);
if (!_isDoubleClick) {
Debug.Log("单击触发");
}
_isDoubleClick = false;
}拖动与点击冲突: 拖动操作可能误触发点击事件,可通过判断拖动距离阈值(eventData.delta)或拖动时间阈值区分。
使用对象池或缓存机制优化频繁的UI更新。
输入兼容性: 同时支持鼠标和触摸输入时,需统一处理 PointerEventData.InputButton。
Canvas层级: 调整Canvas的 Sort Order 或使用 GraphicRaycaster 控制事件优先级。
总结 核心思路:通过Unity事件接口监听输入,结合时间、距离阈值和状态标记区分不同操作。
关键点:解决事件冲突、优化性能、多平台适配。
扩展方向:结合Unity的 Input System 实现更复杂的交互(如长按、滑动)。
如果觉得本篇文章有用别忘了点个关注,关注不迷路,持续分享更多Unity干货文章。 你的点赞就是对博主的支持,有问题记得评论留言 本次总结的就是Unity3D实现UI的单击、双击、拖动状态判断, 有需要会继续增加功能 如能帮助到你,就帮忙点个赞吧,三连更好哦,谢谢 你的点赞就是对博主的支持,有问题记得留言评论哦! 不定时更新Unity开发技巧,觉得有用记得一键三连哦。么么哒!