IAnimationClipCollection.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 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. if (getClips.Invoke(track, null) is IEnumerable trackClips)
  99. {
  100. foreach (var clip in trackClips)
  101. {
  102. var animationClip = clip.GetType().GetProperty("animationClip");
  103. if (animationClip != null &&
  104. animationClip.PropertyType == typeof(AnimationClip))
  105. {
  106. var getClip = animationClip.GetGetMethod();
  107. clips.Gather(getClip.Invoke(clip, null) as AnimationClip);
  108. }
  109. }
  110. }
  111. }
  112. var getChildTracks = trackType.GetMethod("GetChildTracks");
  113. if (getChildTracks != null &&
  114. typeof(IEnumerable).IsAssignableFrom(getChildTracks.ReturnType) &&
  115. getChildTracks.GetParameters().Length == 0)
  116. {
  117. var childTracks = getChildTracks.Invoke(track, null);
  118. GatherFromTracks(clips, childTracks as IEnumerable);
  119. }
  120. }
  121. }
  122. /************************************************************************************************************************/
  123. /// <summary>[Animancer Extension]
  124. /// Calls <see cref="Gather(ICollection{AnimationClip}, AnimationClip)"/> for each clip gathered by
  125. /// <see cref="IAnimationClipSource.GetAnimationClips"/>.
  126. /// </summary>
  127. public static void GatherFromSource(this ICollection<AnimationClip> clips, IAnimationClipSource source)
  128. {
  129. if (source == null)
  130. return;
  131. var list = ListPool.Acquire<AnimationClip>();
  132. source.GetAnimationClips(list);
  133. clips.Gather(list);
  134. ListPool.Release(list);
  135. }
  136. /************************************************************************************************************************/
  137. /// <summary>[Animancer Extension]
  138. /// Calls <see cref="GatherFromSource(ICollection{AnimationClip}, object)"/> for each item in the `source`.
  139. /// </summary>
  140. public static void GatherFromSource(this ICollection<AnimationClip> clips, IEnumerable source)
  141. {
  142. if (source != null)
  143. foreach (var item in source)
  144. clips.GatherFromSource(item);
  145. }
  146. /************************************************************************************************************************/
  147. /// <summary>[Animancer Extension]
  148. /// Calls <see cref="Gather(ICollection{AnimationClip}, AnimationClip)"/> for each clip in the `source`,
  149. /// supporting both <see cref="IAnimationClipSource"/> and <see cref="IAnimationClipCollection"/>.
  150. /// </summary>
  151. public static bool GatherFromSource(this ICollection<AnimationClip> clips, object source)
  152. {
  153. if (TryGetWrappedObject(source, out AnimationClip clip))
  154. {
  155. clips.Gather(clip);
  156. return true;
  157. }
  158. if (TryGetWrappedObject(source, out IAnimationClipCollection collectionSource))
  159. {
  160. collectionSource.GatherAnimationClips(clips);
  161. return true;
  162. }
  163. if (TryGetWrappedObject(source, out IAnimationClipSource listSource))
  164. {
  165. clips.GatherFromSource(listSource);
  166. return true;
  167. }
  168. if (TryGetWrappedObject(source, out IEnumerable enumerable))
  169. {
  170. clips.GatherFromSource(enumerable);
  171. return true;
  172. }
  173. return false;
  174. }
  175. /************************************************************************************************************************/
  176. /// <summary>
  177. /// Attempts to get the <see cref="AnimationClip.frameRate"/> from the `clipSource` and returns true if
  178. /// successful. If it has multiple animations with different rates, this method returns false.
  179. /// </summary>
  180. public static bool TryGetFrameRate(object clipSource, out float frameRate)
  181. {
  182. using (SetPool<AnimationClip>.Instance.Acquire(out var clips))
  183. {
  184. clips.GatherFromSource(clipSource);
  185. if (clips.Count == 0)
  186. {
  187. frameRate = float.NaN;
  188. return false;
  189. }
  190. frameRate = float.NaN;
  191. foreach (var clip in clips)
  192. {
  193. if (float.IsNaN(frameRate))
  194. {
  195. frameRate = clip.frameRate;
  196. }
  197. else if (frameRate != clip.frameRate)
  198. {
  199. frameRate = float.NaN;
  200. return false;
  201. }
  202. }
  203. return frameRate > 0;
  204. }
  205. }
  206. /************************************************************************************************************************/
  207. }
  208. }