Unity – Scripting – OnGUI – Toggle (Interruttore)
Videogames Development – Unity – Code GUI Objects – Toggle (Interruttore)
Text Toggle
1. Assign the script to the Camera:
1 2 3 4 5 6 7 8 9 10 11 | #pragma strict // Text Toggle START private var toggleTxt : boolean = false ; function OnGUI () { if (GUI.Toggle(Rect(10, 10, 100, 30), toggleTxt, "A Toggle text" )) { print ( "You clicked the toggle!" ); } // Text Toggle END } |
Statement:
GUI.Toggle(Rect(10, 10, 100, 30), toggleTxt, “A Toggle text”
GUI.Toggle(Rect(x, y, x, y), bool, “text content”
Image Toggle – Use images as buttons
1. Assign the script to the Camera:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #pragma strict // Image Toggle START var aTexture : Texture; private var toggleImg : boolean = false ; function OnGUI () { // Assign image from Inspector Controller if (!aTexture) { Debug.LogError( "Please assign a texture in the inspector." ); return ; } if (GUI.Toggle(Rect(10, 50, 50, 50), toggleImg, aTexture)) { print ( "You clicked the toggle!" ); } // Image Toggle END } |
2. Select the Camera> ‘Inspector’> Script> DRAG AND DROP a texture from ‘Assets’ to Script> ‘ATexture’ variable.
3. Play and enjoy!