IAnimationClipCollection.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using UnityEngine;
  7. using UnityEngine.Playables;
  8. namespace Animancer
  9. {
  10. /// <summary>
  11. /// A variant of <see cref="IAnimationClipSource"/> which uses a <see cref="ICollection{T}"/> instead of a
  12. /// <see cref="List{T}"/> so that it can take a <see cref="HashSet{T}"/> to efficiently avoid adding duplicates.
  13. /// <see cref="AnimancerUtilities"/> contains various extension methods for this purpose.
  14. /// </summary>
  15. /// <remarks>
  16. /// <see cref="IAnimationClipSource"/> still needs to be the main point of entry for the Animation Window, so this
  17. /// interface is only used internally.
  18. /// </remarks>
  19. /// https://kybernetik.com.au/animancer/api/Animancer/IAnimationClipCollection
  20. ///
  21. public interface IAnimationClipCollection
  22. {
  23. /************************************************************************************************************************/
  24. /// <summary>Adds all the animations associated with this object to the `clips`.</summary>
  25. void GatherAnimationClips(ICollection<AnimationClip> clips);
  26. /************************************************************************************************************************/
  27. }
  28. /************************************************************************************************************************/
  29. /// https://kybernetik.com.au/animancer/api/Animancer/AnimancerUtilities
  30. public static partial class AnimancerUtilities
  31. {
  32. /************************************************************************************************************************/
  33. /// <summary>[Animancer Extension]
  34. /// Adds the `clip` to the `clips` if it wasn't there already.
  35. /// </summary>
  36. public static void Gather(this ICollection<AnimationClip> clips, AnimationClip clip)
  37. {
  38. if (clip != null && !clips.Contains(clip))
  39. clips.Add(clip);
  40. }
  41. /************************************************************************************************************************/
  42. /// <summary>[Animancer Extension]
  43. /// Calls <see cref="Gather(ICollection{AnimationClip}, AnimationClip)"/> for each of the `newClips`.
  44. /// </summary>
  45. public static void Gather(this ICollection<AnimationClip> clips, IList<AnimationClip> gatherFrom)
  46. {
  47. if (gatherFrom == null)
  48. return;
  49. for (int i = gatherFrom.Count - 1; i >= 0; i--)
  50. clips.Gather(gatherFrom[i]);
  51. }
  52. /************************************************************************************************************************/
  53. /// <summary>[Animancer Extension]
  54. /// Calls <see cref="Gather(ICollection{AnimationClip}, AnimationClip)"/> for each of the `newClips`.
  55. /// </summary>
  56. public static void Gather(this ICollection<AnimationClip> clips, IEnumerable<AnimationClip> gatherFrom)
  57. {
  58. if (gatherFrom == null)
  59. return;
  60. foreach (var clip in gatherFrom)
  61. clips.Gather(clip);
  62. }
  63. /************************************************************************************************************************/
  64. /// <summary>[Animancer Extension]
  65. /// Calls <see cref="Gather(ICollection{AnimationClip}, AnimationClip)"/> for each clip in the `asset`.
  66. /// </summary>
  67. public static void GatherFromAsset(this ICollection<AnimationClip> clips, PlayableAsset asset)
  68. {
  69. if (asset == null)
  70. return;
  71. // We want to get the tracks out of a TimelineAsset without actually referencing that class directly
  72. // because it comes from an optional package and Animancer does not need to depend on that package.
  73. var method = asset.GetType().GetMethod("GetRootTracks");
  74. if (method != null &&
  75. typeof(IEnumerable).IsAssignableFrom(method.ReturnType) &&
  76. method.GetParameters().Length == 0)
  77. {
  78. var rootTracks = method.Invoke(asset, null);
  79. GatherFromTracks(clips, rootTracks as IEnumerable);
  80. }
  81. }
  82. /************************************************************************************************************************/
  83. /// <summary>Gathers all the animations in the `tracks`.</summary>
  84. private static void GatherFromTracks(ICollection<AnimationClip> clips, IEnumerable tracks)
  85. {
  86. if (tracks == null)
  87. return;
  88. foreach (var track in tracks)
  89. {
  90. if (track == null)
  91. continue;
  92. var trackType = track.GetType();
  93. var getClips = trackType.GetMethod("GetClips");
  94. if (getClips != null &&
  95. typeof(IEnumerable).IsAssignableFrom(getClips.ReturnType) &&
  96. getClips.GetParameters().Length == 0)
  97. {
  98. var trackClips = getClips.Invoke(track, null) as IEnumerable;
  99. if (trackClips != null)
  100. {
  101. foreach (var clip in trackClips)
  102. {
  103. var animationClip = clip.GetType().GetProperty("animationClip");
  104. if (animationClip != null &&
  105. animationClip.PropertyType == typeof(AnimationClip))
  106. {
  107. var getClip = animationClip.GetGetMethod();
  108. clips.Gather(getClip.Invoke(clip, null) as AnimationClip);
  109. }
  110. }
  111. }
  112. }
  113. var getChildTracks = trackType.GetMethod("GetChildTracks");
  114. if (getChildTracks != null &&
  115. typeof(IEnumerable).IsAssignableFrom(getChildTracks.ReturnType) &&
  116. getChildTracks.GetParameters().Length == 0)
  117. {
  118. var childTracks = getChildTracks.Invoke(track, null);
  119. GatherFromTracks(clips, childTracks as IEnumerable);
  120. }
  121. }
  122. }
  123. /************************************************************************************************************************/
  124. /// <summary>[Animancer Extension]
  125. /// Calls <see cref="Gather(ICollection{AnimationClip}, AnimationClip)"/> for each clip gathered by
  126. /// <see cref="IAnimationClipSource.GetAnimationClips"/>.
  127. /// </summary>
  128. public static void GatherFromSource(this ICollection<AnimationClip> clips, IAnimationClipSource source)
  129. {
  130. if (source == null)
  131. return;
  132. var list = ObjectPool.AcquireList<AnimationClip>();
  133. source.GetAnimationClips(list);
  134. clips.Gather(list);
  135. ObjectPool.Release(list);
  136. }
  137. /************************************************************************************************************************/
  138. /// <summary>[Animancer Extension]
  139. /// Calls <see cref="GatherFromSource(ICollection{AnimationClip}, object)"/> for each item in the `source`.
  140. /// </summary>
  141. public static void GatherFromSource(this ICollection<AnimationClip> clips, IEnumerable source)
  142. {
  143. if (source != null)
  144. foreach (var item in source)
  145. clips.GatherFromSource(item);
  146. }
  147. /************************************************************************************************************************/
  148. /// <summary>[Animancer Extension]
  149. /// Calls <see cref="Gather(ICollection{AnimationClip}, AnimationClip)"/> for each clip in the `source`,
  150. /// supporting both <see cref="IAnimationClipSource"/> and <see cref="IAnimationClipCollection"/>.
  151. /// </summary>
  152. public static bool GatherFromSource(this ICollection<AnimationClip> clips, object source)
  153. {
  154. if (TryGetWrappedObject(source, out AnimationClip clip))
  155. {
  156. clips.Gather(clip);
  157. return true;
  158. }
  159. if (TryGetWrappedObject(source, out IAnimationClipCollection collectionSource))
  160. {
  161. collectionSource.GatherAnimationClips(clips);
  162. return true;
  163. }
  164. if (TryGetWrappedObject(source, out IAnimationClipSource listSource))
  165. {
  166. clips.GatherFromSource(listSource);
  167. return true;
  168. }
  169. if (TryGetWrappedObject(source, out IEnumerable enumerable))
  170. {
  171. clips.GatherFromSource(enumerable);
  172. return true;
  173. }
  174. return false;
  175. }
  176. /************************************************************************************************************************/
  177. /// <summary>
  178. /// Attempts to get the <see cref="AnimationClip.frameRate"/> from the `clipSource` and returns true if
  179. /// successful. If it has multiple animations with different rates, this method returns false.
  180. /// </summary>
  181. public static bool TryGetFrameRate(object clipSource, out float frameRate)
  182. {
  183. using (ObjectPool.Disposable.AcquireSet<AnimationClip>(out var clips))
  184. {
  185. clips.GatherFromSource(clipSource);
  186. if (clips.Count == 0)
  187. {
  188. frameRate = float.NaN;
  189. return false;
  190. }
  191. frameRate = float.NaN;
  192. foreach (var clip in clips)
  193. {
  194. if (float.IsNaN(frameRate))
  195. {
  196. frameRate = clip.frameRate;
  197. }
  198. else if (frameRate != clip.frameRate)
  199. {
  200. frameRate = float.NaN;
  201. return false;
  202. }
  203. }
  204. return frameRate > 0;
  205. }
  206. }
  207. /************************************************************************************************************************/
  208. }
  209. }