Foreword
First of all, we can divide the GUI system in the editor into four categories:
1. UnityEngine.GUI
2. UnityEngine.GUILayout
3. UnityEditor.EditorGUI
4. UnityEditor.EditorGUILayout
GUI system
This is the most widely used GUI system. It belongs to the UnityEngine namespace. All controls drawn with it do not have an automatic layout effect. You need to manually specify the drawing position and size of each control. The adaptability is weak, but the development is free. Degree is higher.
Note: The GUI system can be used after publishing or in the editor.
Examples of GUI system controls use
private void OnGUI()
{ GUI.Button(new Rect(0, 0, 100, 30), "Button"); GUI.Label(new Rect(100, 0, 100, 30), " Label"); GUI.PasswordField(new Rect(0, 30, 100, 30), "Password",'$'); GUI.Toggle(new Rect(100, 30, 100, 30), true, "Toggle" ); } GUILayout system GUI system with automatic layout, belonging to the UnityEngine namespace, all controls drawn with it have automatic layout effects, which are highly adaptable, but have low development freedom.
Note: The GUILayout system can be used after publishing or in the editor.
GUILayout system control usage example
Horizontal layout
private void OnGUI()
{ GUILayout.BeginHorizontal("Box"); //Start a horizontal layout GUILayout.Button("Button"); GUILayout.Label("Label"); GUILayout.PasswordField ("Password",'$'); GUILayout.Toggle(true, ""); GUILayout.EndHorizontal(); //End a horizontal layout
GUILayout.BeginHorizontal("Box"); //Start a horizontal layout
GUILayout.Button("Button");
GUILayout.Label("Label");
GUILayout.PasswordField("Password",'$');
GUILayout.Toggle( true, "");
GUILayout.FlexibleSpace(); //Create an adaptive blank area, which is to fill this part of the space in this layout
GUILayout.EndHorizontal(); //End a horizontal layout
GUILayout.BeginHorizontal("Box"); //Start a horizontal layout
GUILayout.FlexibleSpace(); //Create an adaptive blank area, which is to fill this part of the space in this layout
GUILayout.Button("Button ");
GUILayout.Label("Label");
GUILayout.PasswordField("Password",'$');
GUILayout.Toggle(true, "");
GUILayout.EndHorizontal(); //End a horizontal layout
}
Vertical layout
private void OnGUI()
{ GUILayout.BeginVertical("Box"); //Start a vertical layout GUILayout.Button("Button"); GUILayout.Label("Label"); GUILayout.PasswordField("Password", ' $'); GUILayout.Toggle(true, ""); GUILayout.EndVertical(); //End a vertical layout }
private void OnGUI()
{ GUILayout.BeginVertical("Box"); //Start a vertical layout GUILayout.FlexibleSpace(); //Create an adaptive blank area, which is to fill this part of the space in this layout GUILayout.Button("Button"); GUILayout.Label("Label"); GUILayout.PasswordField("Password",'$'); GUILayout.Toggle(true, ""); GUILayout.EndVertical(); //End A vertical layout }
Scroll view
private Vector2 _scroll;
private void OnGUI()
{ _scroll = GUILayout.BeginScrollView(_scroll);//Start a scroll view GUILayout.BeginVertical("Box"); //Start a vertical layout for (int i = 0; i <20; i++) { GUILayout.Button("Button" + (i + 1)); } GUILayout.EndVertical(); //End a vertical layout GUILayout.EndScrollView(); //End a scroll view }
The EditorGUI system is
suitable for the editor’s GUI system. It belongs to the UnityEditor namespace. All controls drawn with it do not have layout effects. You need to manually specify the drawing position and size of each control. The difference from the GUI system is that it has some editors. Special control, and it can only run in the editor, the adaptability is weak, but the development freedom is higher.
Note: The EditorGUI system cannot be used after publishing, it can only be used in the editor.
###EditorGUI system control usage example
private void OnGUI()
{
EditorGUI.ColorField(new Rect(0, 0, 100, 30), Color.red);
EditorGUI.DoubleField(new Rect(0, 30, 100, 30), 10);
EditorGUI.ProgressBar(new Rect(0, 60, 100, 30), 0.5f, "ProgressBar");
}
1
2
3
4
5
6
EditorGUILayout system has
an EditorGUI system with automatic layout. It belongs to the UnityEditor namespace. All controls drawn with it have automatic layout effects. The difference from the GUILayout system is that it has some editor-specific controls, and it can only run in the editor , The adaptability is strong, but the development freedom is low.
Note: The EditorGUILayout system cannot be used after publishing, it can only be used in the editor.
###EditorGUILayout system control usage example
private void OnGUI()
{ EditorGUILayout.BeginHorizontal("Box"); //Start a horizontal layout EditorGUILayout.LabelField("LabelField"); EditorGUILayout.PasswordField("PasswordField"); EditorGUILayout.RectField(Rect.zero); EditorGUILayout. EndHorizontal(); //End a horizontal layout } The above four GUI systems can be mixed. The controls in the EditorGUI and EditorGUILayout systems are mostly with Field. It can be seen that the intention is mainly to display the value of the field. The GUI system does not have a layout. Therefore, it is less used in editor development, unless sometimes it is necessary to create a high degree of freedom interface. The GUILayout system development editor tool is the most used.