ByNameRootBoneMapper.cs 2.1 KB

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