FAnimationTrack.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. namespace Flux
  4. {
  5. public class FAnimationTrack : FTransformTrack {
  6. // Animation Previews, stored in <sequenceInstanceId, <ownerInstanceId, animPreview>>
  7. private static Dictionary<int, Dictionary<int, FAnimationTrackCache>> _animPreviews = new Dictionary<int, Dictionary<int, FAnimationTrackCache>>();
  8. public static FAnimationTrackCache GetAnimationPreview( FSequence sequence, Transform owner )
  9. {
  10. Dictionary<int, FAnimationTrackCache> sequencePreviews = null;
  11. FAnimationTrackCache animationTrackCache = null;
  12. if( _animPreviews.TryGetValue( sequence.GetInstanceID(), out sequencePreviews ) )
  13. {
  14. sequencePreviews.TryGetValue(owner.GetInstanceID(), out animationTrackCache );
  15. }
  16. return animationTrackCache;
  17. }
  18. private static FAnimationTrackCache GetAnimationPreview( FAnimationTrack animTrack )
  19. {
  20. return GetAnimationPreview( animTrack, true );
  21. }
  22. private static FAnimationTrackCache GetAnimationPreview( FAnimationTrack animTrack, bool createIfDoesntExist )
  23. {
  24. Dictionary<int, FAnimationTrackCache> sequencePreviews = null;
  25. if( !_animPreviews.TryGetValue( animTrack.Sequence.GetInstanceID(), out sequencePreviews ) )
  26. {
  27. if( !createIfDoesntExist )
  28. return null;
  29. sequencePreviews = new Dictionary<int, FAnimationTrackCache>();
  30. _animPreviews.Add( animTrack.Sequence.GetInstanceID(), sequencePreviews );
  31. }
  32. FAnimationTrackCache preview = null;
  33. if( !sequencePreviews.TryGetValue( animTrack.Owner.GetInstanceID(), out preview ) )
  34. {
  35. if( !createIfDoesntExist )
  36. return null;
  37. preview = new FAnimationTrackCache( animTrack );
  38. sequencePreviews.Add( animTrack.Owner.GetInstanceID(), preview );
  39. }
  40. return preview;
  41. }
  42. private static void DeleteAnimationPreview( FAnimationTrack animTrack )
  43. {
  44. Dictionary<int, FAnimationTrackCache> sequencePreviews = null;
  45. if( _animPreviews.TryGetValue( animTrack.Sequence.GetInstanceID(), out sequencePreviews ) )
  46. {
  47. sequencePreviews[animTrack.Owner.GetInstanceID()].Clear();
  48. sequencePreviews.Remove(animTrack.Owner.GetInstanceID());
  49. }
  50. }
  51. public static void DeleteAnimationPreviews( FSequence sequence )
  52. {
  53. Dictionary<int, FAnimationTrackCache> sequencePreviews = null;
  54. if( _animPreviews.TryGetValue( sequence.GetInstanceID(), out sequencePreviews ) )
  55. {
  56. Dictionary<int, FAnimationTrackCache>.Enumerator e = sequencePreviews.GetEnumerator();
  57. while( e.MoveNext() )
  58. {
  59. e.Current.Value.Clear();
  60. }
  61. sequencePreviews.Clear();
  62. _animPreviews.Remove( sequence.GetInstanceID() );
  63. }
  64. }
  65. // what is the animation controller that will be used to
  66. // build this track's animation state machine
  67. [SerializeField]
  68. [HideInInspector]
  69. private RuntimeAnimatorController _animatorController = null;
  70. public RuntimeAnimatorController AnimatorController { get { return _animatorController; } }
  71. [SerializeField]
  72. [HideInInspector]
  73. private string _layerName = null;
  74. public string LayerName { get { return _layerName; } }
  75. [SerializeField]
  76. [HideInInspector]
  77. private int _layerId = -1;
  78. public int LayerId { get { return _layerId; } }
  79. // private TransformSnapshot _snapshot = null;
  80. // public TransformSnapshot Snapshot { get { return _snapshot; } }
  81. public override CacheMode RequiredCacheMode {
  82. get {
  83. return CacheMode.Editor | CacheMode.RuntimeBackwards;
  84. }
  85. }
  86. public override CacheMode AllowedCacheMode {
  87. get {
  88. return RequiredCacheMode | CacheMode.RuntimeForward;
  89. }
  90. }
  91. public override void Init()
  92. {
  93. if( Owner.GetComponent<Animator>() == null )
  94. Owner.gameObject.AddComponent<Animator>();
  95. base.Init();
  96. _snapshot.TakeChildSnapshots();
  97. }
  98. public override void Stop ()
  99. {
  100. if( HasCache && Cache.Track == this )
  101. {
  102. ((FAnimationTrackCache)Cache).StopPlayback();
  103. Owner.GetComponent<Animator>().enabled = false;
  104. }
  105. base.Stop();
  106. }
  107. public override void UpdateEventsEditor( int frame, float time )
  108. {
  109. if( HasCache && Cache.Track == this )
  110. {
  111. GetPreviewAt( time );
  112. }
  113. }
  114. public override void UpdateEvents( int frame, float time )
  115. {
  116. if( Sequence.Speed != 1 && !HasCache )
  117. Owner.GetComponent<Animator>().speed = Sequence.Speed;
  118. if( HasCache )
  119. {
  120. if( Cache.Track == this ) // only one of them needs to set the playback
  121. GetPreviewAt( time );
  122. }
  123. else
  124. base.UpdateEvents(frame, time);
  125. }
  126. public override void CreateCache()
  127. {
  128. FAnimationTrackCache preview = GetAnimationPreview( this );
  129. preview.Build(true);
  130. }
  131. public override void ClearCache()
  132. {
  133. FAnimationTrackCache preview = (FAnimationTrackCache)Cache;//GetAnimationPreview( this, false );
  134. if( preview != null )
  135. {
  136. if( preview.NumberTracksCached <= 1 )
  137. DeleteAnimationPreview( this );
  138. else
  139. preview.Build(true);
  140. }
  141. Cache = null;
  142. }
  143. public override bool CanCreateCache()
  144. {
  145. if( _animatorController == null )
  146. return false;
  147. List<FEvent> evts = Events;
  148. for( int i = 0; i != evts.Count; ++i )
  149. {
  150. if( ((FPlayAnimationEvent)evts[i])._animationClip == null )
  151. return false;
  152. }
  153. return true;
  154. }
  155. private void GetPreviewAt( float time )
  156. {
  157. Cache.GetPlaybackAt( time );
  158. }
  159. private bool HasAnimationOnFrame( int frame )
  160. {
  161. FEvent[] evts = new FEvent[2];
  162. int numEvents = GetEventsAt( frame, evts );
  163. if( numEvents == 0 )
  164. return false;
  165. return ((FPlayAnimationEvent)evts[0])._animationClip != null || (numEvents == 2 && ((FPlayAnimationEvent)evts[1])._animationClip != null );
  166. }
  167. }
  168. }