There are a number of different IMGUI Controls that you can create. This section lists all of the available display and interactive Controls. There are other IMGUI functions that affect layout of Controls, which are described in the Layout section of the Guide.
The Label is non-interactive. It is for display only. It cannot be clicked or otherwise moved. It is best for displaying information only.
/* GUI.Label example */ using UnityEngine; using System.Collections; public class GUITest : MonoBehaviour { void OnGUI () { GUI.Label (new Rect (25, 25, 100, 30), "Label"); } }
The Button is a typical interactive button. It will respond a single time when clicked, no matter how long the mouse remains depressed. The response occurs as soon as the mouse button is released.
In UnityGUI, Buttons will return true when they are clicked. To execute some code when a Button is clicked, you wrap the the GUI.Button function in an if statement. Inside the if statement is the code that will be executed when the Button is clicked.
/* GUI.Button example */ using UnityEngine; using System.Collections; public class GUITest : MonoBehaviour { void OnGUI () { if (GUI.Button (new Rect (25, 25, 100, 30), "Button")) { // This code is executed when the Button is clicked } } }
RepeatButton is a variation of the regular Button. The difference is, RepeatButton will respond every frame that the mouse button remains depressed. This allows you to create click-and-hold functionality.
In UnityGUI, RepeatButtons will return true for every frame that they are clicked. To execute some code while the Button is being clicked, you wrap the the GUI.RepeatButton function in an if statement. Inside the if statement is the code that will be executed while the RepeatButton remains clicked.
/* GUI.RepeatButton example */ using UnityEngine; using System.Collections; public class GUITest : MonoBehaviour { void OnGUI () { if (GUI.RepeatButton (new Rect (25, 25, 100, 30), "RepeatButton")) { // This code is executed every frame that the RepeatButton remains clicked } } }
The TextField Control is an interactive, editable single-line field containing a text string.
The TextField will always display a string. You must provide the string to be displayed in the TextField. When edits are made to the string, the TextField function will return the edited string.
/* GUI.TextField example */ using UnityEngine; using System.Collections; public class GUITest : MonoBehaviour { private string textFieldString = "text field"; void OnGUI () { textFieldString = GUI.TextField (new Rect (25, 25, 100, 30), textFieldString); } }
The TextArea Control is an interactive, editable multi-line area containing a text string.
The TextArea will always display a string. You must provide the string to be displayed in the TextArea. When edits are made to the string, the TextArea function will return the edited string.
/* GUI.TextArea example */ using UnityEngine; using System.Collections; public class GUITest : MonoBehaviour { private string textAreaString = "text area"; void OnGUI () { textAreaString = GUI.TextArea (new Rect (25, 25, 100, 30), textAreaString); } }
The ToggleA checkbox that allows the user to switch an option on or off. More info
See in Glossary Control creates a checkbox with a persistent on/off state. The user can change the state by clicking on it.
The Toggle on/off state is represented by a true/false boolean. You must provide the boolean as a parameter to make the Toggle represent the actual state. The Toggle function will return a new boolean value if it is clicked. In order to capture this interactivity, you must assign the boolean to accept the return value of the Toggle function.
/* GUI.Toggle example */ using UnityEngine; using System.Collections; public class GUITest : MonoBehaviour { private bool toggleBool = true; void OnGUI () { toggleBool = GUI.Toggle (new Rect (25, 25, 100, 30), toggleBool, "Toggle"); } }
The ToolbarA row of buttons and basic controls at the top of the Unity Editor that allows you to interact with the Editor in various ways (e.g. scaling, translation). More info
See in Glossary Control is essentially a row of Buttons. Only one of the Buttons on the Toolbar can be active at a time, and it will remain active until a different Button is clicked. This behavior emulates the behavior of a typical Toolbar. You can define an arbitrary number of Buttons on the Toolbar.
The active Button in the Toolbar is tracked through an integer. You must provide the integer as an argument in the function. To make the Toolbar interactive, you must assign the integer to the return value of the function. The number of elements in the content array that you provide will determine the number of Buttons that are shown in the Toolbar.
/* GUI.Toolbar example */ using UnityEngine; using System.Collections; public class GUITest : MonoBehaviour { private int toolbarInt = 0; private string[] toolbarStrings = {"Toolbar1", "Toolbar2", "Toolbar3"}; void OnGUI () { toolbarInt = GUI.Toolbar (new Rect (25, 25, 250, 30), toolbarInt, toolbarStrings); } }
The SelectionGrid Control is a multi-row Toolbar. You can determine the number of columns and rows in the grid. Only one Button can be active at time.
The active Button in the SelectionGrid is tracked through an integer. You must provide the integer as an argument in the function. To make the SelectionGrid interactive, you must assign the integer to the return value of the function. The number of elements in the content array that you provide will determine the number of Buttons that are shown in the SelectionGrid. You also can dictate the number of columns through the function arguments.
/* GUI.SelectionGrid example */ using UnityEngine; using System.Collections; public class GUITest : MonoBehaviour { private int selectionGridInt = 0; private string[] selectionStrings = {"Grid 1", "Grid 2", "Grid 3", "Grid 4"}; void OnGUI () { selectionGridInt = GUI.SelectionGrid (new Rect (25, 25, 300, 60), selectionGridInt, selectionStrings, 2); } }
The HorizontalSlider Control is a typical horizontal sliding knob that can be dragged to change a value between predetermined min and max values.
The position of the Slider knob is stored as a float. To display the position of the knob, you provide that float as one of the arguments in the function. There are two additional values that determine the minimum and maximum values. If you want the slider knob to be adjustable, assign the slider value float to be the return value of the Slider function.
/* Horizontal Slider example */ using UnityEngine; using System.Collections; public class GUITest : MonoBehaviour { private float hSliderValue = 0.0f; void OnGUI () { hSliderValue = GUI.HorizontalSlider (new Rect (25, 25, 100, 30), hSliderValue, 0.0f, 10.0f); } }
The VerticalSlider Control is a typical vertical sliding knob that can be dragged to change a value between predetermined min and max values.
The position of the Slider knob is stored as a float. To display the position of the knob, you provide that float as one of the arguments in the function. There are two additional values that determine the minimum and maximum values. If you want the slider knob to be adjustable, assign the slider value float to be the return value of the Slider function.
/* Vertical Slider example */ using UnityEngine; using System.Collections; public class GUITest : MonoBehaviour { private float vSliderValue = 0.0f; void OnGUI () { vSliderValue = GUI.VerticalSlider (new Rect (25, 25, 100, 30), vSliderValue, 10.0f, 0.0f); } }
The HorizontalScrollbar Control is similar to a Slider Control, but visually similar to Scrolling elements for web browsers or word processors. This control is used to navigate the ScrollViewA UI Control which displays a large set of Controls in a viewable area that you can see by using a scrollbar. More info
See in Glossary Control.
Horizontal Scrollbars are implemented identically to Horizontal Sliders with one exception: There is an additional argument which controls the width of the Scrollbar knob itself.
/* Horizontal Scrollbar example */ using UnityEngine; using System.Collections; public class GUITest : MonoBehaviour { private float hScrollbarValue; void OnGUI () { hScrollbarValue = GUI.HorizontalScrollbar (new Rect (25, 25, 100, 30), hScrollbarValue, 1.0f, 0.0f, 10.0f); } }
The VerticalScrollbar Control is similar to a Slider Control, but visually similar to Scrolling elements for web browsers or word processors. This control is used to navigate the ScrollView Control.
Vertical Scrollbars are implemented identically to Vertical Sliders with one exception: There is an additional argument which controls the height of the Scrollbar knob itself.
/* Vertical Scrollbar example */ using UnityEngine; using System.Collections; public class GUITest : MonoBehaviour { private float vScrollbarValue; void OnGUI () { vScrollbarValue = GUI. VerticalScrollbar (new Rect (25, 25, 100, 30), vScrollbarValue, 1.0f, 10.0f, 0.0f); } }
ScrollViews are Controls that display a viewable area of a much larger set of Controls.
ScrollViews require two Rects as arguments. The first Rect defines the location and size of the viewable ScrollView area on the screen. The second Rect defines the size of the space contained inside the viewable area. If the space inside the viewable area is larger than the viewable area, Scrollbars will appear as appropriate. You must also assign and provide a 2D Vector which stores the position of the viewable area that is displayed.
/* ScrollView example */ using UnityEngine; using System.Collections; public class GUITest : MonoBehaviour { private Vector2 scrollViewVector = Vector2.zero; private string innerText = "I am inside the ScrollView"; void OnGUI () { // Begin the ScrollView scrollViewVector = GUI.BeginScrollView (new Rect (25, 25, 100, 100), scrollViewVector, new Rect (0, 0, 400, 400)); // Put something inside the ScrollView innerText = GUI.TextArea (new Rect (0, 0, 400, 400), innerText); // End the ScrollView GUI.EndScrollView(); } }
Windows are drag-able containers of Controls. They can receive and lose focus when clicked. Because of this, they are implemented slightly differently from the other Controls. Each Window has an id number, and its contents are declared inside a separate function that is called when the Window has focus.
Windows are the only Control that require an additional function to work properly. You must provide an id number and a function name to be executed for the Window. Inside the Window function, you create your actual behaviors or contained Controls.
/* Window example */ using UnityEngine; using System.Collections; public class GUITest : MonoBehaviour { private Rect windowRect = new Rect (20, 20, 120, 50); void OnGUI () { windowRect = GUI.Window (0, windowRect, WindowFunction, "My Window"); } void WindowFunction (int windowID) { // Draw any Controls inside the window here } }
To detect if the user did any action in the GUI (clicked a button, dragged a slider, etc), read the GUI.changed value from your script. This gets set to true when the user has done something, making it easy to validate the user input.
A common scenario would be for a Toolbar, where you want to change a specific value based on which Button in the Toolbar was clicked. You don’t want to assign the value in every call to OnGUI(), only when one of the Buttons has been clicked.
/* GUI.changed example */ using UnityEngine; using System.Collections; public class GUITest : MonoBehaviour { private int selectedToolbar = 0; private string[] toolbarStrings = {"One", "Two"}; void OnGUI () { // Determine which button is active, whether it was clicked this frame or not selectedToolbar = GUI.Toolbar (new Rect (50, 10, Screen.width - 100, 30), selectedToolbar, toolbarStrings); // If the user clicked a new Toolbar button this frame, we'll process their input if (GUI.changed) { Debug.Log("The toolbar was clicked"); if (0 == selectedToolbar) { Debug.Log("First button was clicked"); } else { Debug.Log("Second button was clicked"); } } } }
GUI.changed will return true if any GUI Control placed before it was manipulated by the user.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.