文章转载自IMGUI crash course
Unity's IMGUI system is quite simple but very powerful. The main parts are:
- The OnGUI callback. This callback is used to handle everything that has to do with GUI.
- The Event class which is tightly connected to OnGUI.
- The GUI controls defined in GUI, GUILayout and additional controls which are only available in the editor in EditorGUI and EditorGUILayout.
- The GUIStyle class which defines how a single control looks like. It's actually responsible for any GUI drawing.
- The GUISkin class which is basically just a collection of predefined default styles for all built in controls and has an array for unlimited custom styles at the end.
- The utility classes GUIUtility and in the editor the EditorGUIUtility. Not to forget the tiny but important GUILayoutUtility class when it comes to using GUILayout classes.
OnGUI
OnGUI is a callback that is called automatically by the engine for several purposes. It's usually called at least two times per frame but might be called several times a frame when other events happens.
OnGUI is, in my opinion, a bad name. Of course it's main purpose is to "do GUI stuff", but it's actually an event processor callback. I will explain that in the next section.
The Event class
The event class holds information about the event that's currently processed in OnGUI. It's only static member current. This holds an instance of the Event class and is only valid in OnGUI.
The type property holds the current EventType that is processed.
Here's a quick overview of the different events:
- Mouse events like MouseDown, MouseUp, MouseMove(editor only), MouseDrag, ScrollWheel. I should add that Unity will emulate those events on mobile touch devices. It calculates the mouseposition by taking the arithmetic mean of all touches.
- Keyboard events like KeyDown and KeyUp. Note: unlike Input.GetKeyDown / Up those events map the system's OS keyboard events. So when you hold down a key, the keydown event is repeated by the systems repeat rate. This is of course important for text editor GUIs.
- The Layout event. This is a special event that is always executed before all other eventa and is used to determine the size and position for automatic layouted controls. Note: This event can be disabled with useGUILayout but keep in mind that all GUILayout stuff won't work in this case.
- The Repaint event. This event will actually draw the GUI elements.
- The Used event type. Any control that processes an event and don't want others to get this event can use the Use function to "eat" the event. When Use is called the event type will be set to Used and every control should just ignore this event.
Beside the type of event, it holds additional information for the event.
- mousePosition holds the mouse position in GUI coordinates. GUI coordinates are different from screen coordinates. The GUI has it's origin at the top left corner, screen coordinates has it's origin at the bottom left corner. The GUIUtility class has functions to convert those coordinates.
- button. Only valid for mouse events and contains the mouse button index (0 - left button, 1 - right button, 2 - middle button)
- modifiers hold information about Shift, Alt, Ctrl keys
- keyCode. Only valid for keyboard events. Holds the KeyCode of the key that has been pressed / released.
- Some additional things like shortcuts for modifier keys.
GUIStyle
As already mentioned this class is responsible for rendering a control to the screen. A GUI control function will call one of the Draw functions when the repaint event is "detected". An GUIStyle consists of different GUIStyleStates where only one is "active" at a time. Which state is used is determined by what Draw function is called, which parameters are passed and if the control has currently the keyboard focus or not.
When a single GUIStyle is drawn it can produce 0 to 3 draw calls. That's why very complicated GUIs are considered bad for mobile development, however we used the GUI system a lot on Android and iOS and hadn't much problems on more or less modern devices.
The things a GUIStyle might draw are:
- The background image from the GUIStyleState. This is usually used to define how the button / textfield / ... generally looks like. If a background image is set it's always drawn first.
- An content image defined in the GUIContent class that is passed to Dram.
- The content's Text also defined in the GUIContent.
The ImagePosition in the GUIStyle defines where the image is displayed in relation to the text or to only draw the image and ignore the text or the other way round.
A GUIStyle has 8 GUIStyleStates where each state allows you to specify a background image as well as a text color. Note: A state is only used when it has a Background image. So unfortunately it's not possible to just setup text colors without an background image.
The different states are:
- normal
- hover
- active
- focused
- onNormal
- onHover
- onSctive
- onFocused
normal is the stylestate that is used in most cases. A label for example will only use this state.
hover is used by buttons, toggles and most other interactive elements when you hover over the element.
active is used for interactive elements while you click-and-hold on the element. It's the button down state.
focused is used by TextFields and TextAreas when the editor is active and you can edit the text.
All the onXXX states have the same function as the other 4 above, but are used for elements that have an "on" state like a Toggle. So if the toggle is unchecked and you click on it you will see the active state. When the toggle is already checked you will see the onActive state.
As mentioned above states without a background image are ignored. So if your button style has a "normal" and "active" state with a texture but no hover state, Unity will use the normal state when you hover over your button. If you want to just change the text color when you hover you have to use the same background image for "hover" as you used for "normal" otherwise the hover state will be ignored.
As last note: Unity has implemented an implicit casting operator (sounds complicated, but it isn't
标签:control,IMGUI,GUI,controls,class,will,Unity,快速,event From: https://www.cnblogs.com/dewxin/p/17853541.html