Search Unity

Change background of GUI.DrawTexture while in game

Discussion in 'Scripting' started by SeeleyBooth, Apr 18, 2014.

  1. SeeleyBooth

    SeeleyBooth

    Joined:
    Jan 23, 2014
    Posts:
    30
    When my game starts i have some code which draws 2 textures that I adjust the length of to display stamina. It works perfect.
    Code (csharp):
    1. void OnGUI() {
    2.         GUI.DrawTexture (new Rect (10, 10, curStaminaBG, barHeights), staminaBarBG);
    3.         GUI.DrawTexture (new Rect (10, 10, displayStamina, barHeights), staminaBar);
    4.     }
    What I would like to able to do is to change the texture when say a bool is set to true. So if I take this line from the above:
    Code (csharp):
    1. GUI.DrawTexture (new Rect (10, 10, displayStamina, barHeights), staminaBar);
    When my bool goes to true I want it to change to:
    Code (csharp):
    1. GUI.DrawTexture (new Rect (10, 10, displayStamina, barHeights), newBackground);
    Seeing as how they are drawn in the ongui function can I even do that?
     
  2. SeeleyBooth

    SeeleyBooth

    Joined:
    Jan 23, 2014
    Posts:
    30
    Totally just figured it out. By adding a new texture2d and just change the variable with if statements in the update function all is working now!
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    There's nothing stopping you from doing it in OnGUI either.

    Code (csharp):
    1.  
    2. if (someBool)
    3.     GUI.DrawTexture(rect, tex1);
    4. else
    5.     GUI.DrawTexture(rect, tex2);
    6.  
     
  4. SeeleyBooth

    SeeleyBooth

    Joined:
    Jan 23, 2014
    Posts:
    30
    Perhaps I missed it in the docs, onGui is drawn every frame?
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It's run multiple times per frame based on different GUI events (input, layout, paint, etc)
     
  6. SeeleyBooth

    SeeleyBooth

    Joined:
    Jan 23, 2014
    Posts:
    30
    Thanks for the help. I will re read the docs