123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Playables;
- using Object = UnityEngine.Object;
- namespace Animancer
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [AddComponentMenu(Strings.MenuPrefix + "Named Animancer Component")]
- [HelpURL(Strings.DocsURLs.APIDocumentation + "/" + nameof(NamedAnimancerComponent))]
- public class NamedAnimancerComponent : AnimancerComponent
- {
-
- #region Fields and Properties
-
- [SerializeField, Tooltip("If true, the 'Default Animation' will be automatically played by " + nameof(OnEnable))]
- private bool _PlayAutomatically = true;
-
-
-
-
- public ref bool PlayAutomatically => ref _PlayAutomatically;
-
- [SerializeField, Tooltip("Animations in this array will be automatically registered by " + nameof(Awake) +
- " as states that can be retrieved using their name")]
- private AnimationClip[] _Animations;
-
-
-
-
-
- public AnimationClip[] Animations
- {
- get => _Animations;
- set
- {
- _Animations = value;
- States.CreateIfNew(value);
- }
- }
-
-
-
-
-
- public AnimationClip DefaultAnimation
- {
- get => _Animations.IsNullOrEmpty() ? null : _Animations[0];
- set
- {
- if (_Animations.IsNullOrEmpty())
- _Animations = new AnimationClip[] { value };
- else
- _Animations[0] = value;
- }
- }
-
- #endregion
-
- #region Methods
-
- #if UNITY_EDITOR
-
-
-
-
-
- protected virtual void OnValidate()
- {
- if (_Animations == null)
- return;
- for (int i = 0; i < _Animations.Length; i++)
- {
- var clip = _Animations[i];
- if (clip == null)
- continue;
- try
- {
- Validate.AssertNotLegacy(clip);
- continue;
- }
- catch (Exception exception)
- {
- Debug.LogException(exception, clip);
- }
- Array.Copy(_Animations, i + 1, _Animations, i, _Animations.Length - (i + 1));
- Array.Resize(ref _Animations, _Animations.Length - 1);
- i--;
- }
- }
- #endif
-
-
- protected virtual void Awake()
- {
- States.CreateIfNew(_Animations);
- }
-
-
-
-
-
-
- protected override void OnEnable()
- {
- base.OnEnable();
- if (_PlayAutomatically && !_Animations.IsNullOrEmpty())
- {
- var clip = _Animations[0];
- if (clip != null)
- Play(clip);
- }
- }
-
-
-
-
-
-
- public override object GetKey(AnimationClip clip) => clip.name;
-
-
- public override void GatherAnimationClips(ICollection<AnimationClip> clips)
- {
- base.GatherAnimationClips(clips);
- clips.Gather(_Animations);
- }
-
- #endregion
-
- }
- }
|