Search Unity

Question How do I programmatically modify a Volume Profile's component's "active" without affecting asset

Discussion in 'Universal Render Pipeline' started by Zethros, Sep 5, 2021.

  1. Zethros

    Zethros

    Joined:
    Jan 11, 2013
    Posts:
    12
    How do I programmatically (via script/code) modify (change/edit) a VolumeProfile's VolumeComponent's "active" field, without affecting the VolumeProfile asset (.asset file located on disk, within the Unity project).

    See the following snippet of my code:
    Code (CSharp):
    1. public class ModifyVolumeByUserSettings : MonoBehaviour
    2. {
    3.     // Set in inspector. Is attached to the same gameobject
    4.     public Volume Comp_Volume;
    5.  
    6.     // Called somewhere else
    7.     public void SetVolumeProfile(VolumeProfile profile)
    8.     {
    9.         Comp_Volume.profile = Object.Instantiate(profile);
    10.     }
    11.     // On update, press M to toggle bloom.active. Will affect the profile asset file, instead of a seperate instance of it.
    12.     private void Update()
    13.     {
    14.         if (Input.GetKeyDown(KeyCode.M))
    15.         {
    16.             Bloom bloom;
    17.             if (Comp_Volume.profile.TryGet(out bloom))
    18.                 bloom.active = !bloom.active;
    19.         }
    20.     }
    21. }
    While the game is running, pressing "M" toggles the Bloom VolumeComponent (or, "override") within the game object (as seen on the left) as well as the volume profile asset (as seen on the right) despite the Profile being an instanced clone of the asset.
    upload_2021-9-5_15-27-1.png

    Can someone help me?

    I am using Unity v2020.3.4f, Universal RP v10.4.0. The gameobject is a regular object, in the root of the hierarchy. It is not set to be DontDestroyOnLoad().