ByBonesRootBoneMapper.cs 1.1 KB

123456789101112131415161718192021222324252627282930
  1. using System.Collections.Generic;
  2. using TriLibCore.Extensions;
  3. using TriLibCore.Interfaces;
  4. using UnityEngine;
  5. namespace TriLibCore.Mappers
  6. {
  7. /// <summary>Represents a Mapper that looks for the Game Object which has only a Transform component and has the biggest number of children as the root bone.</summary>
  8. [CreateAssetMenu(menuName = "TriLib/Mappers/Root Bone/By Bones Root Bone Mapper", fileName = "ByBonesRootBoneMapper")]
  9. public class ByBonesRootBoneMapper : RootBoneMapper
  10. {
  11. /// <inheritdoc />
  12. public override Transform Map(AssetLoaderContext assetLoaderContext, IList<Transform> bones)
  13. {
  14. Transform bestBone = null;
  15. var bestChildrenCount = 0;
  16. for (var i = 0; i < bones.Count; i++)
  17. {
  18. var bone = bones[i];
  19. var childrenCount = bone.CountChild();
  20. if (childrenCount >= bestChildrenCount)
  21. {
  22. bestChildrenCount = childrenCount;
  23. bestBone = bone;
  24. }
  25. }
  26. return bestBone;
  27. }
  28. }
  29. }