123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- using Animancer.Units;
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using Object = UnityEngine.Object;
- namespace Animancer
- {
-
-
- [CreateAssetMenu(menuName = Strings.MenuPrefix + "Mixer Transition/Manual", order = Strings.AssetMenuOrder + 2)]
- [HelpURL(Strings.DocsURLs.APIDocumentation + "/" + nameof(ManualMixerTransitionAsset))]
- public class ManualMixerTransitionAsset : AnimancerTransitionAsset<ManualMixerTransition>
- {
-
- [Serializable]
- public new class UnShared :
- UnShared<ManualMixerTransitionAsset, ManualMixerTransition, ManualMixerState>,
- ManualMixerState.ITransition
- { }
- }
-
-
- [Serializable]
- public abstract class ManualMixerTransition<TMixer> : AnimancerTransition<TMixer>,
- IMotion, IAnimationClipCollection, ICopyable<ManualMixerTransition<TMixer>>
- where TMixer : ManualMixerState
- {
-
- [SerializeField]
- [Tooltip(Strings.Tooltips.OptionalSpeed)]
- [AnimationSpeed]
- [DefaultValue(1f, -1f)]
- private float _Speed = 1;
-
-
-
- public override float Speed
- {
- get => _Speed;
- set => _Speed = value;
- }
-
- [SerializeField]
- [UnityEngine.Serialization.FormerlySerializedAs("_Clips")]
- [UnityEngine.Serialization.FormerlySerializedAs("_States")]
- private Object[] _Animations;
-
-
- public ref Object[] Animations => ref _Animations;
-
- public const string AnimationsField = nameof(_Animations);
-
- [SerializeField]
- [AnimationSpeed]
- [DefaultValue(1f, -1f)]
- private float[] _Speeds;
-
-
-
-
- public ref float[] Speeds => ref _Speeds;
-
- public const string SpeedsField = nameof(_Speeds);
-
- public bool HasSpeeds => _Speeds != null && _Speeds.Length >= _Animations.Length;
-
- [SerializeField]
- private bool[] _SynchronizeChildren;
-
-
-
-
- public ref bool[] SynchronizeChildren => ref _SynchronizeChildren;
-
- public const string SynchronizeChildrenField = nameof(_SynchronizeChildren);
-
-
- public override bool IsLooping
- {
- get
- {
- for (int i = _Animations.Length - 1; i >= 0; i--)
- {
- if (AnimancerUtilities.TryGetIsLooping(_Animations[i], out var isLooping) &&
- isLooping)
- return true;
- }
- return false;
- }
- }
-
- public override float MaximumDuration
- {
- get
- {
- if (_Animations == null)
- return 0;
- var duration = 0f;
- var hasSpeeds = HasSpeeds;
- for (int i = _Animations.Length - 1; i >= 0; i--)
- {
- if (!AnimancerUtilities.TryGetLength(_Animations[i], out var length))
- continue;
- if (hasSpeeds)
- length *= _Speeds[i];
- if (duration < length)
- duration = length;
- }
- return duration;
- }
- }
-
- public virtual float AverageAngularSpeed
- {
- get
- {
- if (_Animations == null)
- return default;
- var average = 0f;
- var hasSpeeds = HasSpeeds;
- var count = 0;
- for (int i = _Animations.Length - 1; i >= 0; i--)
- {
- if (AnimancerUtilities.TryGetAverageAngularSpeed(_Animations[i], out var speed))
- {
- if (hasSpeeds)
- speed *= _Speeds[i];
- average += speed;
- count++;
- }
- }
- return average / count;
- }
- }
-
- public virtual Vector3 AverageVelocity
- {
- get
- {
- if (_Animations == null)
- return default;
- var average = new Vector3();
- var hasSpeeds = HasSpeeds;
- var count = 0;
- for (int i = _Animations.Length - 1; i >= 0; i--)
- {
- if (AnimancerUtilities.TryGetAverageVelocity(_Animations[i], out var velocity))
- {
- if (hasSpeeds)
- velocity *= _Speeds[i];
- average += velocity;
- count++;
- }
- }
- return average / count;
- }
- }
-
-
- public override bool IsValid
- {
- get
- {
- if (_Animations == null ||
- _Animations.Length == 0)
- return false;
- for (int i = _Animations.Length - 1; i >= 0; i--)
- if (_Animations[i] == null)
- return false;
- return true;
- }
- }
-
-
- public virtual void InitializeState()
- {
- var mixer = State;
- var auto = MixerState.AutoSynchronizeChildren;
- try
- {
- MixerState.AutoSynchronizeChildren = false;
- mixer.Initialize(_Animations);
- }
- finally
- {
- MixerState.AutoSynchronizeChildren = auto;
- }
- mixer.InitializeSynchronizedChildren(_SynchronizeChildren);
- if (_Speeds != null)
- {
- #if UNITY_ASSERTIONS
- if (_Speeds.Length != 0 && _Speeds.Length != _Animations.Length)
- Debug.LogError(
- $"The number of serialized {nameof(Speeds)} ({_Speeds.Length})" +
- $" does not match the number of {nameof(Animations)} ({_Animations.Length}).",
- mixer.Root?.Component as Object);
- #endif
- var children = mixer.ChildStates;
- var count = Math.Min(children.Count, _Speeds.Length);
- while (--count >= 0)
- children[count].Speed = _Speeds[count];
- }
- }
-
-
- public override void Apply(AnimancerState state)
- {
- base.Apply(state);
- if (!float.IsNaN(_Speed))
- state.Speed = _Speed;
- for (int i = 0; i < _Animations.Length; i++)
- if (_Animations[i] is ITransition transition)
- transition.Apply(state.GetChild(i));
- }
-
-
- void IAnimationClipCollection.GatherAnimationClips(ICollection<AnimationClip> clips)
- => clips.GatherFromSource(_Animations);
-
-
- public virtual void CopyFrom(ManualMixerTransition<TMixer> copyFrom)
- {
- CopyFrom((AnimancerTransition<TMixer>)copyFrom);
- if (copyFrom == null)
- {
- _Speed = 1;
- _Animations = default;
- _Speeds = default;
- _SynchronizeChildren = default;
- return;
- }
- _Speed = copyFrom._Speed;
- AnimancerUtilities.CopyExactArray(copyFrom._Animations, ref _Animations);
- AnimancerUtilities.CopyExactArray(copyFrom._Speeds, ref _Speeds);
- AnimancerUtilities.CopyExactArray(copyFrom._SynchronizeChildren, ref _SynchronizeChildren);
- }
-
- }
- }
|