Açıklama Yok
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.

SpriteSkinHelpers.cs 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. namespace UnityEngine.U2D.Animation
  4. {
  5. internal static class SpriteSkinHelpers
  6. {
  7. public static void CacheChildren(Transform current, Dictionary<int, List<SpriteSkin.TransformData>> cache)
  8. {
  9. var nameHash = current.name.GetHashCode();
  10. var entry = new SpriteSkin.TransformData()
  11. {
  12. fullName = string.Empty,
  13. transform = current
  14. };
  15. if (cache.TryGetValue(nameHash, out var value))
  16. value.Add(entry);
  17. else
  18. cache.Add(nameHash, new List<SpriteSkin.TransformData>(1) { entry });
  19. for (var i = 0; i < current.childCount; ++i)
  20. CacheChildren(current.GetChild(i), cache);
  21. }
  22. public static string GenerateTransformPath(Transform rootBone, Transform child)
  23. {
  24. var path = child.name;
  25. if (child == rootBone)
  26. return path;
  27. var parent = child.parent;
  28. do
  29. {
  30. path = parent.name + "/" + path;
  31. parent = parent.parent;
  32. } while (parent != rootBone && parent != null);
  33. return path;
  34. }
  35. public static bool GetSpriteBonesTransforms(SpriteSkin spriteSkin, out Transform[] outTransform)
  36. {
  37. var rootBone = spriteSkin.rootBone;
  38. var spriteBones = spriteSkin.sprite.GetBones();
  39. if (rootBone == null)
  40. throw new ArgumentException("rootBone parameter cannot be null");
  41. if (spriteBones == null)
  42. throw new ArgumentException("spriteBones parameter cannot be null");
  43. outTransform = new Transform[spriteBones.Length];
  44. var boneObjects = rootBone.GetComponentsInChildren<Bone>();
  45. if (boneObjects != null && boneObjects.Length >= spriteBones.Length)
  46. {
  47. using (SpriteSkin.Profiling.getSpriteBonesTransformFromGuid.Auto())
  48. {
  49. var i = 0;
  50. for (; i < spriteBones.Length; ++i)
  51. {
  52. var boneHash = spriteBones[i].guid;
  53. var boneTransform = Array.Find(boneObjects, x => (x.guid == boneHash));
  54. if (boneTransform == null)
  55. break;
  56. outTransform[i] = boneTransform.transform;
  57. }
  58. if (i >= spriteBones.Length)
  59. return true;
  60. }
  61. }
  62. var hierarchyCache = spriteSkin.m_HierarchyCache;
  63. if (hierarchyCache.Count == 0)
  64. spriteSkin.CacheHierarchy();
  65. // If unable to successfully map via guid, fall back to path
  66. return GetSpriteBonesTransformFromPath(spriteBones, hierarchyCache, outTransform);
  67. }
  68. static bool GetSpriteBonesTransformFromPath(SpriteBone[] spriteBones, Dictionary<int, List<SpriteSkin.TransformData>> hierarchyCache, Transform[] outNewBoneTransform)
  69. {
  70. using (SpriteSkin.Profiling.getSpriteBonesTransformFromPath.Auto())
  71. {
  72. string[] bonePath = null;
  73. var foundBones = true;
  74. for (var i = 0; i < spriteBones.Length; ++i)
  75. {
  76. var nameHash = spriteBones[i].name.GetHashCode();
  77. if (!hierarchyCache.TryGetValue(nameHash, out var children))
  78. {
  79. outNewBoneTransform[i] = null;
  80. foundBones = false;
  81. continue;
  82. }
  83. if (children.Count == 1)
  84. outNewBoneTransform[i] = children[0].transform;
  85. else
  86. {
  87. if (bonePath == null)
  88. bonePath = new string[spriteBones.Length];
  89. if (bonePath[i] == null)
  90. CalculateBoneTransformsPath(i, spriteBones, bonePath);
  91. var m = 0;
  92. for (; m < children.Count; ++m)
  93. {
  94. if (children[m].fullName.Contains(bonePath[i]))
  95. {
  96. outNewBoneTransform[i] = children[m].transform;
  97. break;
  98. }
  99. }
  100. if (m >= children.Count)
  101. {
  102. outNewBoneTransform[i] = null;
  103. foundBones = false;
  104. }
  105. }
  106. }
  107. return foundBones;
  108. }
  109. }
  110. static void CalculateBoneTransformsPath(int index, SpriteBone[] spriteBones, string[] paths)
  111. {
  112. var spriteBone = spriteBones[index];
  113. var parentId = spriteBone.parentId;
  114. var bonePath = spriteBone.name;
  115. if (parentId != -1)
  116. {
  117. if (paths[parentId] == null)
  118. CalculateBoneTransformsPath(spriteBone.parentId, spriteBones, paths);
  119. paths[index] = $"{paths[parentId]}/{bonePath}";
  120. }
  121. else
  122. paths[index] = bonePath;
  123. }
  124. }
  125. }