GUIUtil.lua里面的
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-06-10 11:12:21 |
👁️ 阅读: 7
根据浮生梦老师前端课程内容,`GUIUtil.lua` 是一个常用的前端工具脚本,主要用于注册界面事件、处理特效和网络消息等。
从资料中可以看到,`GUIUtil.lua` 通常包含以下功能:
1. **注册界面初始化事件** - 在角色属性初始化后创建界面元素
2. **处理特效播放** - 创建和移除场景特效
3. **注册网络消息回调** - 处理服务器发送的特效播放指令
4. **处理富文本超链点击事件** - 响应聊天中的超链接点击
以下是 `GUIUtil.lua` 的典型结构示例:
```lua
-- 注册角色属性初始化事件
SL:RegisterLUAEvent(LUA_EVENT_ROLE_PROPERTY_INITED, "GUIUtil", function ()
local parent = GUI:Win_FindParent(102)
if parent then
local gmBtn = GUI:Button_Create(parent, "btn_switch", -300, -200, "res/public/1900000652.png")
GUI:Button_setTitleText(gmBtn, "GM操作")
GUI:Button_setTitleFontSize(gmBtn, 15)
GUI:addOnClickEvent(gmBtn, function()
GUI:Win_Open("A/GMBoxOBJ")
end)
end
end)
-- 注册网络消息处理特效
local function networkCB(msgID, effectID, loop, _, actorID)
local dir = SL:GetMetaValue("ACTOR_DIR", actorID)
local posM_x = SL:GetMetaValue("ACTOR_POSITION_X", actorID)
local posM_y = SL:GetMetaValue("ACTOR_POSITION_Y", actorID)
local parent = GUI:Attach_SceneF()
local effectName = "effect_" .. effectID
local handle = GUI:getChildByName(parent, effectName)
if handle then
GUI:removeFromParent(handle)
end
handle = GUI:Effect_Create(parent, effectName, posM_x, posM_y, 3, effectID, 0, 0, dir, 2)
GUI:Effect_addOnCompleteEvent(handle, function(widget)
loop = loop - 1
if loop < 1 then
GUI:removeFromParent(widget)
end
end)
end
SL:RegisterLuaNetMsg(9999, networkCB)
-- 注册富文本超链点击事件
SL:RegisterLUAEvent(LUA_EVENT_RICHTEXT_OPEN_URL, "GUIUtil", function (...)
SL:Print("超链点击触发", ...)
end)
```
这个脚本通常放在 `GUILayout` 文件夹中,通过 `SL:Require` 加载到主界面中。
← 返回问答库