一.Unity编辑器中EventTrigger组件用法
1.添加事件类型
2.绑定gameObject指定组件的方法
3.方法执行逻辑
public class NewBehaviourScript : MonoBehaviour
{
public void PointerDown()
{
Debug.Log("Trigger PointerDown");
}
}
4.按下鼠标,绑定方法成功调用
二.代码使用EventTrigger组件
private void addEventTriggerType(EventTriggerType eventTriggerType, UnityAction<BaseEventData> callback)
{
//设置事件类型和回调
EventTrigger.Entry entryObj = new EventTrigger.Entry ();
entryObj.eventID = eventTriggerType;
entryObj.callback.AddListener(callback);
//将设置好的对象和组件关联起来
EventTrigger componentET = gameObject.AddComponent<EventTrigger> ();
componentET.triggers.Add(entryObj);
}
void Start()
{
addEventTriggerType(EventTriggerType.PointerDown, codeTriggerPointerDown);
}
public void codeTriggerPointerDown(BaseEventData data)
{
Debug.Log("codeTriggerPointerDown");
}