先创建一个Button,然后给他加上自动触发的代码
if (GUILayout.Button("Auto Button"))
{
ExecuteEvents.Execute<IPointerClickHandler>(uiButton.gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerClickHandler);
//按钮点击的变色
ExecuteEvents.Execute<ISubmitHandler>(uiButton.gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.submitHandler);
}
有哪些Handler可以去这里查 API
为了验证是否有调到点击的回调,我给按钮加了个Debug信息,在Awake中给他绑上点击的回调
uiButton.onClick.AddListener(delegate () {
Test1(uiButton.gameObject);
});
然后运行却发现回调调用了两次
经过测试发现原来每调用一次ExecuteEvents.Execute,就会执行一次回调,所以如果是默认的按钮点击变色的效果,只要调用一次 ExecuteEvents.Execute<ISubmitHandler>(uiButton.gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.submitHandler); 就可以了。
点击image,跟点击按钮类似,只是由于image没有自带OnClick,需要给它一个Click,给它绑上Event Trigger。如图:
if (GUILayout.Button("Auto Image"))
{
ExecuteEvents.Execute<IPointerClickHandler>(uiImage.gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerClickHandler);
}
转自:雨松momo