ByNameAnimatorOverrideAnimationClipMapper.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Collections.Generic;
  2. using TriLibCore.General;
  3. using TriLibCore.Utils;
  4. using UnityEngine;
  5. namespace TriLibCore.Mappers
  6. {
  7. /// <summary>Represents a Mapper that finds Animator Override Animation Clips by name-matching.</summary>
  8. [CreateAssetMenu(menuName = "TriLib/Mappers/Animation Clip/By Name Animator Override Animation Clip Mapper", fileName = "ByNameAnimatorOverrideAnimationClipMapper")]
  9. public class ByNameAnimatorOverrideAnimationClipMapper : AnimatorOverrideAnimationClipMapper
  10. {
  11. /// <summary>
  12. /// String comparison mode to use on the mapping.
  13. /// </summary>
  14. [Header("Left = Animator Override Clip Names, Right = Loaded Clip Names")]
  15. public StringComparisonMode StringComparisonMode;
  16. /// <summary>
  17. /// Is the string comparison case insensitive?
  18. /// </summary>
  19. public bool CaseInsensitive = true;
  20. /// <inheritdoc />
  21. public override AnimationClip[] MapArray(AssetLoaderContext assetLoaderContext, AnimationClip[] sourceAnimationClips)
  22. {
  23. if (AnimatorOverrideController != null)
  24. {
  25. for (var i = 0; i < sourceAnimationClips.Length; i++)
  26. {
  27. var animationClip = sourceAnimationClips[i];
  28. var overrides = new List<KeyValuePair<AnimationClip, AnimationClip>>(AnimatorOverrideController.overridesCount);
  29. AnimatorOverrideController.GetOverrides(overrides);
  30. for (var j = 0; j < overrides.Count; j++)
  31. {
  32. var kvp = overrides[j];
  33. var keyName = kvp.Key.name;
  34. var clipName = animationClip.name;
  35. if (StringComparer.Matches(StringComparisonMode, CaseInsensitive, keyName, clipName))
  36. {
  37. overrides[j] = new KeyValuePair<AnimationClip, AnimationClip>(kvp.Key, animationClip);
  38. }
  39. }
  40. AnimatorOverrideController.ApplyOverrides(overrides);
  41. }
  42. }
  43. return base.MapArray(assetLoaderContext, sourceAnimationClips);
  44. }
  45. }
  46. }