Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Enable/Disable Post Processing Effects

Discussion in 'Scripting' started by unity_Ug-b95TzTbCuYQ, Mar 28, 2020.

  1. unity_Ug-b95TzTbCuYQ

    unity_Ug-b95TzTbCuYQ

    Joined:
    Mar 6, 2020
    Posts:
    7
    Hi there,

    I would like to build a little menu to set the quality of my game. In this settings it should be possible to enable/disable effects like some post processings. Let's say there is a Bloom Effect and I want to enable and disable this one at runtime. How can I get access to it in a script.

    inspector.png

    I found a script but it doesn't work for me. It looks like this and I added this script to the Object, where the Bloom effect is added.

    Code (CSharp):
    1. Bloom bloomLayer;
    2.  
    3. PostProcessVolume volume = GetComponent<PostProcessVolume>();
    4. volume.profile.TryGetSettings(out bloomLayer);

    How is it possible to disable this effect via script?

    Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    It appears the
    Bloom
    object is a
    Component
    on a
    GameObject
    . Make your
    Bloom
    variable public and drag that
    GameObject
    into it, or else use either
    .GetComponent<Bloom>()
    or
    FindObjectOfType<Bloom>()
    , and once you have a valid reference, I would imagine you could enable and disable it by setting the
    .enabled
    property in code.

    Disclaimer: I have not used this particular class myself but I assume it works like 99.9% of everything else in Unity3D.
     
  3. unity_Ug-b95TzTbCuYQ

    unity_Ug-b95TzTbCuYQ

    Joined:
    Mar 6, 2020
    Posts:
    7
    Thanks, but no, that doesn't work. I tried it before and in many different ways. That's the reason, why I create this post. :-/
     
  4. raghadalghonaim

    raghadalghonaim

    Joined:
    Apr 22, 2020
    Posts:
    54
    Same here. Did you manage to solve it?
     
  5. Uriel_Theodren

    Uriel_Theodren

    Joined:
    May 31, 2019
    Posts:
    1
    i d like to know too
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,194
    Take OP's post and add this to the end:

    Code (csharp):
    1. bloomLayer.enabled.value = false;
     
  7. juraj123

    juraj123

    Joined:
    Feb 15, 2017
    Posts:
    3
    I still cannot get it to work properly, the best way is probably to add each effect to its own volume and then just disable the volume gameobject. But I would like to know some better and faster options.
     
  8. wlad_s

    wlad_s

    Joined:
    Feb 18, 2011
    Posts:
    148
  9. atulvi

    atulvi

    Joined:
    Oct 28, 2020
    Posts:
    35
    Code (CSharp):
    1. using UnityEngine.Rendering.PostProcessing;
    2. using UnityEngine;
    3.  
    4. public class PostprocessingExtension : MonoBehaviour
    5. {
    6.           [SerializeField]private PostProcessProfile postProcessProfile;
    7.        
    8.        
    9.           void Start()
    10.            {
    11.                 PostProcessingProfileIsEnabled(false,postProcessProfile);//true or false
    12.            }
    13.             private void PostProcessingProfileIsEnabled(bool isEnabled,PostProcessProfile postProcessProfile)
    14.            {
    15.                if(postProcessProfile == null) return;
    16.                foreach (var item in postProcessProfile.settings)
    17.               {
    18.                        item.enabled.value = isEnabled;
    19.                }
    20.            }
    21. }

    OR

    Below best solution

    Code (CSharp):
    1.  
    2. using UnityEngine.Rendering.PostProcessing;
    3. using UnityEngine;
    4.  
    5. public class PostprocessingExtension : MonoBehaviour
    6. {
    7.          
    8.          [SerializeField]private PostProcessLayer postProcessLayer;
    9.        
    10.           void Start()
    11.            {
    12.                 SetPostProcessingLayerIsEnabled(false);//true or false
    13.            }
    14.          
    15.          public void SetPostProcessingLayerIsEnabled(bool _value)
    16.         {
    17.             if(postProcessLayer == null) return;
    18.                postProcessLayer.enabled = _value;
    19.        }
    20. }
     
    Last edited: Mar 17, 2021
    DungDajHjep likes this.
  10. Kaldrin

    Kaldrin

    Joined:
    Jul 10, 2018
    Posts:
    42
    You saved me thanks!
     
  11. beaaaat

    beaaaat

    Joined:
    Sep 7, 2020
    Posts:
    1
    [SerializeField]
    private PostProcessVolume _postProcessVolume;
    private AmbientOcclusion _ambientOcclusion;
    private MotionBlur _motionBlur;
    private Bloom _bloom;

    private void Start()
    {
    _postProcessVolume.profile.TryGetSettings(out _ambientOcclusion);
    _postProcessVolume.profile.TryGetSettings(out _motionBlur);
    _postProcessVolume.profile.TryGetSettings(out _bloom);
    }
    public void AmbientOccOnOff(bool value)
    {
    _ambientOcclusion.active = value;
    }
    public void BloomOnOff(bool value)
    {
    _bloom.active = value;
    }
    public void MotionBlurOnOff(bool value)
    {
    _motionBlur.active = value;
    }
    }