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

Unity中基于放置方块数量调整后期处理饱和度的问题

Fix for Post Processing Saturation Not Updating via Code

Root Cause

Your public FloatParameter satu is being serialized by Unity, which overrides the assignment satu = colorGrading.saturation; you make in Start(). This means when you modify satu.value in Update(), you're changing a disconnected parameter instead of the one linked to your ColorGrading effect.

Corrected Code

Remove the redundant satu variable and directly modify the ColorGrading component's saturation value. This ensures your changes are applied to the active post processing effect:

// Reference to the BuildSystem script
BuildSystem buildSys;

// Total blocks placed (synced from BuildSystem)
[SerializeField] int blockTotal;

// Control how much each block increases saturation
[SerializeField] float colorMultiplier = 0.5f;

// Direct reference to the ColorGrading component
private ColorGrading colorGrading;

public GameObject gameManager;

void Start() 
{            
    buildSys = gameManager.GetComponent<BuildSystem>();
    // Ensure this script is attached to the same GameObject as the ColorGrading component
    colorGrading = GetComponent<ColorGrading>();
    
    // Initialize saturation to -100 (full grayscale)
    if (colorGrading != null)
    {
        colorGrading.saturation.value = -100f;
        // Make sure Override State is enabled (you can set it via code too)
        colorGrading.saturation.overrideState = true;
    }
}

void Update() 
{
    if (buildSys == null || colorGrading == null) return;
    
    blockTotal = buildSys.blockTotal;
    
    // Calculate saturation: starts at -100, climbs to 0 (full color)
    // Clamp ensures we don't go above 0 or below -100
    float newSaturation = Mathf.Clamp(-100 + (blockTotal * colorMultiplier), -100, 0);
    
    // Apply the new saturation directly to the ColorGrading component
    colorGrading.saturation.value = newSaturation;
}

Additional Checks

  • Component Placement: Verify this script is attached to the same GameObject as your Volume component that has the ColorGrading effect. If not, use GetComponentInParent<ColorGrading>() or FindObjectOfType<ColorGrading>() (though the latter is less efficient).
  • Override State: The code explicitly enables overrideState for saturation, so you don't have to rely on the Inspector setting. This ensures the local value you set is used instead of the profile's default.
  • Color Multiplier Adjustment: Tweak colorMultiplier to control how fast the world gains color. For example, if you want full color after 200 blocks, set it to 100 / 200 = 0.5.

Why This Works

By directly modifying colorGrading.saturation.value, you bypass the serialized public variable that was causing the disconnect. This ensures every change you make in code is applied directly to the active post processing effect, which will be visible in-game immediately.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 02:48:21