Eventtrigger
Eventtrigger
这个代码回头再看看
trigger需要在使用前初始化列表
任务53:203-EventTrigger实现事件调用的两种方式演示
通过脚本添加EventTrigger方法的,要添加一行trigger.triggers.add(entry) 把新增的事件添加到事件集合中,否则点击之后不会响应。因为没有添加到事件集合。各位同学在这个地方要注意,自行添加一下。
代码手动添加响应事件
EventTrigger trigger=gameObject.AddComponent<EventTrigger>();
trigger.triggers=new List<EventTrigger.Event>();
EventTrigger.Entry entry=new EventTrigger.Entry();
entry.eventId=EventTriggerType.PointerClick();
entry.callback=new EventTrigger.TriggerEvent();
entry.callback.AddListener((data)=>ChangeColor());
void Start()
{
UnityAction<BaseEventData> click = new UnityAction<BaseEventData> (MyClick);
EventTrigger.Entry myclick = new EventTrigger.Entry ();
myclick.eventID = EventTriggerType.PointerClick;
myclick.callback.AddListener (click);
EventTrigger trigger = gameObject.AddComponent<EventTrigger> ();
trigger.triggers.Add (myclick);
}
public void MyClick(BaseEventData data)
{
Debug.Log ("点击");
}