/// <summary> /// Gets and sets a state to Off and invokes the OnStateDeactivated event. Setting a /// state off changes the state value to 0. /// </summary> /// <param name="stateName">The name of the state to set to off</param> /// <returns>The state that was set to off</returns> public InteractionState SetStateOff(string stateName) { InteractionState state = GetState(stateName); if (state != null) { // Only update the state value and invoke events if InteractiveElement is Active if (state.Value != 0 && InteractiveElement.Active) { state.Value = 0; state.Active = false; // If the only state in active states is going to be removed, then activate the default state if (activeStates.Count == 1 && activeStates.First() == state) { SetStateOn(defaultStateName); } // We need to save the last state active state so we can add transitions OnStateDeactivated.Invoke(state, activeStates.Last()); activeStates.Remove(state); } } else { Debug.LogError($"The {stateName} state is not being tracked, add this state using AddState(state) to set it"); } return(state); }
// Add listeners to the OnStateActivated and OnStateDeactivated events private void AddStateEventListeners() { // Add listeners to invoke a state event. OnStateActivated.AddListener((state) => { // If the event configuration for a state is of type StateEvents, this means that the state // does not have associated dynamic event data. Therefore, the state event can be invoked when // the state value changes without passing in event data. if (state.EventConfiguration is StateEvents) { EventReceiverManager.InvokeStateEvent(state.Name); } }); OnStateDeactivated.AddListener((previousState, currentState) => { if (previousState.EventConfiguration is StateEvents) { EventReceiverManager.InvokeStateEvent(previousState.Name); } }); }