- Home /
How to manipulate PostProcessing effects settings during execution with c#?
I need to change the intensity value of my current Vignette effect depending in specific conditions.
So far I haven't been able to achieve this. Please help!
Image of what I want to achieve:
My setup:
Unity 2019.3.5f1 with URP implemented
Post Processing Package (v2) installed
Scene with a GameObject + Volume component with Vignette and Bloom effect overrides added
A monobehaviour component script in the very same GameObject from above
My monobehaviour script:
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
public class PostProcessingHandler : MonoBehaviour
{
private PostProcessVolume _ppv;
private Vignette _vignette;
// Vignette intensity values
[SerializeField, Range(0f, 1f)]
private float _lowIntensity = 0.25f;
[SerializeField, Range(0f, 1f)]
private float _highIntensity = 0.5f;
private void Start()
{
_ppv = GetComponent<PostProcessVolume>();
Debug.Log(_ppv);
}
}
When I try to retrieve PostProcessingVolume component to get it's profile (and further layer manipulation) I always get "null".
Image of my setup:
I've seen several guides/posts and even Unity documentation but I cannot get past that starting point of getting the PostProcessingVolume component, what am i missing?
Answer by JackMcComiskey6 · Nov 14, 2020 at 06:15 PM
I got this working and during my search I came across this Unity answers page so if for the sake of possibly helping someone in the future here is my code taken from this video: https://www.youtube.com/watch?v=jg2I2odw51M
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.PostProcessing;
public class PostProcessControl : MonoBehaviour
{
// Start is called before the first frame update
private PlayerController player;
private Vignette vignette;
private PostProcessVolume volume;
void Start()
{
player = GameObject.Find("PlayerSprite").GetComponent<PlayerController>();
volume = GetComponent<PostProcessVolume>();
volume.profile.TryGetSettings(out vignette);
}
// Update is called once per frame
void Update()
{
if (player.gameover)
{
vignette.intensity.value = Mathf.Lerp(vignette.intensity.value, 1, 1 * Time.deltaTime);
}
}
}
My set up is the same as described in the OP. Note, in the linked video its mentioned to avoid using the package manager for downloading the post processing asset. I did NOT encounter this issue with Unity 2019.2.6f1 and post-processing version 3.0.1
This is the right idea, although do be careful about modifying the volume stuff as-is. I don't remember which version of PP the PostProcessVolume
corresponds to, but those are usually scriptable objects and modifications will persist in the editor after play ends.
For things like that or TerrainData, you can create a runtime version and mess with that one instead. $$anonymous$$y setup for working with Beautify on the newer PP stack:
void Awake()
{
this.runtimeProfile = Instantiate(this.volumeComponent.profile);
this.volumeComponent.profile = this.runtimeProfile;
this.runtimeProfile.TryGet<Beautify.Universal.Beautify>(out this.beautifySettings);
}
void OnDestroy()
{
Destroy(this.runtimeProfile);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to change the color of a vignette? 0 Answers