Search Unity

Access Post-Processing (URP) via script

Discussion in 'Scripting' started by wm-VR, Jul 21, 2020.

  1. wm-VR

    wm-VR

    Joined:
    Aug 17, 2014
    Posts:
    123
    Hello!

    I want to access the ColorAdjustment/postExposure option via script during runtime using the Universal Render Pipeline.

    This is the code I have written so far but it's not working. In the past it seems to me ways easier to access the Post Processing stack.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5. using UnityEngine.Rendering;
    6. using UnityEngine.Rendering.Universal;
    7.  
    8. public class CameraRig : MonoBehaviour
    9. {
    10.     [SerializeField]
    11.     CinemachineVirtualCamera mainCamera;
    12.  
    13.     [SerializeField]
    14.     CinemachineFreeLook _2ndCamera;
    15.  
    16.     [SerializeField]
    17.     bool secondcamera;
    18.  
    19.     ColorAdjustments _colorAdjustment;
    20.     VolumeProfile volumeProfile;
    21.  
    22.     private void Awake()
    23.     {
    24.        volumeProfile = GameObject.Find("Post Processing").GetComponent<UnityEngine.Rendering.Volume>()?.profile;
    25.        if(!volumeProfile.TryGet(out _colorAdjustment)) throw new System.NullReferenceException(nameof(_colorAdjustment));
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         if(Input.GetButtonDown("Fire2"))
    31.         {
    32.             secondcamera = !secondcamera;
    33.  
    34.             if (secondcamera)
    35.             {
    36.                 _colorAdjustment.active = true;
    37.                 _colorAdjustment.postExposure = 1.2f;
    38.                 mainCamera.m_Priority = 9;
    39.             }
    40.  
    41.             else
    42.             {
    43.                 _colorAdjustment.active = false;
    44.                 mainCamera.m_Priority = 11;
    45.             }    
    46.         }
    47.     }
    48. }
     
    Last edited: Jul 21, 2020