Skip to content

Instantly share code, notes, and snippets.

@DarrenTsung
Created August 30, 2016 17:24
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save DarrenTsung/9b12027afff93e61911e64ea3977a9b1 to your computer and use it in GitHub Desktop.
Save DarrenTsung/9b12027afff93e61911e64ea3977a9b1 to your computer and use it in GitHub Desktop.
Encapsulated class I use instead of StateMachineBehaviour
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() {}
}
}
@nicmar
Copy link

nicmar commented Apr 15, 2018

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! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment