Search Unity

Change fill color of slider at runtime?

Discussion in 'Immediate Mode GUI (IMGUI)' started by vekkna, Aug 27, 2014.

  1. vekkna

    vekkna

    Joined:
    Apr 6, 2014
    Posts:
    6
    Does anybody know how to change the fill color of a slider in Unity 4.6 at runtime?

    Any help gratefully received.
     
  2. ShawnFeatherly

    ShawnFeatherly

    Joined:
    Feb 22, 2013
    Posts:
    57
    This worked for me, at the top be sure to add "using System.Linq;":

    var fill = (slider as UnityEngine.UI.Slider).GetComponentsInChildren<UnityEngine.UI.Image>()
    .FirstOrDefault(t => t.name == "Fill");
    if (fill != null)
    {
    fill.color = Color.Lerp(Color.red, Color.green, 0.5);
    }
     
    JPBA1984, kdrkrndr and vekkna like this.
  3. vekkna

    vekkna

    Joined:
    Apr 6, 2014
    Posts:
    6
    Thanks a lot, that worked.
     
  4. jepriddy

    jepriddy

    Joined:
    Dec 9, 2014
    Posts:
    4
    How would we do this in java?
     
  5. bryanhood

    bryanhood

    Joined:
    Nov 6, 2014
    Posts:
    1
    change the using System.Linq; to import using System.Linq;
    then copy the code and past it where do you want
     
  6. JPBA1984

    JPBA1984

    Joined:
    Jan 23, 2014
    Posts:
    6
    Works perfect, the only thing i changed was to get a hold on the image on start/wake to a property to avoid calling this function on update, i also pass the colors as a parameter. thanks.