目录
Unlua的使用
前言
整理一下Unlua的整个学习流程
下载Unlua插件
我们此处使用的是腾讯的Unlua插件,打开官方的Github链接,下载对应的版本
官方链接GitHub
Wiki文档
插件安装
把下载好的插件放在自己新建项目的Plugins文件夹下,编译启动
快速入门
点击Create的时候,会根据填写的模块名字生成路径
语法汇总
模块导入
与路径对应即可
local Common = require "Core.Common"
多行字符串
官方静态方法调用
UE.类名.静态方法名字
nil
代表无效值空值未定义的值
蓝图方法调用
self:XXXFunction()
输出结果:这是我的蓝图测试
重载蓝图中的方法
function M:XXXFunction())
end
输出结果:M:TestFunction
主动调用被重载的蓝图方法
self.Overridden.XXXFunction()
输出结果:
这是我的蓝图测试
M:TestFunction
输入绑定
function M:LeftMouseButton_Pressed()
end
实例:绑定按键并打印它的名字
local M = UnLua.Class()
local PrintString = UE.UKismetSystemLibrary.PrintString
local function Print(text)
PrintString(nil, text, true, false, UE.FLinearColor(1, 1, 1, 1), 100)
end
function M:ReceiveBeginPlay()
local msg =
[[
来试试以下输入吧:
字母、数字、小键盘、方向键、鼠标
]]
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)
Print(string.format("按下了%s", key.KeyName))
end
end
end
local function SetupAxisBindings()
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
Print(string.format("%s(%f)", axis_name, value))
end
end
end
end
SetupKeyBindings() -- 在require的时候会执行
SetupAxisBindings()
local BindKey = UnLua.Input.BindKey
BindKey(M, "C", "Pressed", function(self, Key)
Print("按下了C")
end)
BindKey(M, "C", "Pressed", function(self, Key)
Print("复制")
end, {
Ctrl = true })
BindKey(M, "V", "Pressed", function(self, Key)
Print("按下了V")
end)
BindKey(M, "V", "Pressed", function(self, Key)
Print("粘贴")
end, {
Ctrl =