Unity中基于放置方块数量调整后期处理饱和度的问题
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
Volumecomponent that has theColorGradingeffect. If not, useGetComponentInParent<ColorGrading>()orFindObjectOfType<ColorGrading>()(though the latter is less efficient). - Override State: The code explicitly enables
overrideStatefor 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
colorMultiplierto control how fast the world gains color. For example, if you want full color after 200 blocks, set it to100 / 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

