I have added to a Camera a Animation component and changed the Animations size to 2. Added two clips.
Then in the script in the top:
Animation _animation;
List<string> animations = new List<string>();
In Start
private void Start()
{
_animation = GetComponent<Animation>();
foreach (AnimationState state in _animation)
{
animations.Add(state.name);
}
int n = _animation.GetClipCount();
}
But for some reason animations is empty count 0. But the variable n value is 2.
What i want to get all the Animations in the array.
I’m using unity 5.5.1f1 personal
The Animation component is a legacy component - you should only use it if you’re really sure you know what you’re doing, or are maintaining code that was written before its replacement existed.
Its replacement is Animator , and this information is available in that class via .runtimeAnimationController.animationClips
That said, I think the code you’re using should work as written - but if it’s a Unity bug, I doubt it’ll get fixed at this point.
1 Like
Thanks found another way to do what i need without the Animation component at all. Sorry for the mess.
N0NameDev:
HOW???
Start a new thread with your own situation and your own thread. It’s unlikely that this person’s exact workaround will be exactly like what you need for your situation, and more unlikely that they’ll come back and answer you 5 years after their post.
You’ve gotta tell us what the solution was when you get the solution please.
StarManta:
The Animation component is a legacy component - you should only use it if you’re really sure you know what you’re doing, or are maintaining code that was written before its replacement existed.
Its replacement is Animator , and this information is available in that class via .runtimeAnimationController.animationClips
That said, I think the code you’re using should work as written - but if it’s a Unity bug, I doubt it’ll get fixed at this point.
I can confirm it works with Unity 2021.2.8f1
I can’t find how to make the List or an Array visible in the inspector. and this is one of those things about Unity that is beyond infuriating…
But here’s how to get that array of names:
using UnityEditor;
using UnityEngine;
public class AnimControlLetter : MonoBehaviour {
[SerializeField] private Animation _anim;
[SerializeField] private AnimationClip[] _clips;
[SerializeField] private int _clipCount;
[SerializeField] private int clipToRun;
public string[] _clipNames;
void Start ( ) {
if ( _anim == null ) _anim = GetComponent<Animation>( );
_clipCount = _anim.GetClipCount( );
_clips = new AnimationClip[_clipCount];
_clips = AnimationUtility.GetAnimationClips( this.gameObject );
_clipNames = new string[_clipCount];
for ( int i = 0; i < _clips.Length; i++ ) {
_clipNames[i] = _clips[i].name;
}
}
}
3 Likes
here is a little script the will auto fetch all the clip names and add a button that will play the animation in play mode :
using System.Linq;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class AnimPlayer : MonoBehaviour
{
public Animator animator;
string currentState = "none";
[HideInInspector] public string[] clips;
private void OnValidate()
{
if( animator != null )
{
clips = animator
.runtimeAnimatorController
.animationClips
.Select( x => x.name )
.ToArray();
}
}
private void Awake()
{
currentState = animator.GetCurrentAnimatorClipInfo( 0 )[ 0 ].clip.name;
}
public void Play( string newState )
{
if( ! Application.isPlaying ) return;
if( currentState == newState ) return;
animator.Play( newState );
currentState = newState;
}
#if UNITY_EDITOR
[CustomEditor(typeof(AnimPlayer))]
class Inspector : Editor
{
int nextClip = 0;
int[] intvalues;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
GUILayout.Space( 7 );
var script = ( AnimPlayer ) target;
intvalues = new int[ script.clips.Length ];
for( var i = 0; i < intvalues.Length; ++ i ) intvalues[ i ] = i ;
using ( new GUILayout.HorizontalScope() )
{
nextClip = EditorGUILayout.IntPopup( nextClip, script.clips, intvalues );
if( GUILayout.Button("Play") )
{
script.Play( script.clips[ nextClip ] );
}
}
}
}
#endif
}
1 Like
@Vladerx , you should note that this thread is a bit on the old side. And with that, it is also referencing the older legacy ‘Animation’ class not the contemporary ‘Animator’ class.
Yes i am aware , and here is one for the Animator class ( also works in edit mode ) , this one is a standalone animation clip player without the need to create animation controller asset , works with 2d sprites and 3d skinned mesh renderers
using System.Collections;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;
using UnityEngine.SceneManagement;
[ExecuteAlways] public class PlayClip : MonoBehaviour
{
// -------------------------------------------------------
public void Pause() { this.enabled = false; }
public void Play() { this.enabled = true; }
public void LastFrame()
{
playable.GetAnimationClip().SampleAnimation(gameObject, duration);
graph.Stop();
}
public event System.Action<PlayClip> OnClipLoop;
public AnimationClip GetClip()
{
return m_Clip;
}
public void SetSpeed(float speed)
{
m_Speed = speed;
Validate();
// if( playable.IsValid() ) playable.SetSpeed( m_Speed );
}
public void SetClip( AnimationClip clip )
{
m_Clip = clip;
Validate();
}
public void SetClip( AnimationClip clip , float speed )
{
m_Speed = speed;
m_Clip = clip;
Validate();
// if( playable.IsValid() ) playable.SetSpeed( m_Speed );
}
public void SetClip( AnimationClip clip , System.Action callback )
{
SetClip( clip , m_Speed , callback );
}
public void SetClip( AnimationClip clip, float speed , System.Action callback )
{
m_LastClip = clip;
m_LastClipCapture = true;
m_LastClipCallback = callback;
SetClip( clip , speed );
}
public void SetClip( AnimationClip clip , AnimationClip next_clip )
{
SetClip( clip, m_Speed, next_clip, m_Speed );
}
public void SetClip( AnimationClip clip , float speed , AnimationClip next_clip , float next_speed )
{
SetClip( clip, speed, () => SetClip( next_clip, next_speed ) );
}
// -------------------------------------------------------
public bool isPlaying { private set; get; } = false;
public bool isPaused { private set; get; } = false;
public float duration { private set; get; } = 0f;
public int loopCount { private set; get; } = 0;
public float clipProgress { private set; get; } = 0; // 0 to 1
// -------------------------------------------------------
[SerializeField] AnimationClip m_Clip;
[SerializeField]
[Range(0f,3.5f)] float m_Speed = 1f;
[Space(10)]
public UnityEngine.Events.UnityEvent<PlayClip> EventClipLoop;
// -------------------------------------------------------
[System.NonSerialized] AnimationClip prev;
[System.NonSerialized] Animator animator;
[System.NonSerialized] PlayableGraph graph;
[System.NonSerialized] AnimationClipPlayable playable;
// -------------------------------------------------------
// bool hasPlayable => ! playable.Equals( default( AnimationClipPlayable ) );
// -------------------------------------------------------
AnimationClip m_LastClip;
System.Action m_LastClipCallback;
bool m_LastClipCapture = false;
void LastClipCapture()
{
if ( ! m_LastClipCapture ) return;
m_LastClipCapture = false;
if ( m_LastClip != m_Clip ) return;
m_LastClipCallback.Invoke();
}
// -------------------------------------------------------
private void Update()
{
if( ! isPlaying || isPaused ) return;
var anim_time = (float) ( playable.GetTime() / duration );
clipProgress = anim_time % 1;
if ( graph.IsPlaying() && anim_time >= ( 1 + loopCount ) )
{
loopCount ++ ;
if( Application.isPlaying )
{
OnClipLoop?.Invoke( this );
LastClipCapture();
}
}
}
void OnEnable()
{
#if UNITY_EDITOR
if( ! Application.isPlaying )
UnityEditor.SceneManagement.EditorSceneManager.sceneSaved += OnSaved;
#endif
AttachAnimator();
Validate();
if( m_Clip != null )
{
if( isPaused && isPlaying )
{
isPaused = false;
playable.Play();
return;
}
ForcePlay();
}
}
void OnSaved(Scene scene)
{
if ( enabled ) ForcePlay();
}
public void ForcePlay()
{
if( m_Clip == null || animator == null ) return;
Clean();
playable = AnimationPlayableUtilities.PlayClip(animator, m_Clip, out graph);
playable.SetSpeed( m_Speed );
playable.Play();
loopCount = 0;
duration = m_Clip.averageDuration / m_Speed;
isPlaying = true;
prev = m_Clip;
}
void AttachAnimator()
{
if ( animator == null && ! gameObject.TryGetComponent( out animator ) )
animator = gameObject.AddComponent<Animator>();
animator.runtimeAnimatorController = null;
animator.hideFlags = HideFlags.HideInInspector;
}
// public bool debug = false;
void OnValidate()
{
if( isActiveAndEnabled )
{
AttachAnimator();
Validate();
}
// animator.hideFlags = debug ? HideFlags.None : HideFlags.HideInInspector;
}
void Validate()
{
if ( m_Clip == null ) prev = null;
if (m_Clip != null && m_Clip != prev )
{
ForcePlay();
return;
}
if ( isPlaying && playable.IsValid() )
playable.SetSpeed( m_Speed );
else ForcePlay();
}
private void OnDisable()
{
#if UNITY_EDITOR
if( ! Application.isPlaying )
UnityEditor.SceneManagement.EditorSceneManager.sceneSaved -= OnSaved;
#endif
if( isPlaying && playable.IsValid() )
{
isPaused = true;
playable.Pause();
}
}
void OnDestroy()
{
Clean();
if ( animator )
{
if ( Application.isPlaying ) Destroy( animator );
else animator.hideFlags = HideFlags.None;
}
}
void Clean()
{
if ( playable.IsValid() ) playable.Destroy();
if ( graph.IsValid() ) graph.Destroy();
isPlaying = false;
}
}
1 Like