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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Access Color Adjustment hue shift per script

Discussion in 'Scripting' started by LifeSymbiont, Dec 4, 2021.

  1. LifeSymbiont

    LifeSymbiont

    Joined:
    Apr 19, 2015
    Posts:
    36
    Hello, I am trying to add a script to my game that changes the hueshift of my color adjustment post processing effect +1 F per second. So the whole game changes color the whole time.

    How exactly do I access this via code?

    What I currently have:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering;
    5. using UnityEngine.Rendering.Universal;
    6.  
    7. public class AllColorShift : MonoBehaviour
    8. {  
    9.     public volume myvolume;
    10.  
    11.     void Update()
    12.     {
    13.         ColorAdjusment CA;
    14.         if(volume.profile.tryGet<ColorAdjusment>(out CA))
    15.         {
    16.             CA.hueshift.
    17.         }
    18.     }
    19. }
    20.  
    Please help me.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    The new post processing system is really finicky for this kind of real-time updating, but give something like this a try:
    Code (csharp):
    1.  
    2.    public Volume volume;
    3.    public float timeScale = 1f;
    4.  
    5.    private ColorAdjustments colorAdjustments;
    6.    private VolumeParameter<float> hueShift = new VolumeParameter<float>();
    7.  
    8.    void Start()
    9.    {
    10.        volume.profile.TryGet<ColorAdjustments>(out colorAdjustments);
    11.  
    12.        if(colorAdjustments == null)
    13.            Debug.LogError("No ColorAdjustments found on profile");
    14.  
    15.    }
    16.  
    17.    void Update()
    18.    {
    19.        if(colorAdjustments == null)
    20.            return;
    21.  
    22.        hueShift.value = -180f + Mathf.PingPong(Time.time * timeScale, 360f);
    23.  
    24.        colorAdjustments.hueShift.SetValue(hueShift);
    25.    }
    26.  
    I typed it here so no guarantees it works or is typo-free, but it should get you started.
     
  3. LifeSymbiont

    LifeSymbiont

    Joined:
    Apr 19, 2015
    Posts:
    36
    Thank you for your reply. I changed the code a little without error but also without it working.

    The code currently sets the hueshift value to the first float, to say, the -180 (in my case the 0), but it does not update it. It just stays there.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering;
    5. using UnityEngine.Rendering.HighDefinition;
    6.  
    7. public class AllColorShift : MonoBehaviour
    8. {
    9.    public Volume volume;
    10.    public float timeScale = 1f;
    11.    private VolumeParameter<float> hueShift = new VolumeParameter<float>();
    12.    public ColorAdjustments CA;
    13.  
    14.    void Start()
    15.    {
    16.  
    17.        volume.profile.TryGet<ColorAdjustments>(out CA);
    18.        if(CA == null)
    19.            Debug.LogError("No ColorAdjustments found on profile");
    20.    }
    21.    void Update()
    22.    {
    23.        if(CA == null)
    24.        {
    25.             return;
    26.        }
    27.  
    28.        hueShift.value = 0 + Mathf.PingPong(timeScale * Time.deltaTime, 360f);
    29.        CA.hueShift.SetValue(hueShift);
    30.    }
    31. }
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    The Mathf.PingPong in your version doesn't have a growing value, so it's going to be static. If you want it to bounce between 0 and 180 only, not -180 to 180, then use:
    hueShift.value = Mathf.PingPong(Time.time * timeScale, 180f);


    Otherwise the code is working fine from what I can tell, just tested it in 2019.4 LTS.
     
  5. LifeSymbiont

    LifeSymbiont

    Joined:
    Apr 19, 2015
    Posts:
    36
    Thank you a ton for your help, it works now.
     
    GroZZleR likes this.