ByNameHumanoidAvatarMapper.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System.Collections.Generic;
  2. using TriLibCore.General;
  3. using TriLibCore.Extensions;
  4. using UnityEngine;
  5. using HumanLimit = TriLibCore.General.HumanLimit;
  6. namespace TriLibCore.Mappers
  7. {
  8. /// <summary>Represents a Mapper that finds humanoid Avatar bones by name-matching.</summary>
  9. [CreateAssetMenu(menuName = "TriLib/Mappers/Humanoid/By Name Humanoid Avatar Mapper", fileName = "ByNameHumanoidAvatarMapper")]
  10. public class ByNameHumanoidAvatarMapper : HumanoidAvatarMapper
  11. {
  12. /// <summary>
  13. /// String comparison mode to use on the mapping.
  14. /// </summary>
  15. [Header("Left = Loaded GameObjects Names, Right = Names in BonesMapping.BoneNames")]
  16. public StringComparisonMode stringComparisonMode;
  17. /// <summary>
  18. /// Is the string comparison case insensitive?
  19. /// </summary>
  20. public bool CaseInsensitive = true;
  21. /// <summary>
  22. /// The human bone to Unity bone relationship list.
  23. /// </summary>
  24. public List<BoneMapping> BonesMapping;
  25. /// <inheritdoc />
  26. public override Dictionary<BoneMapping, Transform> Map(AssetLoaderContext assetLoaderContext)
  27. {
  28. var mapping = new Dictionary<BoneMapping, Transform>();
  29. foreach (var boneMapping in BonesMapping)
  30. {
  31. if (boneMapping.BoneNames != null)
  32. {
  33. var found = false;
  34. foreach (var boneName in boneMapping.BoneNames)
  35. {
  36. var transform = assetLoaderContext.RootGameObject.transform.FindDeepChild(boneName, stringComparisonMode, CaseInsensitive);
  37. if (transform == null)
  38. {
  39. continue;
  40. }
  41. mapping.Add(boneMapping, transform);
  42. found = true;
  43. break;
  44. }
  45. if (!found && !IsBoneOptional(boneMapping.HumanBone))
  46. {
  47. mapping.Clear();
  48. return mapping;
  49. }
  50. }
  51. }
  52. return mapping;
  53. }
  54. private static bool IsBoneOptional(HumanBodyBones humanBodyBones)
  55. {
  56. return !HumanTrait.RequiredBone((int)humanBodyBones);
  57. }
  58. /// <summary>Adds a new mapping item, containing the humanoid bone type, the limits, and the list of names to look for to the query.</summary>
  59. /// <param name="humanBodyBones">The Human Body Bones (Humanoid Bone type).</param>
  60. /// <param name="humanLimit">The bone Human Limit.</param>
  61. /// <param name="boneNames">The bones Transform names.</param>
  62. public void AddMapping(HumanBodyBones humanBodyBones, HumanLimit humanLimit, params string[] boneNames)
  63. {
  64. if (BonesMapping == null)
  65. {
  66. BonesMapping = new List<BoneMapping>();
  67. }
  68. BonesMapping.Add(new BoneMapping(humanBodyBones, humanLimit, boneNames));
  69. }
  70. }
  71. }