forked from Tencent/UnLua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_BindInputs.lua
71 lines (57 loc) · 1.98 KB
/
03_BindInputs.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
--[[
说明:需要监听按键或Action时,只需要在返回的table中声明 {KeyName}_Pressed / {KeyName}_Released
例如:
function M:SpaceBar_Pressed()
end
{KeyName}可以参考EKeys的文档:
https://docs.unrealengine.com/4.26/en-US/API/Runtime/InputCore/EKeys/
或者源码:
\Engine\Source\Runtime\InputCore\Classes\InputCoreTypes.h
]]--
require "UnLua"
local Screen = require "Tutorials.Screen"
local M = Class()
function M:ReceiveBeginPlay()
local msg =
[[
来试试以下输入吧:
字母、数字、小键盘、方向键、鼠标
—— 本示例来自 "Content/Script/Tutorials.03_BindInputs.lua"
]]
Screen.Print(msg)
end
local function SetupKeyBindings()
local key_names = {
-- 字母
"A", "B", "C", "D", "E","F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
-- 数字
"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
-- 小键盘
"NumPadOne", "NumPadTwo", "NumPadThree", "NumPadFour", "NumPadFive", "NumPadSix", "NumPadSeven", "NumPadEight", "NumPadNine",
-- 方向键
"Up", "Down", "Left", "Right",
-- ProjectSettings -> Engine - Input -> Action Mappings
"Fire", "Aim",
}
for _, key_name in ipairs(key_names) do
M[key_name .. "_Pressed"] = function(self, key)
Screen.Print(string.format("按下了%s", key.KeyName))
end
end
end
local function SetupAxisBindings()
-- ProjectSettings -> Engine - Input -> Axis Mappings
local axis_names = {
"MoveForward", "MoveRight", "Turn", "LookUp", "LookUpRate", "TurnRate"
}
for _, axis_name in ipairs(axis_names) do
M[axis_name] = function(self, value)
if value ~= 0 then
Screen.Print(string.format("%s(%f)", axis_name, value))
end
end
end
end
SetupKeyBindings()
SetupAxisBindings()
return M