1、封装好方法:
private delegate void TriggerEventMehod( BaseEventData pd );
private void AddTriggersListener( GameObject obj, EventTriggerType eventTriggerType, TriggerEventMehod triggerEventMehod )
{
EventTrigger ET = obj.GetComponent();
if ( ET == null )
{
ET = obj.AddComponent();
}
if ( ET.triggers.Count == 0 )
{
ET.triggers = new List<EventTrigger.Entry>();
}
UnityAction<BaseEventData> callBack = new UnityAction<BaseEventData>( triggerEventMehod );
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = eventTriggerType;
entry.callback.AddListener( callBack );
ET.triggers.Add( entry );
}
2、调用
AddTriggersListener(img.gameObject,EventTriggerType.PointerClick,MyFunction01);
void MyFunction01(BaseEventData data)
{
Debug.Log(“click event1”);
}