Unity3d Basics-Common GUI controls

Source: Internet
Author: User

The GUI portion of the Unity3d is redrawn per frame, and only the GUI should be drawn in the Ongui, and the value of the variable declared in the Ongui method will not be saved every time, for example, To draw a text edit box in unity, you can call the following code: Guilayout.textfield ("text"), but when you run it, you will see that the text string is displayed regardless of what we enter. This is because the string we entered in the previous frame is not saved, and the redraw is all erased in the next frame, which is to redraw a "text" string. Workaround: We can save input values in the member variables of the class, Initialize text = Guilayout.textfield (text) in the Start method, and then introduce several more commonly used controls.

1. Button: Guilayout.button ("Hello"); The method returns a Boolean value that returns True when the button is pressed and returns false in the process of pressing.

2. Repeat button: Guilayout.repeatbutton ("Hello"), unlike the button, this method will always return true when we press the button, which is suitable for the bullet reflection button.

3. Password box: pwd = Guilayout.passwordfield (pwd, ' * '), and the second parameter is a masked character.

4.Tab page: Selectedtoolbarid = Guilayout.toolbar (selectedtoolbarid,new string[]{"Tab1", "Tab2", "TAB3"}); The return value is the ordinal of the activated button. We can draw the corresponding tab page according to the tag number.

Using unityengine;using System.collections;public class Guitest:monobehaviour {private int selectedtoolbarid;//use thi S for Initializationvoid Start () {selectedtoolbarid = 0;} Update is called once per framevoid Update () {}void Ongui () {Selectedtoolbarid = Guilayout.toolbar (selectedtoolbarid,ne W string[]{"Tab1", "Tab2", "TAB3"}), switch (selectedtoolbarid) {case 0:guilayout.label ("Selected Tab1");// Here to draw the contents of the Tab1 break;case 1:guilayout.label ("Selected TaB2");//The contents of TAB2 are plotted here Break;case 2:guilayout.label ("Selected Tab3 ");//The contents of the Tab3 are plotted here;}}}
5. Radio box Toggle, returns a Boolean value indicating the currently selected condition.
if (ismuted) {ismuted = Guilayout.toggle (ismuted, "Yes"),} else {ismuted = Guilayout.toggle (ismuted, "No");}
6. Slider: Portrait, Slidervalue = Guilayout.verticalslider (slidervalue,0,100); The return value is the current value, the second parameter is the minimum value, and the third is the maximum value. Horizontalslider () landscape.

7. Region area, the equivalent of a control box, the area of the control to follow area move, Beginarea () Start a region, the parameters specify the size and coordinates of the area, Endarea () end region;

Guilayout.beginarea (New Rect (50, 50, 100, 100)); Guilayout.label ("area"); Guilayout.endarea ();
8. The window, the area is no border and title, nor can drag and drop.  Guilayout.window (0, New Rect (50,50,200,200), AddWindow1, "my Window"); The first parameter is the number of the window, the second is the window size, and the third is a void windowfunction (int windowid) delegate, which is used to draw the window contents.

Window dragging, the last call to Gui.dragwindow () in Windowfunction can enable full-screen drag-and-drop (to the Dragwindow rect parameter can be set to drag and drop the area). Consider the problem of frame refresh, in order to save the return rect of window () Ongui can be dragged, start () set the initial position.

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, Winfu NC1, "Title");} void WinFunc1 (int id) {Guilayout.button ("Winbutton"); Guilayout.label ("Winlabel"); Gui. Dragwindow ();}}








Unity3d Basics-Common GUI controls

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.