定义
void binding_call(GameObject target, string id, System.Action<string> callback){
EventTrigger trigger = target.GetComponent<EventTrigger>();
if(!trigger) trigger = target.AddComponent<EventTrigger>();
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerClick;
entry.callback.AddListener( (eventData) => { callback(id); } );
trigger.triggers.Add(entry);
}
调用
点击后会得到id值
binding_call(unit,idstr,(string id)=>{
// Debug.Log(id);
});
参考:
https://answers.unity.com/questions/854251/how-do-you-add-an-ui-eventtrigger-by-script.html