在网上找的程序基本上大概都是这么写的:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class Test : MonoBehaviour {
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("点击");
}
}
然而运行时加断点看了一下:
看起来myclick.callback.AddListener(click);这句似乎没能成功运行。。。
而且运行结束之后,组件状态是这样的:
环境是unity2d,5.6版本,所以我这里还有什么没考虑到的嘛?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class Test : MonoBehaviour {
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("点击");
}
}
然而运行时加断点看了一下:
看起来myclick.callback.AddListener(click);这句似乎没能成功运行。。。
而且运行结束之后,组件状态是这样的:
环境是unity2d,5.6版本,所以我这里还有什么没考虑到的嘛?