Pages

Sunday, June 26, 2011

GUI Basics For The Unity3d

Making Controls with UnityGUI

UnityGUI controls make use of a special function called OnGUI(). The OnGUI() function gets called every frame as long as the containing script is enabled - just like the Update() function.

Code :

function OnGUI () 
{
    // Make a background box
       GUI.Box (Rect (10,10,100,90), "Loader Menu");
   if (GUI.Button (Rect (20,40,80,20), "Level 1")) 
{
        Application.LoadLevel (1);
      }
   // Make the second button.
   if (GUI.Button (Rect (20,70,80,20), "Level 2")) 
      {
        Application.LoadLevel (2);
      }
}
 
It Will Look Like This :  
 
 
 
 
 
Now Controls in GUI : 

How To add Lable in GUI ?
 
-->  CODE : 
function OnGUI ()
{
   GUI.Label (Rect (0,0,100,50), "This is the text string for a Label Control");
 }
 
If you Want to Add Texture2D then it calls like 
 
--> var controlTexture : Texture2D;

function OnGUI () 
{
    GUI.Label (Rect (0,0,100,50), controlTexture);
}  
 
How To Add Button in GUI Function ??
--> Code : 
 var icon : Texture2D;

function OnGUI ()
{
    if (GUI.Button (Rect (10,10, 100, 50), icon)) 
{
   print ("you clicked the icon");
       }

    if (GUI.Button (Rect (10,70, 100, 20), "This is text")) 
{
  print ("you clicked the text button");
       }
} 
 
 
---
Vivek P Shah
Game Developer in Unity3d  


1 comment:

  1. Stolen! from here
    https://docs.unity3d.com/Documentation/Components/GUIScriptingGuide.html

    ReplyDelete