---@diagnostic disable: undefined-global
local ui = {}
function ui.init(parent)
local width = SL:Get_SCREEN_WIDTH() --屏幕宽
local height = SL:Get_SCREEN_HEIGHT() --屏幕高
-- Create Layout
local Layout = GUI:Layout_Create(parent, "Layout", width / 2, h
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-05 19:46:20 |
👁️ 阅读: 1
可以通过点击NPC触发显示界面。根据浮生梦老师前端课程内容,你需要使用 `SL:OpenNpcUI(npcID)` 函数来打开与NPC关联的界面。
在你的代码基础上,可以这样修改:
```lua
---@diagnostic disable: undefined-global
local ui = {}
function ui.init(parent)
local width = SL:GetMetaValue("SCREEN_WIDTH") -- 使用正确的函数获取屏幕宽
local height = SL:GetMetaValue("SCREEN_HEIGHT") -- 使用正确的函数获取屏幕高
-- Create Layout
local Layout = GUI:Layout_Create(parent, "Layout", width / 2, height / 2, 0.5, 0.5, false)
GUI:Layout_setBackGroundColorType(Layout, 1)
GUI:Layout_setBackGroundColor(Layout, "#96c8ff")
GUI:Layout_setBackGroundColorOpacity(Layout, 140)
GUI:setTouchEnabled(Layout, false)
GUI:setTag(Layout, -1)
-- Create ImageView
local ImageView = GUI:Image_Create(Layout, "ImageView", 0, 0, "res/sss/000003.png")
GUI:setAnchorPoint(ImageView, 0.5, 0.5)
GUI:setTouchEnabled(ImageView, false)
GUI:setTag(ImageView, -1)
local close = GUI:Button_Create(ImageView, "close", 616.00, 517.00, "res/sss/000005.png")
GUI:Button_loadTexturePressed(close, "res/sss/000006.png")
GUI:Button_loadTextureDisabled(close, "res/sss/000007.png")
GUI:Button_setTitleText(close, "")
GUI:Button_setTitleColor(close, "#ffffff")
GUI:Button_setTitleFontSize(close, 14)
GUI:Button_titleEnableOutline(close, "#000000", 1)
GUI:setTouchEnabled(close, true)
GUI:setTag(close, -1)
-- 添加关闭按钮事件
GUI:addOnClickEvent(close, function()
GUI:removeFromParent(Layout) -- 移除界面
end)
-- Create Button
local Button = GUI:Button_Create(ImageView, "Button", 349.00, 290.00, "res/private/gui_edit/Button_Normal.png")
GUI:Button_loadTexturePressed(Button, "res/private/gui_edit/Button_Press.png")
GUI:Button_loadTextureDisabled(Button, "res/private/gui_edit/Button_Disable.png")
GUI:Button_setTitleText(Button, "升级")
GUI:Button_setTitleColor(Button, "#ffffff")
GUI:Button_setTitleFontSize(Button, 14)
GUI:Button_titleEnableOutline(Button, "#000000", 1)
GUI:setTouchEnabled(Button, true)
GUI:setTag(Button, -1)
-- Create Text
local Text = GUI:Text_Create(ImageView, "Text", 194.00, 298.00, 16, "#ffffff", [[当前等级:]])
GUI:setTouchEnabled(Text, false)
GUI:setTag(Text, -1)
GUI:Text_enableOutline(Text, "#000000", 1)
-- Create Text_1
local Text_1 = GUI:Text_Create(Text, "Text_1", 90.00, -1.00, 16, "#ffffff", [[0]])
GUI:setTouchEnabled(Text_1, false)
GUI:setTag(Text_1, -1)
GUI:Text_enableOutline(Text_1, "#000000", 1)
-- Create Button_1
local Button_1 = GUI:Button_Create(ImageView, "Button_1", 418.18, 287.88, "res/private/gui_edit/Button_Normal.png")
GUI:Button_loadTexturePressed(Button_1, "res/private/gui_edit/Button_Press.png")
GUI:Button_loadTextureDisabled(Button_1, "res/private/gui_edit/Button_Disable.png")
GUI:Button_setTitleText(Button_1, "升级2")
GUI:Button_setTitleColor(Button_1, "#ffffff")
GUI:Button_setTitleFontSize(Button_1, 14)
GUI:Button_titleEnableOutline(Button_1, "#000000", 1)
GUI:setTouchEnabled(Button_1, true)
GUI:setTag(Button_1, -1)
return Layout -- 返回布局对象
end
return ui
```
然后在你的NPC触发脚本中这样调用:
```lua
-- 当玩家点击NPC时触发
function OnNpcClick(npcID)
-- 打开NPC界面,npcID是具体的NPC编号
SL:OpenNpcUI(npcID)
end
```
**关键点说明:**
1. `SL:OpenNpcUI(npcID)` 是996引擎的标准函数,用于打开与指定NPC关联的界面
2. 你需要将 `npcID` 替换为实际NPC的编号
3. 界面创建后,通过 `GUI:addOnClickEvent` 为关闭按钮添加移除界面的功能
4. 注意我修正了获取屏幕宽高的函数,使用 `SL:GetMetaValue("SCREEN_WIDTH")` 而不是 `SL:Get_SCREEN_WIDTH()`
这样当玩家点击NPC时,就会显示你创建的界面了。记得在NPC配置中关联这个界面功能哦!
← 返回问答库