Answer the question
In order to leave comments, you need to log in
State machine implementation?
I am making a state machine for UI.
Here is the code:
namespace UI
{
public class StateMachine
{
public enum States
{
Menu,
Pause,
Settings,
GraphicsSettings,
MusicSettings,
GameWindow,
NewGame,
}
public delegate void StateHandler(States state);
static public event StateHandler ChangeState;
static public States currentState
{
get
{
return currentState;
}
set
{
currentState = value; // Здесь Unity зависает.
ChangeState?.Invoke(value);
}
}
}
[System.Serializable]
public class StateElement
{
public StateMachine.States state;
public bool isEnabled;
}
}
namespace UI
{
public class UIState : MonoBehaviour
{
public StateElement[] states = new StateElement[System.Enum.GetValues(typeof(StateMachine.States)).Length];
private void Awake()
{
StateMachine.ChangeState += (state) => ChangeState(state);
}
private void ChangeState(StateMachine.States state)
{
gameObject.SetActive(states[(int)state].isEnabled);
}
}
}
Answer the question
In order to leave comments, you need to log in
It turns out that setting the value in set again calls set, and so recursively.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question