Unity3D basics-common GUI controls, unity3dgui controls

Source: Internet
Author: User

Unity3D basics-common GUI controls, unity3dgui controls

In Unity3D, the GUI part is erased and re-painted at each frame. You should only draw the GUI in the OnGUI, and the variable values declared in the OnGUI method will not be saved every time. For example, you can call the following code to draw a text edit box in Unity: GUILayout. textField ("text"); however, after running, we will find that no matter what we enter, only the text string will be displayed. This is because the strings we entered in the previous frame are not saved. In the next frame, all strings are erased and re-painted, that is, a "text" string is re-drawn. Solution: we can save the input values in the member variables of the class and initialize text = GUILayout. TextField (text) in the Start method. Next we will introduce several commonly used controls.

1. Button: GUILayout. Button ("Hello"); this method returns a Boolean value. Only true is returned when the Button is lifted, and false is returned when the Button is pressed.

2. Repeat Button: GUILayout. RepeatButton ("Hello"); different from the Button, this method always returns true when we press the Button. This method is suitable for bullet reflection buttons.

3. Password box: pwd = GUILayout. PasswordField (pwd, '*'). The second parameter is a mask character.

4. Tab: selectedToolBarId = GUILayout. Toolbar (selectedToolBarId, new string [] {"Tab1", "Tab2", "Tab3"}); return value is the serial number of the activated button. Based on the label serial number, we can draw the corresponding Tab page.

Using UnityEngine; using System. collections; public class GUITest: MonoBehaviour {private int selectedToolBarId; // Use this for initializationvoid Start () {selectedToolBarId = 0;} // Update is called once per framevoid Update () {} void OnGUI () {selectedToolBarId = GUILayout. toolbar (selectedToolBarId, new string [] {"Tab1", "Tab2", "Tab3"}); switch (selectedToolBarId) {case 0: GUILayout. label ("selected tab1"); // The content break in Tab1 is drawn here; case 1: GUILayout. label ("selected tab2"); // The content break in Tab2 is drawn here; case 2: GUILayout. label ("selected tab3"); // The content break in Tab3 is drawn here ;}}}
5. single vertex Toggle, return a Boolean value indicating the selected status.
if (isMuted) {isMuted = GUILayout.Toggle(isMuted, "Yes");} else {isMuted = GUILayout.Toggle(isMuted, "No");}
6. slide bar: vertical, sliderValue = GUILayout. VerticalSlider (sliderValue, 0,100); Return Value: current value; second parameter: Minimum value; third parameter: maximum value. HorizontalSlider () Transverse.

7. Area, which is equivalent to a control box. The control in Area moves along with Area. BeginArea () starts an Area. The parameter specifies the Area size and coordinates, and the EndArea () ends;

GUILayout.BeginArea (new Rect (50, 50, 100, 100));GUILayout.Label("Area");GUILayout.EndArea ();
8. There is no border or title in the window and the area cannot be dragged or dragged. GUILayout. window (0, new Rect (50, 50, 200,200), AddWindow1, "my Window"); the first parameter is the number of the Window, and the second parameter is the size of the Window, the third is the void WindowFunction (int windowId) Delegate, which is used to draw the window content.

Window dragging: Call GUI. DragWindow () at the end of WindowFunction to enable full-screen drag-and-drop (send Rect parameters to DragWindow to set the drag-and-drop areas ). To refresh the frame, you must save the Rect returned by Window () in OnGUI before you can drag the Rect and set the initial position in Start.

using UnityEngine;using System.Collections;public class GUITest : MonoBehaviour {private Rect positionRect;// Use this for initializationvoid Start () {positionRect = new Rect (50, 50, 200, 150);}// Update is called once per framevoid Update () {}void OnGUI(){positionRect =  GUILayout.Window(0, positionRect, WinFunc1, "Title");}void WinFunc1(int id){GUILayout.Button("WinButton");GUILayout.Label ("WinLabel");GUI.DragWindow();}}








Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.