Gui custom button JavaScript code needed

Asked By 50 points N/A Posted on -
qa-featured

HI,

I am making dynamic Graphical user interface. I am trying to create a button that has a custom shape by using java script but it create some problem. if any body know how to create gui custom button java script or has code of Gui custom button JavaScript then please help me and solve my problem.

SHARE
Answered By 5 points N/A #135867

Gui custom button JavaScript code needed

qa-featured

Hi Lee,

The control functions of almost all GUIs have an optional parameter called ‘GUIStyle’ which is used to display the Control. If this is omitted, Unity's default ‘GUIStyle’ will be used. This applies the control type name as a string. So the ‘GUI.Button()’ uses the "button" style and the ‘GUI. Toggle()’ uses the "toggle" style and more like this. One can overwrite the default ‘GUIStyle’ for a control while specifying it as the last parameter.

To replace the default Control Style with a different style in the ‘UnityGUI’, you can use the following code:


Script in JavaScript –
”function OnGUI () {
      // Make a label that uses the "box" GUIStyle.
      GUI.Label (Rect (0,0,200,100), "Hi – I'm a label looking like a box", "box");

      // Make a button that uses the "toggle" GUIStyle
      GUI.Button (Rect (10,140,180,20), "This is a button", "toggle");
}”

 To get a custom Button, you can make a public variable with “GUIStyle”. While declaring a public ‘GUIStyle’ variable, all elements of the Style should show up in the ‘Inspector’. You should be able to edit almost all of the different values here.

To override the default Control Style with the one you've defined yourself follow the below code:

Script in JavaScript –
”var customButton : GUIStyle;”

”function OnGUI () {
            // Make a button. We pass in the GUIStyle defined above as the style to use
            GUI.Button (Rect (10,10,150,20), "I am a Custom Button", customButton);
}”

Related Questions