Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

horizontal GUI slider to control game object transparency

Discussion in 'Immediate Mode GUI (IMGUI)' started by tobydog20, Jun 23, 2009.

  1. tobydog20

    tobydog20

    Joined:
    Jul 31, 2008
    Posts:
    71
    If I wanted to have a GUI slider to control the visibility of an object (from transparent to fully opaque), would I use a transparent shader or renderer.enabled?

    I haven't started researching this yet in the documentation, but was just considering where to begin.

    Thanks so much.
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    You'll want to use a transparent shader and then adjust the color (notably the alpha) from there. Toggling renderer.enabled is like an on/off switch, that doesn't seem to mesh with your desired use of a slider (if you wanted on/off then it might be better to use a toggle or something).
     
  3. tobydog20

    tobydog20

    Joined:
    Jul 31, 2008
    Posts:
    71
    Thanks H.

    so I did something like this:

    Code (csharp):
    1. var slider = 0;
    2. var a : float;
    3.  
    4. function OnGUI () {
    5.    
    6.     slider = GUI.HorizontalSlider( Rect(20,135,175,30), slider, 0, 255);
    7.    
    8.     if (slider <= 255){
    9.     a = 0;
    10.     }
    11.    
    12.     else if (slider > 0){
    13.     a = 1;
    14.     }
    15. }
    The slider doesn't change the alpha value at either end - do you know what's wrong here?

    Also,do you have any suggestions for how to make the alpha channel change incrementally with the slider instead of having values of 0 or 1?
     
  4. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    As Higgy said, you need to make sure you have a transparent shader applied. Then use code like this:

    Code (csharp):
    1. function OnGUI () {
    2.     slider = GUI.HorizontalSlider( Rect(20,135,175,30), slider, 0.0, 1.0);
    3.     renderer.material.color.a = slider;
    4. }
    Cheers.
     
  5. tobydog20

    tobydog20

    Joined:
    Jul 31, 2008
    Posts:
    71
    It worked like a charm.

    In attempt to have the fully visible version on the right side of the slider, I switched the 1.0 and 0 like so:

    Code (csharp):
    1. var slider : float;
    2.  
    3. function OnGUI () {
    4.    slider = GUI.HorizontalSlider( Rect(20,135,175,30), slider, 1.0, 0);
    5.    renderer.material.color.a = slider;
    6. }
    7.  
    However, it just switched the actual slider-head's start location to the 1.0 side (the right side).

    Do you know how to switch the location of the values so that 1 is on the left and 0 is on the right, but the slider-head begins on the left?

    Thanks Thylaxene for your help!
     
  6. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Set your initial slider value to be 1.0.

    Code (csharp):
    1. var slider : float = 1.0;
    2.  
    3. function OnGUI () {
    4.    slider = GUI.HorizontalSlider( Rect(20,135,175,30), slider, 1.0, 0);
    5.    renderer.material.color.a = slider;
    6. }
     
  7. DaFame

    DaFame

    Joined:
    Apr 24, 2009
    Posts:
    41
    I would solve it like that
    For example:

    Variables:
    Code (csharp):
    1.  
    2. Color defaultColor, transparancyColor;
    3. float trans;
    4.  
    on the top of your OnGUI()
    Code (csharp):
    1.  
    2. trans= GUI.VerticalScrollbar(new Rect(100, 100, 20, 100), panelTransparancy, 0.1f, 1.0f, 0.0f);
    3. transparancyColor.a = trans;
    4. GUI.color = transparancyColor;
    5.  
    and at the end, u need to init the colors, because if u dont, u just got black as color...
    so in the start method
    Code (csharp):
    1.  
    2. transparancyColor = GUI.color;
    3. defaultColor = GUI.color;
    4.  
    And if u want differents part with no transparancy ull wirte GUI.color = defaultColor;
    so ull get the def. colors back.

    All u want to change the transparany would be written after GUI.color = transparancyColor;
     
  8. tobydog20

    tobydog20

    Joined:
    Jul 31, 2008
    Posts:
    71
    thanks guys.

    Hb, I tried setting my initial float value to 1 and the sliderhead is still on the left at runtime.

    DaFame, I think the approach you suggested is over my head...but I still have to try it - I'll let you know,

    thanks again,
    M
     
  9. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    But isn't that what you asked for?

    If you want it on the left, set the initial value to 1.0, if you want it on the right, set the initial value to 0.0. The gist of this is that you're telling the slider to use the range of 1.0 to 0.0 (left to right), so just set your default as you want (1.0 for the left, 0.0 for the right, something in between for something in between). :)
     
  10. WiLsoN

    WiLsoN

    Joined:
    Jul 10, 2009
    Posts:
    9
    I tried to do the same thing with a vertical bar and a Texture2D.

    If i try :

    Code (csharp):
    1. my2DTexture.renderer.material.color.a = slider;
    I get :

    Code (csharp):
    1. Assets/Script perso/Perspikub.js(29,40): BCE0019: 'renderer' is not a member of 'UnityEngine.Texture2D'.
    2DTexture seems to be different from an object. Anybody knows how resolve this problem ? :wink:
     
  11. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Textures do not have a renderer property just as the error says, and as per the docs:

    Texture2D

    If this code is attached to the game object whose opacity you want to control then just access the renderer directly (drop my2DTexture. entirely). Remember, the opacity you're controlling has no relation at all to the texture itself. A game object uses a renderer component, that in turn uses a material to determine how it should be rendered (including a color and a texture among other properties).
     
  12. tobydog20

    tobydog20

    Joined:
    Jul 31, 2008
    Posts:
    71
    it's been a while, but now I'm back to working on this...

    Hb...per the old posts, I was having trouble with the slider coordinating with the slider head. I made it confusing in the last reply, but what I would like is for the object to be visible (slider value=1) at runtime (and the slider head can be wherever it wants - left or right), and then to be invisible (at 0) as the slider head moves towards the zero side.

    But the game object has a visibility value of 0 at runtime with the following code:
    Code (csharp):
    1. var slider : float=1;
    2. function OnGUI () {
    3.    slider = GUI.HorizontalSlider( Rect(20,135,175,30), slider, 1, 0);
    4.    renderer.material.color.a = slider;
    5. }
    How can I make the visibility be 1 at initial run time?
    Gracias in advance.
     
  13. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The default value of a variable set in a script is somewhat misleading. It will use that value only if the variable isn't subsequently set in the editor. Try setting the value of the slider variable to 1 in the inspector.
     
  14. tobydog20

    tobydog20

    Joined:
    Jul 31, 2008
    Posts:
    71
    it worked, thanks!
     
  15. RHD

    RHD

    Joined:
    Mar 30, 2009
    Posts:
    719
    I'm having some difficulty getting the script to talk to the shader.
    Also I get "Material doesn't have a color property '-Color' error from the Console.
    Can anyone advise?
    Thanks
     
  16. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If you've typed this exactly as it appeared, then the problem is that the first character should be an underscore (ie, "_Color", not "-Color").
     
  17. RHD

    RHD

    Joined:
    Mar 30, 2009
    Posts:
    719
    This is the script and the error and the slider shows up but it doesn't affect the shader:

    var slider : float = 0.5;


    function OnGUI () {
    slider = GUI.HorizontalSlider( Rect(20,135,175,30), slider, 0.0, 1.0);
    renderer.material.color.a = slider;
    }
     

    Attached Files:

  18. Bezzy

    Bezzy

    Joined:
    Apr 1, 2009
    Posts:
    75
    Not all shaders have a "_Color" property. You can do a "HasProperty" check first.
     
  19. stford

    stford

    Joined:
    Sep 7, 2010
    Posts:
    5
    got it I had two shaders with the same name


    ive been baning my head on this for hours I can control the RGB but not the alfa
    Ive tried switching the shaders and checking the alfas in the texture files
    HEEEEEEEELLLLLLLPP!!

    ribsSlidervalue = GUI.VerticalSlider ( Rect(710, 340 , 200, 60), ribsSlidervalue, 0.0, 1.0);
    //renderer.material.color.a = RibsSlidervalue;
    SliderVisiablity(ribsSlidervalue,ribsPart);
    }

    function SliderVisiablity(VisVal:float,bodyPart:GameObject)
    {
    // bodyPart.renderer.material.color.a = VisVal;
    //UnityEngine.Renderer:get_material(bodyPart);
    //renderer.get_material(bodyPart);
    //renderer.material.color.a = VisVal;
    bodyPart.renderer.sharedMaterial.color.a =VisVal;
    //bodyPart.renderer.sharedMaterial.color.r =VisVal ;
    // bodyPart.renderer.sharedMaterial.color.g =VisVal;
    // bodyPart.renderer.sharedMaterial.color.b =VisVal;
    // RibsM.renderer.material.color.a = 0.5;
    print(bodyPart.renderer.sharedMaterial.color.a);
    print(VisVal);
    // print(bodyPart);
    }
     
    Last edited: Oct 26, 2011
  20. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264
    Control Object Transparency with UI Slider