赞
踩
这篇文章就来实现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,IPointerExitHandler
using 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点击模型点击
如果觉得本篇文章有用别忘了点个关注,关注不迷路,持续分享更多Unity干货文章。
你的点赞就是对博主的支持,有问题记得评论留言
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。