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

Change Color keys on a gradient

Discussion in 'Scripting' started by Phantom_X, Oct 13, 2016.

  1. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    305
    Hey,
    I'm trying to tint the color keys on a gradient startColor on a particle system by code. It doesn't seem to let me.
    here is what I'm doing:

    Code (CSharp):
    1.  
    2. ParticleSystem ps = getComponent<ParticleSystem>();
    3. var psMain = ps.main;
    4. var grad = psMain.startColor.gradient;
    5. grad.colorKeys[0].color = Color.blue;
    6.                  
    7. psMain.startColor = grad;
    8.  
    So this just does nothing at all. But if I create a new empty Gradient and assign it the same way it works.
    I just don't want to create a new gradient and copy every keys every time I want to change the color of one key.

    Is there a way to do this?

    (Unity 5.5)
    Thanks!
     
    Last edited: Oct 13, 2016
  2. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    305
    Ok, I had to use SetKeys() on the gradient to update the keys
     
  3. frattapa

    frattapa

    Joined:
    May 30, 2018
    Posts:
    6
    Code (CSharp):
    1.         List<GradientColorKey> keys = m_Gradient.colorKeys.ToList();
    2.  
    3.         keys[0] = new GradientColorKey(NewColor, m_Gradient.colorKeys[0].time);
    4.         keys[m_Gradient.colorKeys.Length - 1] = new GradientColorKey(NewColor, m_Gradient.colorKeys[m_Gradient.colorKeys.Length - 1].time);
    5.      
    6.         m_Gradient.SetKeys(keys.ToArray(), m_Gradient.alphaKeys);
    If someone is still looking for a solution, this is how I changed the first and last keys of a gradient. Perhaps it can be of some help! :)
     
    FlashMuller likes this.
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,452
    This:
    Code (CSharp):
    1. keys[m_Gradient.colorKeys.Length - 1] =...
    really should be
    Code (CSharp):
    1. keys[keys.Count - 1] =...
    Note that "colorKeys" is a property that is calling a native method. This does create a new array each time it is read since the actual keys have to be copied from the native side into managed memory. I haven't really tried it, but I don't think you have to use SetKeys at all, especially when you just want to change the color keys. The colorKeys property does have a setter as well. So you can simply do

    Code (CSharp):
    1. GradientColorKey[] keys = m_Gradient.colorKeys;
    2. keys[0].color = NewColor;
    3. keys[keys.Length - 1].color = NewColor;
    4. m_Gradient.colorKeys = keys;
    Note: changing the "color" of a GradientColorKey directly only works when using an array. When using a List it would not work because GradientColorKey is a struct and a List uses an indexer (get and set methods) to access the elements of the list. Therefore you get a local copy of the key and changing that won't affect the actual element in the list. This does work with an array though. If you really need / want to use a List (only if you need to insert or remove keys) you would could use the GradientColorKey constructor to essentially replace the whole key, or do the same thing, read the key into a local variable, change the color and assign the key back to the same element. This would have the same effect.
     
    FlashMuller likes this.
  5. StripeGuy

    StripeGuy

    Joined:
    Dec 30, 2016
    Posts:
    52
    For anyone trying this, this will not work unfortunately. I will post my solution as an edit as soon I figure it out.