动态创建UMG
local widget_class =UE.UClass.Load("/Game/MyApp/WBP_MainUI") local widget_root = NewObject(widget_class, self) widget_root:AddToViewport()
UMG添加事件
function WBP_MainUI:Construct() Screen.Print('-----WBP_MainUI-----'); self.Btn.OnClicked:Add(self, self.OnButtonClicked)
self.ClickMeButton.OnClicked:Add(self, self.OnButtonClicked)
self.ClickMeCheckBox.OnCheckStateChanged:Add(self, self.OnCheckBoxToggled)
end function WBP_MainUI:OnButtonClicked() Screen.Print('Click...sdfsdfdfd.'); 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
定时事件
-- 相当于在蓝图中的 Set Timer by Event self.TimerHandle = UE.UKismetSystemLibrary.K2_SetTimerDelegate({ self, self.OnTimer }, 1, true) function M:OnTimer() local seconds = UE.UKismetSystemLibrary.GetGameTimeInSeconds(self) self.GameTimeTextBlock:SetText(string.format("游戏时长:%d 秒", math.floor(seconds))) end function M:Destruct() -- 在UMG被销毁时尽量清理已绑定的委托,不清理则会在切换Map时自动清理 self.ClickMeButton.OnClicked:Remove(self, self.OnButtonClicked) self.ClickMeCheckBox.OnCheckStateChanged:Remove(self, self.OnCheckBoxToggled) -- 相当于在蓝图中的 Clear and Invalidate Timer by Handle UE.UKismetSystemLibrary.K2_ClearAndInvalidateTimerHandle(self, self.TimerHandle) end
容器类
Lua调用C++类
--本示例C++源码: --Source\TPSProject\TutorialBlueprintFunctionLibrary.cpp Screen.Print(msg) UE.UTutorialBlueprintFunctionLibrary.CallLuaByGlobalTable() Screen.Print("=================") UE.UTutorialBlueprintFunctionLibrary.CallLuaByFLuaTable()