改方法主要是用于按钮检测鼠标的进入、滑出、点击、抬起、长按、长按停止
1:先将下面这个脚本挂载到需要检测鼠标状态的按钮上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtonEvent : MonoBehaviour
{
public bool isClickDown = false;
public bool isClickUp = true;
void Start()
{
}
void Update()
{
if (m_down && Time.time - _time > 2f)//按下2秒属于长按
{
m_down = false;
Debug.Log("长按中");
}
}
public void MouseEnter()
{
Debug.Log("鼠标进入");
}
public void MouseExit()
{
Debug.Log("鼠标滑出");
}
public void MouseDown()
{
isClickDown = true;
isClickUp = false;
//MouseDownLong(true);//用于长按
}
public void MouseUp()
{
isClickDown = false;
isClickUp = true;
//MouseDownLong(false);//用于长按终止
}
public void MouseClick()
{
Debug.Log("点击");
}
float _time;
bool m_down = false;
void MouseDownLong(bool _isdown)
{
m_down = _isdown;
if (_isdown)
{
Debug.Log("开始长按");
_time = Time.time;
}
else if (_time != 0)
{
_time = 0;
Debug.Log("停止长按");
}
}
}
2:首先,对需要进行检测的按钮上添加EventTrigger组件,然后添加需要检测的事件类型,比如说要检测鼠标的按下,那么就要添加PointerDown,然后将上面脚本里面的MouseDown方法赋值过去。