一般来说UGUI里的Button 触发是通过鼠标选中单击然后触发的。那么怎么自定义按键触发呢。
直接上代码然后可以参考下方法:
using UnityEngine;
using UnityEngine.EventSystems;
public class MyUIButtonKeyTrigger : MonoBehaviour
{
public KeyCode myKey = KeyCode.None;
public string buttonName = string.Empty;
void Awake()
{
button = GetComponent<Button>();
if (button == null) this.enabled = false;
}
void Update()
{
if(Input.GetKeyDown(myKey)||(!string.IsNullOrEmpty(buttonName)
{var pointer = new PointerEventData(EventSystem.current);
ExecuteEvents.Execute(button.gameObject, pointer, ExecuteEvents.submitHandler);
}
}
}
把这个脚本直接挂在有Button的组件上即可,主要是通过获取按键来控制调用。想了解Execute的用法可以参考http://www.xuanyusong.com/archives/3760