Created
August 30, 2016 17:24
-
-
Save DarrenTsung/9b12027afff93e61911e64ea3977a9b1 to your computer and use it in GitHub Desktop.
Encapsulated class I use instead of StateMachineBehaviour
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using UnityEngine; | |
namespace DT { | |
public class LogicalStateMachineBehaviour : StateMachineBehaviour { | |
// PRAGMA MARK - StateMachineBehaviour Lifecycle | |
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { | |
this.Animator = animator; | |
this.OnStateEntered(); | |
this._active = true; | |
} | |
public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { | |
this.Animator = animator; | |
this._active = false; | |
this.OnStateExited(); | |
} | |
public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { | |
this.OnStateUpdated(); | |
} | |
// PRAGMA MARK - Internal | |
private bool _active = false; | |
protected Animator Animator { get; private set; } | |
void OnDisable() { | |
if (this._active) { | |
this.OnStateExited(); | |
} | |
} | |
protected virtual void OnStateEntered() {} | |
protected virtual void OnStateExited() {} | |
protected virtual void OnStateUpdated() {} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey again, great tutorial on FSM with the animator, but I'm trying to get my head around how the LogicalStateMachineBehaviour works. When I changed all behaviours to use LogicalStateMachineBehaviour instead of StateMachineBehaviour, it works, but I can't figure out how to use this.
For example, I wanted to add some debug info here, to display a Debug.Log every time i enter or exit a state. Currently I have it in each states behaviour, which is pretty hard to update. Also I tried to figure out how you did the DoAfterDelay, and tried putting it here, but I didn't figure it out.
If you could just explain what this code is supposed to do, then it would be great! :)