一般情况下大家都用Update
监测形如Input.GetMouseButton(0)
来检测鼠键输入;
毕竟Update是在每次渲染新的一帧的时候才会调用,时间有快有慢
笔者总是担心输入会监测不到。
推荐方法如下:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
void OnGUI()
{
if (Event.current.Equals(Event.KeyboardEvent("[enter]")))
{
print("你按下了“Enter”键!");
}
if (Event.current.Equals(Event.KeyboardEvent("return")))
{
print("你按下了“Return”键!");
}
//Ctrl + Alt + X 执行截屏
if(Event.current.Equals(Event.KeyboardEvent("^&X")))
{
screenshot.enabled = true;
}
}
}
OnGUI
每帧执行很多次,并且可以很容易监测组合键,故推荐之~
Tips:下面是特定字符与按键对应关系
& = Alternate
^ = Control
% = Command/Windows key
# = Shift
----Examples------
&f12 = Alternate + F12
"^[0]" = Control + keypad0
API详见:Event.KeyboardEvent
快捷访问: