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.

SkeletonCache.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. namespace UnityEditor.U2D.Animation
  5. {
  6. internal class SkeletonCache : TransformCache
  7. {
  8. [SerializeField]
  9. bool m_IsPosePreview = false;
  10. [SerializeField]
  11. List<BoneCache> m_Bones = new List<BoneCache>();
  12. public bool isPosePreview => m_IsPosePreview;
  13. public int boneCount => m_Bones.Count;
  14. public virtual BoneCache[] bones => m_Bones.ToArray();
  15. public void AddBone(BoneCache bone, bool worldPositionStays = true)
  16. {
  17. Debug.Assert(bone != null);
  18. Debug.Assert(!Contains(bone));
  19. if (bone.parent == null)
  20. bone.SetParent(this, worldPositionStays);
  21. m_Bones.Add(bone);
  22. }
  23. public void ReorderBones(IEnumerable<BoneCache> boneCache)
  24. {
  25. if (boneCache.Count() == m_Bones.Count)
  26. {
  27. foreach (var b in m_Bones)
  28. {
  29. if (!boneCache.Contains(b))
  30. return;
  31. }
  32. m_Bones = boneCache.ToList();
  33. }
  34. }
  35. public void DestroyBone(BoneCache bone)
  36. {
  37. Debug.Assert(bone != null);
  38. Debug.Assert(Contains(bone));
  39. m_Bones.Remove(bone);
  40. var boneChildren = bone.children;
  41. foreach (var child in boneChildren)
  42. child.SetParent(bone.parent);
  43. skinningCache.Destroy(bone);
  44. }
  45. public void SetDefaultPose()
  46. {
  47. foreach (var bone in m_Bones)
  48. bone.SetDefaultPose();
  49. m_IsPosePreview = false;
  50. }
  51. public void RestoreDefaultPose()
  52. {
  53. foreach (var bone in m_Bones)
  54. bone.RestoreDefaultPose();
  55. m_IsPosePreview = false;
  56. skinningCache.events.skeletonPreviewPoseChanged.Invoke(this);
  57. }
  58. public void SetPosePreview()
  59. {
  60. m_IsPosePreview = true;
  61. }
  62. public BonePose[] GetLocalPose()
  63. {
  64. var pose = new List<BonePose>();
  65. foreach (var bone in m_Bones)
  66. pose.Add(bone.localPose);
  67. return pose.ToArray();
  68. }
  69. public void SetLocalPose(BonePose[] pose)
  70. {
  71. Debug.Assert(m_Bones.Count == pose.Length);
  72. for (var i = 0; i < m_Bones.Count; ++i)
  73. m_Bones[i].localPose = pose[i];
  74. m_IsPosePreview = true;
  75. }
  76. public BonePose[] GetWorldPose()
  77. {
  78. var pose = new List<BonePose>();
  79. foreach (var bone in m_Bones)
  80. pose.Add(bone.worldPose);
  81. return pose.ToArray();
  82. }
  83. public void SetWorldPose(BonePose[] pose)
  84. {
  85. Debug.Assert(m_Bones.Count == pose.Length);
  86. for (var i = 0; i < m_Bones.Count; ++i)
  87. {
  88. var bone = m_Bones[i];
  89. var childWoldPose = bone.GetChildrenWoldPose();
  90. bone.worldPose = pose[i];
  91. bone.SetChildrenWorldPose(childWoldPose);
  92. }
  93. m_IsPosePreview = true;
  94. }
  95. public BoneCache GetBone(int index)
  96. {
  97. return m_Bones[index];
  98. }
  99. public int IndexOf(BoneCache bone)
  100. {
  101. return m_Bones.IndexOf(bone);
  102. }
  103. public bool Contains(BoneCache bone)
  104. {
  105. return m_Bones.Contains(bone);
  106. }
  107. public void Clear()
  108. {
  109. var roots = children;
  110. foreach (var root in roots)
  111. DestroyHierarchy(root);
  112. m_Bones.Clear();
  113. }
  114. public string GetUniqueName(BoneCache bone)
  115. {
  116. Debug.Assert(Contains(bone));
  117. var boneName = bone.name;
  118. var names = m_Bones.ConvertAll(b => b.name);
  119. var index = IndexOf(bone);
  120. var count = 0;
  121. Debug.Assert(index < names.Count);
  122. for (var i = 0; i < index; ++i)
  123. if (names[i].Equals(boneName))
  124. ++count;
  125. return count == 0 ? boneName : $"{boneName} ({count + 1})";
  126. }
  127. void DestroyHierarchy(TransformCache root)
  128. {
  129. Debug.Assert(root != null);
  130. var rootChildren = root.children;
  131. foreach (var child in rootChildren)
  132. DestroyHierarchy(child);
  133. skinningCache.Destroy(root);
  134. }
  135. }
  136. }