ByNameRootBoneMapper.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Collections.Generic;
  2. using TriLibCore.General;
  3. using TriLibCore.Interfaces;
  4. using TriLibCore.Utils;
  5. using UnityEngine;
  6. namespace TriLibCore.Mappers
  7. {
  8. /// <summary>Represents a Mapper that searches for a root bone on the Models by the bone names.</summary>
  9. [CreateAssetMenu(menuName = "TriLib/Mappers/Root Bone/By Name Root Bone Mapper", fileName = "ByNameRootBoneMapper")]
  10. public class ByNameRootBoneMapper : RootBoneMapper
  11. {
  12. /// <summary>
  13. /// String comparison mode to use on the mapping.
  14. /// </summary>
  15. [Header("Left = Loaded GameObjects Names, Right = Names in RootBoneNames")]
  16. public StringComparisonMode StringComparisonMode;
  17. /// <summary>
  18. /// Is the string comparison case insensitive?
  19. /// </summary>
  20. public bool CaseInsensitive = true;
  21. /// <summary>
  22. /// Root bone names to be searched.
  23. /// </summary>
  24. public string[] RootBoneNames = { "Hips", "Bip01", "Pelvis" };
  25. /// <inheritdoc />
  26. public override Transform Map(AssetLoaderContext assetLoaderContext, IList<Transform> bones)
  27. {
  28. if (RootBoneNames != null)
  29. {
  30. for (var i = 0; i < RootBoneNames.Length; i++)
  31. {
  32. var rootBoneName = RootBoneNames[i];
  33. var found = FindDeepChild(bones, rootBoneName);
  34. if (found != null)
  35. {
  36. return found;
  37. }
  38. }
  39. }
  40. return base.Map(assetLoaderContext, bones);
  41. }
  42. private Transform FindDeepChild(IList<Transform> transforms, string right)
  43. {
  44. for (var i = 0; i < transforms.Count; i++)
  45. {
  46. var transform = transforms[i];
  47. if (StringComparer.Matches(StringComparisonMode, CaseInsensitive, transform.name, right))
  48. {
  49. return transform;
  50. }
  51. }
  52. return null;
  53. }
  54. }
  55. }