No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BoneCacheExtensions.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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, ToCharacterIfNeeded);
  11. }
  12. public static BoneCache[] ToSpriteSheetIfNeeded(this BoneCache[] bones)
  13. {
  14. return Array.ConvertAll(bones, ToSpriteSheetIfNeeded);
  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. guid = bone.guid,
  81. position = new Vector3(position.x, position.y, bone.depth),
  82. rotation = rotation,
  83. length = bone.localLength,
  84. parentId = parentId,
  85. color = bone.bindPoseColor
  86. };
  87. }
  88. public static UnityEngine.U2D.SpriteBone[] ToSpriteBone(this BoneCache[] bones, Matrix4x4 rootTransform)
  89. {
  90. var spriteBones = new List<UnityEngine.U2D.SpriteBone>();
  91. foreach (var bone in bones)
  92. {
  93. var parentId = -1;
  94. if (ArrayUtility.Contains(bones, bone.parentBone))
  95. parentId = Array.IndexOf(bones, bone.parentBone);
  96. spriteBones.Add(bone.ToSpriteBone(rootTransform, parentId));
  97. }
  98. return spriteBones.ToArray();
  99. }
  100. }
  101. }