LegacyToHumanoidAnimationClipMapper.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using TriLibCore.Utils;
  2. using UnityEngine;
  3. namespace TriLibCore.Mappers
  4. {
  5. /// <summary>Represents a Mapper that converts legacy Animation Clips into humanoid Animation Clips.</summary>
  6. [CreateAssetMenu(menuName = "TriLib/Mappers/Animation Clip/Legacy To Humanoid Animation Clip Mapper", fileName = "LegacyToHumanoidAnimationClipMapper")]
  7. public class LegacyToHumanoidAnimationClipMapper : AnimationClipMapper
  8. {
  9. /// <summary>
  10. /// Template mecanim animation clip.
  11. /// Unity runtime API can't access mecanim animation clip settings as root motion baking, animation loop mode, etc.
  12. /// So we get these settings from the template animation clip.
  13. /// </summary>
  14. public AnimationClip MecanimAnimationClipTemplate;
  15. /// <inheritdoc/>
  16. public override AnimationClip[] MapArray(AssetLoaderContext assetLoaderContext, AnimationClip[] sourceAnimationClips)
  17. {
  18. var animator = assetLoaderContext.RootGameObject.GetComponent<Animator>();
  19. if (animator != null)
  20. {
  21. animator.enabled = false;
  22. if (MecanimAnimationClipTemplate == null)
  23. {
  24. if (assetLoaderContext.Options.ShowLoadingWarnings)
  25. {
  26. Debug.LogWarning("No MecanimAnimationClipTemplate specified when using the LegacyToHumanoidAnimationClipMapper.");
  27. }
  28. MecanimAnimationClipTemplate = new AnimationClip();
  29. }
  30. for (var i = 0; i < sourceAnimationClips.Length; i++)
  31. {
  32. var oldAnimationClip = sourceAnimationClips[i];
  33. sourceAnimationClips[i] = HumanoidRetargeter.ConvertLegacyIntoHumanoidAnimationClip(assetLoaderContext.RootGameObject, animator.avatar, sourceAnimationClips[i], MecanimAnimationClipTemplate);
  34. Destroy(oldAnimationClip);
  35. }
  36. animator.enabled = true;
  37. }
  38. return sourceAnimationClips;
  39. }
  40. }
  41. }