Skip to content

Instantly share code, notes, and snippets.

@gegagome
Last active April 29, 2018 04:15
Show Gist options
  • Save gegagome/4ca67be14ecfe9c54fcfca3a1def62fc to your computer and use it in GitHub Desktop.
Save gegagome/4ca67be14ecfe9c54fcfca3a1def62fc to your computer and use it in GitHub Desktop.
How to handle multiple animations/animators
using System.Collections.Generic;
using UnityEngine;
public class SpritesController : MonoBehaviour {
public SpriteRenderer[] _sprites;
List<Animator> _anims = new List<Animator>();
AnimatorStateInfo _stateInfo;
int _animAudioTrack1HashActive;
int _animAudioTrack1HashUnactive;
int _animAudioTrack2HashActive;
int _animAudioTrack2HashUnactive;
int _animAudioTrack3HashActive;
int _animAudioTrack3HashUnactive;
void Awake ()
{
for (int i = 0; i < _sprites.Length; i++)
{
Animator anim;
if (_sprites[i].gameObject.GetComponent<Animator>())
{
anim = _sprites[i].gameObject.GetComponent<Animator>();
_anims.Add(anim);
}
}
_animAudioTrack1HashActive = Animator.StringToHash("Base Layer.c_AudioTrack1_active");
_animAudioTrack1HashUnactive = Animator.StringToHash("Base Layer.c_AudioTrack1_unactive");
_animAudioTrack2HashActive = Animator.StringToHash("Base Layer.c_AudioTrack2_active");
_animAudioTrack2HashUnactive = Animator.StringToHash("Base Layer.c_AudioTrack2_unactive");
_animAudioTrack3HashActive = Animator.StringToHash("Base Layer.c_AudioTrack3_active");
_animAudioTrack3HashUnactive = Animator.StringToHash("Base Layer.c_AudioTrack3_unactive");
}
void Update ()
{
if (Input.GetKeyUp(KeyCode.A))
{
_stateInfo = _anims[0].GetCurrentAnimatorStateInfo(0);
if (_stateInfo.fullPathHash == _animAudioTrack1HashActive)
{
_anims[0].SetTrigger("isUnactive");
}
if (_stateInfo.fullPathHash == _animAudioTrack1HashUnacAudioTrack2)
{
_anims[0].SetTrigger("isActive");
}
}
if (Input.GetKeyUp(KeyCode.S))
{
_stateInfo = _anims[1].GetCurrentAnimatorStateInfo(0);
if (_stateInfo.fullPathHash == _animAudioTrack2HashActive)
{
_anims[1].SetTrigger("isUnactive");
}
if (_stateInfo.fullPathHash == _animAudioTrack2HashUnactive)
{
_anims[1].SetTrigger("isActive");
}
}
if (Input.GetKeyUp(KeyCode.D))
{
_stateInfo = _anims[2].GetCurrentAnimatorStateInfo(0);
if (_stateInfo.fullPathHash == _animAudioTrack2HashActive)
{
_anims[2].SetTrigger("isUnactive");
}
if (_stateInfo.fullPathHash == _animAudioTrack2HashUnactive)
{
_anims[2].SetTrigger("isActive");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment