SL:print("=========GUI加载成功!")
local GUI = GUI
function GUI:addfunchhhhhhhhhhhhh()
SL:print("打印出的信息")
end
---打开npc界面
function GUI:npcUI(uiname)
local w ,h = SL:GetMetaValue("SCREEN_WIDTH") ,SL:GetMetaValue("SCREEN_HEIGHT")---获取元变量的宽和高
if
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-30 14:58:52 |
👁️ 阅读: 17
```lua
SL:print("=========GUI加载成功!")
local GUI = GUI
function GUI:addfunchhhhhhhhhhhhh()
SL:print("打印出的信息")
end
---打开npc界面
function GUI:npcUI(uiname)
local w ,h = SL:GetMetaValue("SCREEN_WIDTH") ,SL:GetMetaValue("SCREEN_HEIGHT")---获取元变量的宽和高
-- 创建全屏半透明黑色背景
local bg = GUI:Widget_Create(nil, "black_bg", 0, 0, w, h)
GUI:setColor(bg, 0, 0, 0) -- 设置黑色
GUI:setOpacity(bg, 128) -- 50%透明度
local root
if SL:GetMetaValue("CURRENT_OPERMODE") == 1 then
root = GUI:Win_Create("NPC_Window", 0, 0, w, h) ---创建界面 调整界面所在的位置
else
root = GUI:Win_Create("NPC_Window", -80, -100, w, h) ---创建界面 调整界面所在的位置
end
-- 将背景添加到界面
GUI:addChild(root, bg)
GUI:setOpacity(root, 180)
GUI:LoadExport(root, "NPC/"..uiname) --加载UI界面 和路径
local ui = GUI:ui_delegate(root)
---获取父节点下的所有组件
if not ui.ImageView then error("你把背景唯一id改成ImageView") end -----如果gui的主图名字 不是bg,那就修改 成bg
if ui.ImageView then
GUI:setTouchEnabled(ui.ImageView, true)--从gui中拿过来的 禁止左键穿透(true) 允许穿透(false):::说明书中的含义是:设置控件是否可以触摸
GUI:Win_SetSwallowRightMouseTouch(ui.ImageView, true) ---鼠标右键吞噬 禁止右键穿透
-- 添加可拖拽功能
GUI:Win_SetDrag(root, ui.ImageView)
end
if not ui.guanbi then error("你把关闭按钮唯一id改成guanbi") end -----如果gui的主图名字 不是bg,那就修改 成bg
if ui.guanbi then
GUI:addOnClickEvent(ui.guanbi, function()
GUI:Win_Close(root)
end)
end
-- 添加界面浮起功能
if ui.ImageView then
GUI:Win_SetZPanel(root, ui.ImageView)
end
return ui , root
end
function GUI:onclick(widget, func)
SL:print(tolua.type(widget))---属于cocos的函数 打印组件的类型
GUI:setTouchEnabled(widget, true) ---设置控件是否可以触摸
GUI:addOnClickEvent(widget, func) ---设置控件点击事件
end
--设置锚点
function GUI: maodian(widget, x, y)
GUI: setAnchorPoint(widget, x or 0.5, y or 0.5)
end
return GUI
```
**修改说明:**
1. **添加了全屏半透明黑色背景**:
- 使用 `GUI:Widget_Create()` 创建全屏黑色背景
- 设置颜色为黑色 `(0, 0, 0)` 和透明度 `128`(50%)
- 添加到界面作为背景层
2. **添加了可拖拽功能**:
- 使用 `GUI:Win_SetDrag(root, ui.ImageView)` 使界面可以通过背景图拖拽
- 需要确保 `ui.ImageView` 存在,这是拖拽的触发区域
3. **添加了界面浮起功能**:
- 使用 `GUI:Win_SetZPanel(root, ui.ImageView)` 使界面浮在其他界面之上
**注意**:
- 背景创建时使用了 `nil` 作为父节点,因为界面本身会作为父容器
- 拖拽和浮起功能都依赖于 `ui.ImageView` 这个背景图控件
- 如果界面布局不同,可能需要调整拖拽控件的名称
这样修改后,你的NPC界面就有了全屏半透明黑色背景,并且可以通过背景图拖拽移动界面,同时界面会浮在其他界面之上。
```lua
SL:print("=========GUI加载成功!")
local GUI = GUI
function GUI:addfunchhhhhhhhhhhhh()
SL:print("打印出的信息")
end
---打开npc界面
function GUI:npcUI(uiname)
local w ,h = SL:GetMetaValue("SCREEN_WIDTH") ,SL:GetMetaValue("SCREEN_HEIGHT")---获取元变量的宽和高
-- 创建全屏半透明黑色背景
local bg = GUI:Widget_Create(nil, "black_bg", 0, 0, w, h)
GUI:setColor(bg, 0, 0, 0) -- 设置黑色
GUI:setOpacity(bg, 128) -- 50%透明度
local root
if SL:GetMetaValue("CURRENT_OPERMODE") == 1 then
root = GUI:Win_Create("NPC_Window", 0, 0, w, h) ---创建界面 调整界面所在的位置
else
root = GUI:Win_Create("NPC_Window", -80, -100, w, h) ---创建界面 调整界面所在的位置
end
-- 将背景添加到界面
GUI:addChild(root, bg)
GUI:setOpacity(root, 180)
GUI:LoadExport(root, "NPC/"..uiname) --加载UI界面 和路径
local ui = GUI:ui_delegate(root)
---获取父节点下的所有组件
if not ui.ImageView then error("你把背景唯一id改成ImageView") end -----如果gui的主图名字 不是bg,那就修改 成bg
if ui.ImageView then
GUI:setTouchEnabled(ui.ImageView, true)--从gui中拿过来的 禁止左键穿透(true) 允许穿透(false):::说明书中的含义是:设置控件是否可以触摸
GUI:Win_SetSwallowRightMouseTouch(ui.ImageView, true) ---鼠标右键吞噬 禁止右键穿透
-- 添加可拖拽功能
GUI:Win_SetDrag(root, ui.ImageView)
end
if not ui.guanbi then error("你把关闭按钮唯一id改成guanbi") end -----如果gui的主图名字 不是bg,那就修改 成bg
if ui.guanbi then
GUI:addOnClickEvent(ui.guanbi, function()
GUI:Win_Close(root)
end)
end
-- 添加界面浮起功能
if ui.ImageView then
GUI:Win_SetZPanel(root, ui.ImageView)
end
return ui , root
end
function GUI:onclick(widget, func)
SL:print(tolua.type(widget))---属于cocos的函数 打印组件的类型
GUI:setTouchEnabled(widget, true) ---设置控件是否可以触摸
GUI:addOnClickEvent(widget, func) ---设置控件点击事件
end
--设置锚点
function GUI: maodian(widget, x, y)
GUI: setAnchorPoint(widget, x or 0.5, y or 0.5)
end
return GUI
```
**修改说明:**
1. **添加了全屏半透明黑色背景**:
- 使用 `GUI:Widget_Create()` 创建全屏黑色背景
- 设置颜色为黑色 `(0, 0, 0)` 和透明度 `128`(50%)
- 添加到界面作为背景层
2. **添加了可拖拽功能**:
- 使用 `GUI:Win_SetDrag(root, ui.ImageView)` 使界面可以通过背景图拖拽
- 需要确保 `ui.ImageView` 存在,这是拖拽的触发区域
3. **添加了界面浮起功能**:
- 使用 `GUI:Win_SetZPanel(root, ui.ImageView)` 使界面浮在其他界面之上
**注意**:
- 背景创建时使用了 `nil` 作为父节点,因为界面本身会作为父容器
- 拖拽和浮起功能都依赖于 `ui.ImageView` 这个背景图控件
- 如果界面布局不同,可能需要调整拖拽控件的名称
这样修改后,你的NPC界面就有了全屏半透明黑色背景,并且可以通过背景图拖拽移动界面,同时界面会浮在其他界面之上。
← 返回问答库