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

Newbie question: draw a red button by script

Discussion in 'Scripting' started by FirebladeBR, Apr 25, 2014.

  1. FirebladeBR

    FirebladeBR

    Joined:
    Apr 21, 2014
    Posts:
    65
    Hi all,

    I'am having a hard time finding a way to create a custom button via script. I know you can create a new guiskin in the assets menu, but I want to do it dynamically.

    Let's say for example that a player will have a red button and another a random colored button, And the style will have to be done via script depending on the game circunstance.

    How can I do a custom gui purely via script?

    Thanks
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    A GUI skin is a collection of GUI styles. You can create a new GUI style in script:
    Code (csharp):
    1.  
    2. GUIstyle buttonStyle = new GUIStyle(GUI.skin.button); // Based off default button style.
    3. buttonStyle.normal.background = randomNormalButtonBackgroundTexture;
    4. buttonStyle.hover.background = randomHoverButtonBackgroundTexture;
    5. ...
    6. GUI.Button(rect, "Button Text", buttonStyle);
    7.  
    Set the style states (normal, hover, active, etc.) that you need for your application.

    If you only need one texture, you can use an alternate form of GUI.Button():
    Code (csharp):
    1.  
    2. GUI.Button(rect, "Button Text", myRandomTexture);
    3.  
     
  3. FirebladeBR

    FirebladeBR

    Joined:
    Apr 21, 2014
    Posts:
    65
    Thanks for the quick answer.

    Another question: How do I change the border of the button, so that it has no round corners? (via script again)

    And how do you create a texture with a random color using js?

    Thanks
     
    Last edited: Apr 25, 2014
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Use a different texture for your button style. The textures in the default GUI skin use rounded corners.

    If you know the colors ahead of time, in your script you could define a public array of Texture2D's. Then create separate textures for each color, and in the inspector assign the textures to the array. When you set up the GUI style, randomly choose one of the textures from the array.

    If you'll only have one button of this type on the screen, you can use SetPixels() to change the texture directly. If you have multiple randomly-colored buttons onscreen, you'll want to create a texture for each and use SetPixels() to set their colors.
     
  5. FirebladeBR

    FirebladeBR

    Joined:
    Apr 21, 2014
    Posts:
    65
    Can I Load the texture dynamically, like from my website? So then I could use in the button?

    Thanks for the help!
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Use WWW(). The example on the manual page shows how to load a texture. And happy to help!