我来帮你添加渐显和渐隐效果。你的代码逻辑不错,但需要做一些调整来加入透明度动画:
```lua
local ui, root = GUI:npcUI("shijieditu")
-- 设置初始透明度为0(完全透明)
if GUI:Win_IsNotNull(root) then
GUI:setOpacity(root, 0)
end
-- 渐显动画
local function fadeIn()
if GUI:Win_IsNotNull(root) then
-- 先设置可见
GUI:setVisible(root, true)
-- 执行渐显动画
GUI:runAction(root, GUI:ActionFadeTo(0.3, 180))
end
end
-- 渐隐动画并关闭界面
local function fadeOutAndClose()
if GUI:Win_IsNotNull(root) then
GUI:runAction(root, GUI:ActionSequence(
GUI:ActionFadeTo(0.3, 0),
GUI:CallFunc(function()
GUI:Win_Close(root)
end)
))
end
end
-- 延迟执行渐显
SL:ScheduleOnce(function()
fadeIn()
end, 0.1)
-- 获取当前转生等级
local reLevel = SL:GetMetaValue("RELEVEL") or 0
local levelRequirements = {
{level = 5, button = 1, effect = 1, msgType = 33},
{level = 10, button = 2, effect = 2, msgType = 34},
{level = 15, button = 3, effect = 3, msgType = 35},
{level = 20, button = 4, effect = 4, msgType = 36},
{level = 25, button = 5, effect = 5, msgType = 37},
{level = 30, button = 6, effect = 6, msgType = 38},
{level = 35, button = 7, effect = 7, msgType = 39},
{level = 40, button = 8, effect = 8, msgType = 40},
{level = 45, button = 9, effect = 9, msgType = 41}
}
-- 初始隐藏所有按钮和特效
for i = 1, 9 do
local button_name = "Button_" .. i
local effect_name = "Effect_" .. i
if GUI:Win_IsNotNull(ui[button_name]) then
GUI:setVisible(ui[button_name], false)
end
if GUI:Win_IsNotNull(ui[effect_name]) then
GUI:setVisible(ui[effect_name], false)
end
end
for _, requirement in ipairs(levelRequirements) do
local button_name = "Button_" .. requirement.button
local effect_name = "Effect_" .. requirement.effect
if reLevel >= requirement.level then
for i = 1, requirement.button do
local btn_name = "Button_" .. i
if GUI:Win_IsNotNull(ui[btn_name]) then
GUI:setVisible(ui[btn_name], true)
end
end
for i = 1, requirement.effect do
local eff_name = "Effect_" .. i
if GUI:Win_IsNotNull(ui[eff_name]) then
GUI:setVisible(ui[eff_name], true)
end
end
if GUI:Win_IsNotNull(ui[button_name]) then
GUI:addOnClickEvent(ui[button_name], function()
SL:print("点击了第" .. requirement.button .. "个按钮,转生等级:" .. reLevel)
GUI:delayTouchEnabled(ui[button_name], 0.2)
SL:SendLuaNetMsg(58004, 0, requirement.msgType, 0, 0)
fadeOutAndClose()
end)
end
end
end
-- 转生5级
if reLevel >= 5 then
if GUI:Win_IsNotNull(ui.Button_1) then
GUI:setVisible(ui.Button_1, true)
GUI:addOnClickEvent(ui.Button_1, function()
SL:print("点击了第1个按钮,转生等级:" .. reLevel)
GUI:delayTouchEnabled(ui.Button_1, 0.2)
SL:SendLuaNetMsg(58004, 0, 33, 0, 0)
fadeOutAndClose()
end)
end
if GUI:Win_IsNotNull(ui.Effect_1) then
GUI:setVisible(ui.Effect_1, true)
end
end
-- 转生10级
if reLevel >= 10 then
if GUI:Win_IsNotNull(ui.Button_2) then
GUI:setVisible(ui.Button_2, true)
GUI:addOnClickEvent(ui.Button_2, function()
SL:print("点击了第2个按钮,转生等级:" .. reLevel)
GUI:delayTouchEnabled(ui.Button_2, 0.2)
SL:SendLuaNetMsg(58004, 0, 34, 0, 0)
fadeOutAndClose()
end)
end
if GUI:Win_IsNotNull(ui.Effect_2) then
GUI:setVisible(ui.Effect_2, true)
end
end
-- 转生15级
if reLevel >= 15 then
if GUI:Win_IsNotNull(ui.Button_3) then
GUI:setVisible(ui.Button_3, true)
GUI:addOnClickEvent(ui.Button_3, function()
SL:print("点击了第3个按钮,转生等级:" .. reLevel)
GUI:delayTouchEnabled(ui.Button_3, 0.2)
SL:SendLuaNetMsg(58004, 0, 35, 0, 0)
fadeOutAndClose()
end)
end
if GUI:Win_IsNotNull(ui.Effect_3) then
GUI:setVisible(ui.Effect_3, true)
end
end
-- 转生20级
if reLevel >= 20 then
if GUI:Win_IsNotNull(ui.Button_4) then
GUI:setVisible(ui.Button_4, true)
GUI:addOnClickEvent(ui.Button_4, function()
SL:print("点击了第4个按钮,转生等级:" .. reLevel)
GUI:delayTouchEnabled(ui.Button_4, 0.2)
SL:SendLuaNetMsg(58004, 0, 36, 0, 0)
fadeOutAndClose()
end)
end
if GUI:Win_IsNotNull(ui.Effect_4) then
GUI:setVisible(ui.Effect_4, true)
end
end
-- 转生25级
if reLevel >= 25 then
if GUI:Win_IsNotNull(ui.Button_5) then
GUI:setVisible(ui.Button_5, true)
GUI:addOnClickEvent(ui.Button_5, function()
SL:print("点击了第5个按钮,转生等级:" .. reLevel)
GUI:delayTouchEnabled(ui.Button_5, 0.2)
SL:SendLuaNetMsg(58004, 0, 37, 0, 0)
fadeOutAndClose()
end)
end
if GUI:Win_IsNotNull(ui.Effect_5) then
GUI:setVisible(ui.Effect_5, true)
end
end
-- 转生30级
if reLevel >= 30 then
if GUI:Win_IsNotNull(ui.Button_6) then
GUI:setVisible(ui.Button_6, true)
GUI:addOnClickEvent(ui.Button_6, function()
SL:print("点击了第6个按钮,转生等级:" .. reLevel)
GUI:delayTouchEnabled(ui.Button_6, 0.2)
SL:SendLuaNetMsg(58004, 0, 38, 0, 0)
fadeOutAndClose()
end)
end
if GUI:Win_IsNotNull(ui.Effect_6) then
GUI:setVisible(ui.Effect_6, true)
end
end
-- 转生35级
if reLevel >= 35 then
if GUI:Win_IsNotNull(ui.Button_7) then
GUI:setVisible(ui.Button_7, true)
GUI:addOnClickEvent(ui.Button_7, function()
SL:print("点击了第7个按钮,转生等级:" .. reLevel)
GUI:delayTouchEnabled(ui.Button_7, 0.2)
SL:SendLuaNetMsg(58004, 0, 39, 0, 0)
fadeOutAndClose()
end)
end
if GUI:Win_IsNotNull(ui.Effect_7) then
GUI:setVisible(ui.Effect_7, true)
end
end
-- 转生40级
if reLevel >= 40 then
if GUI:Win_IsNotNull(ui.Button_8) then
GUI:setVisible(ui.Button_8, true)
GUI:addOnClickEvent(ui.Button_8, function()
SL:print("点击了第8个按钮,转生等级:" .. reLevel)
GUI:delayTouchEnabled(ui.Button_8, 0.2)
SL:SendLuaNetMsg(58004, 0, 40, 0, 0)
fadeOutAndClose()
end)
end
if GUI:Win_IsNotNull(ui.Effect_8) then
GUI:setVisible(ui.Effect_8, true)
end
end
-- 转生45级
if reLevel >= 45 then
if GUI:Win_IsNotNull(ui.Button_9) then
GUI:setVisible(ui.Button_9, true)
GUI:addOnClickEvent(ui.Button_9, function()
SL:print("点击了第9个按钮,转生等级:" .. reLevel)
GUI:delayTouchEnabled(ui.Button_9, 0.2)
SL:SendLuaNetMsg(58004, 0, 41, 0, 0)
fadeOutAndClose()
end)
end
if GUI:Win_IsNotNull(ui.Effect_9) then
GUI:setVisible(ui.Effect_9, true)
end
end
--注册游戏事件
SL:RegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "shijieditu", function(data)
if data and data.opera == 1 then
shijieditu.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_REINLEVELCHANGE, "shijieditu", function(data)
shijieditu.upmain()
end)
SL:RegisterLUAEvent(LUA_EVENT_Win_Close, "shijieditu", function(Win_Closedata)
if Win_Closedata and Win_Closedata == "shijieditu" then
SL:UnRegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "shijieditu")
SL:UnRegisterLUAEvent(LUA_EVENT_REINLEVELCHANGE, "shijieditu")
SL:UnRegisterLUAEvent(LUA_EVENT_Win_Close, "shijieditu")
end
end)
```
**主要改动说明:**
1. **渐显效果**:
- 界面打开时先设置透明度为0(完全透明)
- 使用`SL:ScheduleOnce`延迟0.1秒执行渐显动画
- 使用`GUI:ActionFadeTo(0.3, 180)`在0.3秒内渐变到180透明度
2. **渐隐效果**:
- 创建了`fadeOutAndClose`函数
- 使用`GUI:ActionSequence`组合动作:先渐隐到0透明度,再执行关闭界面
- 所有按钮点击后都调用这个函数,而不是直接`GUI:Win_CloseAll()`
3. **透明度设置**:
- 透明度范围是0-255,180对应约70%不透明度
- 动画时长0.3秒,你可以根据需要调整
**注意事项**:
- 记得测试时界面要有足够的层级,避免被其他界面遮挡
- 如果界面有背景图片,透明度效果会更明显
- 动画时长和透明度值可以根据实际效果微调
这样修改后,你的界面就会有平滑的淡入淡出效果了!