Skip to content

Instantly share code, notes, and snippets.

@simonrcodrington
Last active November 2, 2020 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonrcodrington/e9a0d6e31b8360e5cee8dba758671fb1 to your computer and use it in GitHub Desktop.
Save simonrcodrington/e9a0d6e31b8360e5cee8dba758671fb1 to your computer and use it in GitHub Desktop.
Unity URP Disabling volume components at run time
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
/// <summary>
/// Unity's URP profile uses 'Volumes' attached to the camera to apply it's new post processing effects.
/// Below is how you can access one of the components(e.g the depth of field component) and disable it at runtime
/// </summary>
class urp_dof_conditional : MonoBehaviour
{
[Tooltip("Determines if the selected camera will have it's DOF enabled or disabled")]
public bool dofEnabled = true;
[Tooltip("The camera with a URP volume attached to it")]
public Camera targetCamera;
/// <summary>
/// On start, enable or disable to DOF attached to the target camera
/// </summary>
public void Start()
{
//get the volume attached to the main camera
VolumeProfile volumeProfile = targetCamera.GetComponent<Volume>().profile;
//get the DOF component from the volume and se it
DepthOfField depthOfField;
volumeProfile.TryGet(out depthOfField);
if(depthOfField != null) depthOfField.active = dofEnabled;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment