Access Bloom volume override in script
I'm trying to control Threshold/Intensity for the Bloom override on a Volume object from a script in 2019.3.10f1. Here's what I've got:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.PostProcessing;
public class MainMenuFlicker: MonoBehaviour
{
Volume volume;
Bloom bloom;
void Start()
{
volume = gameObject.GetComponent<Volume>();
Bloom tmp;
if (volume.profile.TryGet<Bloom>(out tmp))
{
bloom = tmp;
}
}
void Update()
{
}
}
This gives me the error: Assets\Scripts\MainMenuFlicker.cs(16,28): error CS0311: The type 'UnityEngine.Rendering.PostProcessing.Bloom' cannot be used as type parameter 'T' in the generic type or method 'VolumeProfile.TryGet<T>(out T)'. There is no implicit reference conversion from 'UnityEngine.Rendering.PostProcessing.Bloom' to 'UnityEngine.Rendering.VolumeComponent'.
Looking around online, it seems like this part of the API is changing pretty rapidly (e.g. different required using
statements), and I can't figure out how to correctly TryGet the Bloom from my Volume.
Answer by victormeas · Mar 28, 2021 at 09:47 AM
I've seen a number of people get stuck with this and for good reason, it's definitely a bug on the Unity Dev Teams end. One thing to not is that volumes stack, as such, you can simply put your bloom on a separate object and change its influence on your game screen through the weight parameter.
using UnityEngine;
using UnityEngine.Rendering;
public class VolumeWeightSin : MonoBehaviour
{
Volume m_Volume;
void Update()
{
if (m_Volume != null)
{
m_Volume.weight = Mathf.Sin(Time.realtimeSinceStartup);
}
}
}
Your answer
Follow this Question
Related Questions
Is there a way to apply a post processing effect only in one object in unity 2d? 0 Answers
Render UI on a Separate Camera without losing Image Effects? 2 Answers
Not sure how to word this question but having issue with mixer and sound clips 1 Answer
How to use Post-Processing with URP in a 2D project? 0 Answers
How do I add music code to my C# without getting a API update prompt? 1 Answer