GuiActionsList Class |
Namespace: KSPDev.GUIUtils
The GuiActionsList type exposes the following members.
Name | Description | |
---|---|---|
GuiActionsList | Initializes a new instance of the GuiActionsList class |
Name | Description | |
---|---|---|
Add | Adds an action to the pending list. | |
ExecutePendingGuiActions | Executes actions when it's safe to do the changes. |
public class MyUI : MonoBehaviour { private readonly GuiActionsList guiActions = new GuiActionsList(); private bool showLabel = false; void OnGUI() { if (guiActions.ExecutePendingGuiActions()) { // ...do other stuff that affects UI... } if (GUILayout.Button(new GUIContent("Test Button"))) { // If "showLabel" is changed right here then Unity GUI will complain saying the number // of UI controls has changed. So, postpone the change until current frame is ended. guiActions.Add(() => { showLabel = !showLabel; // This will be done at the beginning of the next frame. }); } if (showLabel) { GUILayout.Label("Test label"); } } }
If you were using simple approach and updated showLabel right away Unity would likely thrown an error like this:
[EXCEPTION] ArgumentException: Getting control 1's position in a group with only 1 controls when doing Repaint