UIStateItem implements different UI item states and provides API to switch between them.
UIStateItem contains three built-in states: active, inactive and disabled, that ideally fits most of UI elements use cases.
Besides of that you can add any additional states that you need.
Usually at initialization it's required to set UIStateItem to default state.
You can set default state to one of your state. Use "Flush" button if you don't need a default state.
States UI game objects:
UIStateItem is automatically uses nested gameobject for each state with exact name.
Alternatively you can switch to "Manual" mode. It will allow you to link each state game object manually to any game object from hierarchy.
On state change UIStateItem uses GameObject.SetActive to enable new state UI game object and to disable other states.
You can easily test state change directly in editor as well as in player mode.
UIStateItem implements callback event OnStateChange that called at any state change
Check the next additional components to add extra features to UIStateItem * UIStateItemMirror * UIStateItemAnimation
Built-in state "inactive"
Built-in state "active"
Built-in state "disabled"
String array of states names
Default tree states are: - inactive - active - disabled
Debug.Log("UIStateItem states count = " + GetComponent<UIStateItem>().States.Length);
GameObject array of States UI game objects
You can assign game objects in run time. Just make sure that "UI game objects" mode is set to "manual" in editor. See UIStateItem editor manual for details.
Note: You can't change a StatesUi array size. The StatesUi array size is always equals to States array size and controlling automatically.
Current state name
Default state name
Previous state name before last state change
Link to UIStateGroup object this item belongs to
If group defined then UIStateGroup.ItemStateChanged (itemName) function will be called for each state change.
public void SetDefaultStateTo( |
| ) |
Set default state to new state by name
GetComponent<UIStateItem>().SetDefaultStateTo(UIStateItem.STATE_ACTIVE);
GetComponent<UIStateItem>().SetStateDefault();
state string | state name from States array |
Flush default state
public string GetStateName( |
| ) |
public int GetStateId( |
| ) |
public int AddState ( |
| ) |
Add new state with given name
state string | new state name |
new state id from States array or -1 in case of duplicate state name
public void RemoveState ( |
| ) |
Remove state by given name
Note: Built-in states could not be removed!
state string | state name to remove |
public void SetState( |
| ) |
Change CurrentState to given state
For force = true | all state change actions will be processed even if CurrentState is already equals to given state. |
For force = false | all actions will be skipped if CurrentState is already equals to given state. |
state string | state name from States array |
force bool | bool, should state change be forced? |
public void SetState( |
| ) |
Change CurrentState to given state if it's not in given state yet
This function will skip all actions if CurrentState is already equals to given state.
See SetState (state, force) for details.
state string | state name from States array |
public void ForceSetState( |
| ) |
Force change CurrentState to given state
This function will set CurrentState to given state even if CurrentState is already equals to given state.
See SetState (state, force) for details.
state string | state name from States array |
public void SetStateDefault( |
| ) |
Shortcut function to set CurrentState to DefaultState
Use force parameter to control state change behavior. See SetState (state, force) for details.
public void SetStateActive( |
| ) |
Shortcut function to set CurrentState to built-in state UIStateItem.STATE_ACTIVE
Use force parameter to control state change behavior. See SetState (state, force) for details.
force bool | bool, should state change be forced? |
public void SetStateInactive( |
| ) |
Shortcut function to set CurrentState to built-in state UIStateItem.STATE_INACTIVE
Use force parameter to control state change behavior. See SetState (state, force) for details.
force bool | bool, should state change be forced? |
public void SetStateDisabled( |
| ) |
Shortcut function to set CurrentState to built-in state STATE_DISABLED
Use force parameter to control state change behavior. See SetState (state, force) for details.
force bool | bool, should state change be forced? |
This UnityEvent is called on each state change
You can subscribe any of your functions to this event.
GetComponent<UIStateItem>().OnStateChange.AddListener(StateChanged);
...
void StateChanged() {
Debug.Log("State has been changed!");
}