一般情況下大家都用Update
監(jiān)測(cè)形如Input.GetMouseButton(0)
來檢測(cè)鼠鍵輸入;
畢竟Update是在每次渲染新的一幀的時(shí)候才會(huì)調(diào)用亩进,時(shí)間有快有慢
筆者總是擔(dān)心輸入會(huì)監(jiān)測(cè)不到。
推薦方法如下:
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 執(zhí)行截屏
if(Event.current.Equals(Event.KeyboardEvent("^&X")))
{
screenshot.enabled = true;
}
}
}
OnGUI
每幀執(zhí)行很多次尼酿,并且可以很容易監(jiān)測(cè)組合鍵集乔,故推薦之~
Tips:下面是特定字符與按鍵對(duì)應(yīng)關(guān)系
& = Alternate
^ = Control
% = Command/Windows key
# = Shift
----Examples------
&f12 = Alternate + F12
"^[0]" = Control + keypad0
API詳見:Event.KeyboardEvent
快捷訪問:
- Unity3D 如何設(shè)置CubeMap
- Unity3D UGUI Button OnClick事件統(tǒng)一管理
- UGUI Slider onValueChanged事件統(tǒng)一管理
- UGUI Toggle onValueChanged事件統(tǒng)一管理
- Unity3D監(jiān)測(cè)按鍵輸入(快捷鍵)的有效方法 ←您在這里
- Unity3D Editor模式下的System.IO數(shù)據(jù)訪問
- Unity3D 掛載的腳本取消勾選居然還會(huì)運(yùn)行P吭啤感耙!
- Unity 代碼動(dòng)態(tài)勾選或取消材質(zhì)球上的選項(xiàng)
- Unity SerializeField序列化字段導(dǎo)致Bug的跳坑筆記