BoneCacheExtensions.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace UnityEditor.U2D.Animation
  5. {
  6. internal static class BoneCacheExtensions
  7. {
  8. public static BoneCache[] ToCharacterIfNeeded(this BoneCache[] bones)
  9. {
  10. return Array.ConvertAll(bones, b => ToCharacterIfNeeded(b));
  11. }
  12. public static BoneCache[] ToSpriteSheetIfNeeded(this BoneCache[] bones)
  13. {
  14. return Array.ConvertAll(bones, b => ToSpriteSheetIfNeeded(b));
  15. }
  16. public static BoneCache ToCharacterIfNeeded(this BoneCache bone)
  17. {
  18. if (bone == null)
  19. return null;
  20. var skinningCache = bone.skinningCache;
  21. if (skinningCache.hasCharacter)
  22. {
  23. if (bone.skeleton != skinningCache.character.skeleton)
  24. {
  25. var selectedSprite = skinningCache.selectedSprite;
  26. if (selectedSprite == null)
  27. return null;
  28. var skeleton = selectedSprite.GetSkeleton();
  29. var characterPart = selectedSprite.GetCharacterPart();
  30. Debug.Assert(skeleton != null);
  31. Debug.Assert(characterPart != null);
  32. Debug.Assert(bone.skeleton == skeleton);
  33. Debug.Assert(skeleton.BoneCount == characterPart.BoneCount);
  34. var index = skeleton.IndexOf(bone);
  35. if (index == -1)
  36. bone = null;
  37. else
  38. bone = characterPart.GetBone(index);
  39. }
  40. }
  41. return bone;
  42. }
  43. public static BoneCache ToSpriteSheetIfNeeded(this BoneCache bone)
  44. {
  45. if (bone == null)
  46. return null;
  47. var skinningCache = bone.skinningCache;
  48. if (skinningCache.hasCharacter && skinningCache.mode == SkinningMode.SpriteSheet)
  49. {
  50. var selectedSprite = skinningCache.selectedSprite;
  51. if (selectedSprite == null)
  52. return null;
  53. var characterSkeleton = skinningCache.character.skeleton;
  54. Debug.Assert(bone.skeleton == characterSkeleton);
  55. var skeleton = selectedSprite.GetSkeleton();
  56. var characterPart = selectedSprite.GetCharacterPart();
  57. Debug.Assert(skeleton != null);
  58. Debug.Assert(characterPart != null);
  59. Debug.Assert(skeleton.BoneCount == characterPart.BoneCount);
  60. var index = characterPart.IndexOf(bone);
  61. if (index == -1)
  62. bone = null;
  63. else
  64. bone = skeleton.GetBone(index);
  65. }
  66. return bone;
  67. }
  68. public static UnityEngine.U2D.SpriteBone ToSpriteBone(this BoneCache bone, Matrix4x4 rootTransform, int parentId)
  69. {
  70. var position = bone.localPosition;
  71. var rotation = bone.localRotation;
  72. if (parentId == -1)
  73. {
  74. rotation = bone.rotation;
  75. position = rootTransform.inverse.MultiplyPoint3x4(bone.position);
  76. }
  77. return new UnityEngine.U2D.SpriteBone()
  78. {
  79. name = bone.name,
  80. position = new Vector3(position.x, position.y, bone.depth),
  81. rotation = rotation,
  82. length = bone.localLength,
  83. parentId = parentId
  84. };
  85. }
  86. public static UnityEngine.U2D.SpriteBone[] ToSpriteBone(this BoneCache[] bones, Matrix4x4 rootTransform)
  87. {
  88. var spriteBones = new List<UnityEngine.U2D.SpriteBone>();
  89. foreach (var bone in bones)
  90. {
  91. var parentId = -1;
  92. if (ArrayUtility.Contains(bones, bone.parentBone))
  93. parentId = Array.IndexOf(bones, bone.parentBone);
  94. spriteBones.Add(bone.ToSpriteBone(rootTransform, parentId));
  95. }
  96. return spriteBones.ToArray();
  97. }
  98. }
  99. }