Search Unity

GUIButton

Discussion in 'Immediate Mode GUI (IMGUI)' started by nooB, Nov 23, 2009.

  1. nooB

    nooB

    Joined:
    Oct 4, 2009
    Posts:
    20
    Hi Guys,


    Is there any easy way to change the texture on a GUIButton after a press on the Button.

    I have to randomly chose the textures from a list
     
  2. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    Since GUI is immediate mode, there is really nothing to "change". Each OnGUI, you draw the button from scratch -- you would just need generate a new random value for to select from an array of images when the button returns true, and use that image, for example.
     
  3. nooB

    nooB

    Joined:
    Oct 4, 2009
    Posts:
    20
    I guess I tried something similar. But the texture doesnt seem to change at all.


    This is
    Code (csharp):
    1.  
    2. if(GUI.Button (Rect (0,Screen.height - 75, 50, 60), controlTexture1)){
    3.  
    4.         GUI.Button (Rect (-50,Screen.height - 75, 50, 60), controlTexture6);
    5.     }
    6.  
    Do help me out if am doing something stupid. I am very new to Unity3D as well as prgramming.
     
  4. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    This is what I was thinking -- I didn't test it. When GUI.Button returns true, you change the reference to the image. Next time OnGUI is called, it is referencing a new image. In this, I'm selecting randomly from a list of textures.
    Code (csharp):
    1.  
    2. // a list of textures
    3. var controlTextures : Texture[];
    4. // an integer
    5. var button : int = 0;
    6. OnGUI(){
    7.    // pulled the Rect out to make it fit better on the forum
    8.    var r : Rect = new Rect(0, Screen.height - 75, 50, 60);
    9.    // our button was pressed
    10.    if (GUI.Button (r, controlTextures[button]){
    11.       // get a new random value for the texture
    12.       button = Random.Range(0, controlTextures.Length);
    13.    }
    14. }
    15.  
     
  5. nooB

    nooB

    Joined:
    Oct 4, 2009
    Posts:
    20
    Thanks a lot !!!
     
unityunity