/// <summary> /// changes the current state /// </summary> public R ChangeState <R>() where R : SKMecanimState <T> { // avoid changing to the same state var newType = typeof(R); if (_currentState.GetType() == newType) { return(_currentState as R); } #if UNITY_EDITOR // do a sanity check while in the editor to ensure we have the given state in our state list if (!_states.ContainsKey(newType)) { var error = GetType() + ": state " + newType + " does not exist. Did you forget to add it by calling AddState?"; Debug.LogError(error); throw new Exception(error); } #endif // end the previous state _currentState.End(); // swap states and call begin _currentState = _states[newType]; _currentState.Begin(); // fire the changed event if we have a listener if (onStateChanged != null) { onStateChanged(); } return(_currentState as R); }
/// <summary> /// creates a Mecanim state machine and sets it's current state /// </summary> public SKMecanimStateMachine(Animator animator, T context, SKMecanimState <T> initialState) { this.animator = animator; _context = context; // setup our initial state AddState(initialState); _currentState = initialState; _currentState.Begin(); }