使用一
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class ETListener : UnityEngine.EventSystems.EventTrigger
{
public delegate void VoidDelegate();
public delegate void EventDelegate(BaseEventData baseEventData);
public delegate void BoolDelegate(bool state);
public delegate void FloatDelegate(float delta);
public delegate void VectorDelegate(Vector2 delta);
public delegate void ObjectDelegate(GameObject obj);
public delegate void KeyCodeDelegate(KeyCode key);
public VoidDelegate onClick;
public EventDelegate onEventClick;
public VoidDelegate onDown;
public VoidDelegate onEnter;
public VoidDelegate onExit;
public VoidDelegate onUp;
public VoidDelegate onSelect;
public VoidDelegate onUpdateSelect;
static public ETListener Get(Transform transform)
{
ETListener listener = transform.GetComponent<ETListener>();
if (listener == null)
listener = transform.gameObject.AddComponent<ETListener>();
return listener;
}
public override void OnPointerClick(PointerEventData eventData)
{
if (onClick != null)
{
onClick();
}
if (onEventClick != null)
{
onEventClick(eventData);
}
}
public override void OnPointerDown(PointerEventData eventData)
{
if (onDown != null)
onDown();
}
public override void OnPointerEnter(PointerEventData eventData)
{
if (onEnter != null)
onEnter();
}
public override void OnPointerExit(PointerEventData eventData)
{
if (onExit != null)
onExit();
}
public override void OnPointerUp(PointerEventData eventData)
{
if (onUp != null)
onUp();
}
public override void OnSelect(BaseEventData eventData)
{
if (onSelect != null)
onSelect();
}
public override void OnUpdateSelected(BaseEventData eventData)
{
if (onUpdateSelect != null)
onUpdateSelect();
}
}
public Button click;
void Start () {
EventTriggerListener.Get(click.transform).onClick += Click;
EventTriggerListener.Get(click.transform).onDown += ClickDela;
}
public void Click ()
{
bar = this.GetComponent<Scrollbar>();
bar.value = 1;
}
public void ClickDela()
{
bar = this.GetComponent<Scrollbar>();
bar.value = 0.5f;
}
使用2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public abstract class EventTriggerListener : MonoBehaviour
{
public abstract EventTriggerType EventType { get; }
void Awake()
{
AddTriggersListener();
}
private void AddTriggersListener()
{
AddTriggersListener(gameObject);
}
public abstract void CallAction(BaseEventData eventData);
/// <summary>
/// 为obj添加Eventrigger监听事件
/// </summary>
/// <param name="obj">添加监听的对象</param>
/// <param name="eventID">添加的监听类型</param>
/// <param name="action">触发的函数</param>
private void AddTriggersListener(GameObject obj)
{
//首先判断对象是否已经有EventTrigger组件,若没有那么需要添加
EventTrigger trigger = obj.GetComponent<EventTrigger>();
if (trigger == null)
{
trigger = obj.AddComponent<EventTrigger>();
}
//实例化delegates
if (trigger.triggers.Count == 0)
{
trigger.triggers = new List<EventTrigger.Entry>();//
}
//定义所要绑定的事件类型
EventTrigger.Entry entry = new EventTrigger.Entry();
//设置事件类型
entry.eventID = EventType;
//定义回调函数
UnityAction<BaseEventData> callback = new UnityAction<BaseEventData>(CallAction);
//设置回调函数
entry.callback.AddListener(callback);
//添加事件触发记录到GameObject的事件触发组件
trigger.triggers.Add(entry);
}
}
使用时继承该类