鼠标的监听脚本
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger
{
public delegate void VoidDelegate(GameObject go);
public delegate void BoolDelegate(GameObject go, bool state);
public delegate void FloatDelegate(GameObject go, float delta);
public delegate void VectorDelegate(GameObject go, Vector2 delta);
public delegate void ObjectDelegate(GameObject go, GameObject obj);
public delegate void KeyCodeDelegate(GameObject go, KeyCode key);
/// <summary>
/// 单机 ~按下和释放在同一对象
/// </summary>
public VoidDelegate onClick;
/// <summary>
/// 指针按下
/// </summary>
public VoidDelegate onDown;
/// <summary>
/// 指针进入
/// </summary>
public VoidDelegate onEnter;
/// <summary>
/// 指针退出
/// </summary>
public VoidDelegate onExit;
/// <summary>
/// 指针释放
/// </summary>
public VoidDelegate onUp;
/// <summary>
/// 调用对象成为选定的对象
/// </summary>
public VoidDelegate onSelect;
public VoidDelegate onUpdateSelect;
/// <summary>
/// 双击 单双击同时又得花,第一点击单击启动
/// </summary>
public VoidDelegate onDoubleClick;
/// <summary>
/// 长按
/// </summary>
public VoidDelegate onLongPress;
//双击
private float endtime = 0;
private float Doubletime = 0.5f; //响应时间
// 长按
public float durationThreshold = 1.0f; //响应时间
private bool bDown = false;
private int nLangDelegate = -1;
private float fLangtime = 0;
static public EventTriggerListener Get(GameObject go)
{
EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
if (listener == null) listener = go.AddComponent<EventTriggerListener>();
return listener;
}
static public EventTriggerListener Get(Transform transform)
{
EventTriggerListener listener = transform.GetComponent<EventTriggerListener>();
if (listener == null) listener = transform.gameObject.AddComponent<EventTriggerListener>();
return listener;
}
// public override void OnPointerClick(PointerEventData eventData)
// {
// if (onClick != null) onClick(gameObject);
// }
public override void OnPointerDown(PointerEventData eventData)
{
if (onDown != null)
{
onDown(gameObject);
return;
}
// 长按
fLangtime = Time.realtimeSinceStartup;
bDown = true;
nLangDelegate = 0;
}
public override void OnPointerEnter(PointerEventData eventData)
{
if (onEnter != null)
onEnter(gameObject);
}
public override void OnPointerExit(PointerEventData eventData)
{
if (onExit != null)
onExit(gameObject);
bDown = false;
nLangDelegate = -1;
}
public override void OnPointerUp(PointerEventData eventData)
{
if (onUp != null)
onUp(gameObject);
bDown = false;
if (nLangDelegate > 0)
nLangDelegate = 1;
}
public override void OnSelect(BaseEventData eventData)
{
if (onSelect != null)
onSelect(gameObject);
}
public override void OnUpdateSelected(BaseEventData eventData)
{
if (onUpdateSelect != null)
onUpdateSelect(gameObject);
}
public override void OnPointerClick(PointerEventData eventData)
{
//长按执行,就屏蔽点击,跟双击
if (nLangDelegate > 0)
return;
// 双击
float time = Time.realtimeSinceStartup;
if ((endtime + Doubletime) > time)
{
if (onDoubleClick != null)
onDoubleClick(gameObject);
return;
}
endtime = time;
//单机
if (onClick != null)
{
onClick(gameObject);
return;
}
}
private void Update()
{
if (bDown && nLangDelegate >= 0)
{
if (Time.realtimeSinceStartup - fLangtime > durationThreshold)
{
nLangDelegate++;
if (onLongPress != null)
onLongPress(gameObject);
}
}
}
}