点击展开
-- 战斗渐隐脚本(仅野外/主城生效,所有副本禁用)
local fadeFrame = CreateFrame("Frame", "CombatFaderFrame", UIParent)
-- 配置选项:你可以调整这些数值
local COMBAT_ALPHA = 1.0      -- 战斗/副本内的透明度(完全不透明)
local NO_COMBAT_ALPHA = 0    -- 非战斗/野外/主城的透明度(完全透明,可调)
local FADE_DURATION = 3.0    -- 渐隐动画时间(秒)
-- 要应用渐隐效果的头像框架列表
local unitFramesToFade = {
    PlayerFrame,              -- 玩家头像
    TargetFrame,              -- 目标头像
    PetFrame,                  -- 玩家宝宝/宠物头像
    TargetFrameToT,            -- 目标的目标头像(可选)
    -- FocusFrame,            -- 焦点目标头像(按需添加)
    -- PartyMemberFrame1~4,    -- 小队成员头像(按需添加)
}
-- 检查玩家是否满血
local function IsPlayerFullHealth()
    local health = UnitHealth("player")
    local maxHealth = UnitHealthMax("player")
    return health == maxHealth
end
-- 检查是否满足渐隐条件:非战斗、无目标、满血、[仅野外/主城](核心修改)
local function ShouldFadeOut()    -- IsInInstance() 返回:(是否在副本内, 副本类型)    -- 副本类型说明:    -- "none" = 野外/主城 | "party"=5人本 | "raid"=团本 | "pvp"=战场 | "arena"=竞技场    local inInstance, instanceType = IsInInstance()        -- 只有同时满足:非战斗 + 无目标 + 满血 + 场景为野外/主城(instanceType="none"),才触发渐隐    return not UnitAffectingCombat("player")         and not UnitExists("target")         and IsPlayerFullHealth()         and instanceType == "none"  -- 精准限定:仅野外/主城end-- 初始化透明度for _, frame in ipairs(unitFramesToFade) do    if frame then        frame:SetAlpha(COMBAT_ALPHA)    endend-- 统一的渐隐/显示处理函数(优先判断副本场景)local function UpdateFramesAlpha()    local inInstance, instanceType = IsInInstance()        -- 核心规则:只要不是野外/主城(instanceType≠"none"),强制显示所有框架    if instanceType ~= "none" then        for _, frame in ipairs(unitFramesToFade) do            if frame and frame:IsVisible() then                UIFrameFadeRemoveFrame(frame)                frame:SetAlpha(COMBAT_ALPHA)            end        end        return  -- 副本场景直接退出,不执行任何渐隐逻辑    end    -- 野外/主城的原有逻辑    if UnitAffectingCombat("player") or UnitIsDeadOrGhost("player") then        -- 战斗中/死亡:强制显示        for _, frame in ipairs(unitFramesToFade) do            if frame and frame:IsVisible() then                UIFrameFadeRemoveFrame(frame)                frame:SetAlpha(COMBAT_ALPHA)            end        end    else        if ShouldFadeOut() then            -- 满足条件:渐隐            for _, frame in ipairs(unitFramesToFade) do                if frame and frame:IsVisible() and not frame:IsMouseOver() then                    UIFrameFadeOut(frame, FADE_DURATION, COMBAT_ALPHA, NO_COMBAT_ALPHA)                end            end        else            -- 不满足条件:强制显示            for _, frame in ipairs(unitFramesToFade) do                if frame and frame:IsVisible() then                    UIFrameFadeRemoveFrame(frame)                    frame:SetAlpha(COMBAT_ALPHA)                end            end        end    endend-- 注册所有需要监听的事件fadeFrame:RegisterEvent("PLAYER_REGEN_DISABLED")    -- 进入战斗fadeFrame:RegisterEvent("PLAYER_REGEN_ENABLED")    -- 脱离战斗fadeFrame:RegisterEvent("PLAYER_ENTERING_WORLD")    -- 登录/载入时fadeFrame:RegisterEvent("UNIT_HEALTH")              -- 血量变化fadeFrame:RegisterEvent("PLAYER_TARGET_CHANGED")    -- 目标变化fadeFrame:RegisterEvent("UNIT_PET")                -- 宠物变化fadeFrame:RegisterEvent("ZONE_CHANGED_NEW_AREA")    -- 区域切换(进出副本/主城/野外触发)-- 事件处理函数fadeFrame:SetScript("OnEvent", function(self, event, ...)    local arg1 = ...        -- 所有事件都触发透明度更新(区域切换时立即检查场景类型)    if event == "ZONE_CHANGED_NEW_AREA" then        UpdateFramesAlpha()  -- 进出主城/野外/副本时立即更新    elseif event == "PLAYER_REGEN_DISABLED" or event == "PLAYER_REGEN_ENABLED" then        UpdateFramesAlpha()    elseif event == "PLAYER_ENTERING_WORLD" then        UpdateFramesAlpha()    elseif event == "UNIT_HEALTH" and arg1 == "player" then        UpdateFramesAlpha()    elseif event == "PLAYER_TARGET_CHANGED" then        UpdateFramesAlpha()    elseif event == "UNIT_PET" then        UpdateFramesAlpha()    endend)-- 鼠标悬停/离开逻辑(仅野外/主城生效)for _, frame in ipairs(unitFramesToFade) do    if frame then        frame:SetScript("OnEnter", function(self)            -- 悬停时强制显示(无论场景)            UIFrameFadeRemoveFrame(self)            self:SetAlpha(COMBAT_ALPHA)        end)                frame:SetScript("OnLeave", function(self)            local inInstance, instanceType = IsInInstance()            -- 离开时仅野外/主城恢复渐隐            if instanceType == "none" and ShouldFadeOut() then                UIFrameFadeOut(self, FADE_DURATION, COMBAT_ALPHA, NO_COMBAT_ALPHA)            end        end)    endend