Last active
July 14, 2020 01:13
-
-
Save jmschrack/812a1daa679d59c244c38abdaf414c58 to your computer and use it in GitHub Desktop.
A thin helper wrapper for manually calling animations on an Animator.
This file contains hidden or 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.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| /// <summary> | |
| /// A thin wrapper for easily Playing specific animations. And checking if the animation is currently playing | |
| /// </summary> | |
| public class AnimatorController : MonoBehaviour | |
| { | |
| Animator _animator; | |
| public Animator animator=>_animator; | |
| [SerializeField] | |
| string _animLayer="AnimLayer"; | |
| /// <summary> | |
| /// The Animator Layer containing all your animations. | |
| /// CASE SENSITIVE | |
| /// </summary> | |
| /// <value></value> | |
| public string animLayer{ | |
| get{ | |
| return _animLayer; | |
| } | |
| set{ | |
| _animLayer=value; | |
| layerIndex=animator.GetLayerIndex(_animLayer); | |
| } | |
| } | |
| int layerIndex; | |
| //For example | |
| /// <summary> | |
| /// For a given Animator State name, returns the associated int value. | |
| /// YOU MUST SET animLayer BEFORE HAND! | |
| /// CASE SENSITIVE | |
| /// </summary> | |
| /// <param name="animName"></param> | |
| /// <returns></returns> | |
| public int GetID(string animName){ | |
| return Animator.StringToHash(animLayer+"."+animName); | |
| } | |
| int _currentAnim; | |
| // Start is called before the first frame update | |
| void Start() | |
| { | |
| _animator=GetComponent<Animator>(); | |
| layerIndex=animator.GetLayerIndex(animLayer); | |
| Init(); | |
| } | |
| /// <summary> | |
| /// Called in Start() | |
| /// Override this method with initializers for your animations. | |
| /// Example: | |
| /// <code>IdleAnim=GetID("Idle");</code> | |
| /// </summary> | |
| public virtual void Init(){ | |
| } | |
| public int CurrentAnim{ | |
| get{ | |
| return _currentAnim; | |
| } | |
| set{ | |
| _currentAnim=value; | |
| } | |
| } | |
| public void Play(int anim){ | |
| CurrentAnim=anim; | |
| animator.Play(anim); | |
| } | |
| /// <summary> | |
| /// Checks if the CurrentAnim==anim and if this | |
| /// </summary> | |
| /// <param name="anim"></param> | |
| /// <returns></returns> | |
| public bool IsPlaying(int anim){ | |
| return (CurrentAnim==anim)&&(animator.GetCurrentAnimatorStateInfo(layerIndex).normalizedTime<1||animator.GetNextAnimatorStateInfo(layerIndex).normalizedTime<1); | |
| } | |
| public void CrossFade(int anim,float normalizedTime){ | |
| CurrentAnim=anim; | |
| animator.CrossFade(anim,normalizedTime); | |
| } | |
| public void CrossFadeInFixedTime(int anim,float duration){ | |
| CurrentAnim=anim; | |
| animator.CrossFadeInFixedTime(anim,duration); | |
| } | |
| /// <summary> | |
| /// Returns true if it was not playing and had to Crossfade. | |
| /// </summary> | |
| /// <param name="anim"></param> | |
| /// <param name="normalizedTime"></param> | |
| /// <returns></returns> | |
| public bool CrossFadeIfNotPlaying(int anim,float normalizedTime){ | |
| if(!IsPlaying(anim)){ | |
| CrossFade(anim,normalizedTime); | |
| return true; | |
| } | |
| return false; | |
| } | |
| } |
This file contains hidden or 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 UnityEngine; | |
| /// <summary> | |
| /// On State Enter, calls out to the AnimatorController and sets the CurrentAnim | |
| /// </summary> | |
| public class UpdateControllerState : StateMachineBehaviour{ | |
| AnimatorController ac; | |
| override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex){ | |
| if(ac==null) | |
| ac=animator.gameObject.GetComponent<AnimatorController>(); | |
| ac.CurrentAnim=stateInfo.fullPathHash; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment