You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Roblox Studio中如何根据时间切换游戏光照与色彩校正?

在Roblox中根据时间自动调整ColorCorrection效果

完全可以实现根据游戏时间切换色彩校正效果,你当前的代码问题在于只会执行一次,没法实时响应时间变化,下面是具体实现方案:

代码放置位置

  • 若要全局生效,把脚本放在ServerScriptService中(ServerScript)
  • 若要让玩家客户端单独控制,放在StarterPlayerScripts中(LocalScript)

具体实现步骤

  1. 先在Roblox Studio中配置ColorCorrection:右键game.Lighting → 插入对象 → 选择ColorCorrection,调整好你需要的夜间参数(比如给TintColor设偏蓝的色调、拉高一点Contrast)。
  2. 使用以下代码替换你的原代码:
local Lighting = game:GetService("Lighting")
local ColorCorrection = Lighting:FindFirstChild("ColorCorrection")

-- 自动创建ColorCorrection(如果不存在)
if not ColorCorrection then
    ColorCorrection = Instance.new("ColorCorrection")
    ColorCorrection.Name = "ColorCorrection"
    -- 这里设置夜间默认参数,可按需修改
    ColorCorrection.TintColor = Color3.fromRGB(100, 150, 255)
    ColorCorrection.Contrast = 0.1
    ColorCorrection.Parent = Lighting
end

-- 定义光照更新逻辑
local function updateLighting()
    local timeStr = Lighting.TimeOfDay
    -- 将时间转换为小时数,方便判断时段
    local hours = tonumber(string.sub(timeStr, 1, 2))
    local minutes = tonumber(string.sub(timeStr, 4, 5))
    local totalHours = hours + minutes / 60

    -- 夜间时段:18:00 到次日7:00,可自行调整范围
    if totalHours >= 18 or totalHours <= 7 then
        ColorCorrection.Enabled = true
    else
        ColorCorrection.Enabled = false
    end
end

-- 初始化执行一次
updateLighting()
-- 监听时间变化,实时更新
Lighting:GetPropertyChangedSignal("TimeOfDay"):Connect(updateLighting)

关键说明

  • 原代码只执行一次,通过GetPropertyChangedSignal("TimeOfDay")可以监听时间变化,实时触发调整
  • 把时间转成小时数,比直接字符串比较更灵活,方便自定义夜间时段
  • 你可以根据需求修改totalHours的判断范围,以及ColorCorrection的各项参数

内容的提问来源于stack exchange,提问作者Braulio Valencia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 00:12:14