SpineVisualElementAttributeDrawers.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated April 5, 2025. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2025, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. //#define CHANGE_BOUNDS_ON_ANIMATION_CHANGE
  30. using UnityEditor;
  31. using UnityEditor.UIElements;
  32. using UnityEngine;
  33. using UnityEngine.UIElements;
  34. namespace Spine.Unity.Editor {
  35. [CustomPropertyDrawer(typeof(BoundsFromAnimationAttribute))]
  36. public class BoundsFromAnimationAttributeDrawer : PropertyDrawer {
  37. protected BoundsFromAnimationAttribute TargetAttribute { get { return (BoundsFromAnimationAttribute)attribute; } }
  38. public override VisualElement CreatePropertyGUI (SerializedProperty boundsProperty) {
  39. var container = new VisualElement();
  40. PropertyField referenceMeshBounds = new PropertyField();
  41. referenceMeshBounds.BindProperty(boundsProperty);
  42. var parentPropertyPath = boundsProperty.propertyPath.Substring(0, boundsProperty.propertyPath.LastIndexOf('.'));
  43. var parent = boundsProperty.serializedObject.FindProperty(parentPropertyPath);
  44. SerializedProperty animationProperty = parent.FindPropertyRelative(TargetAttribute.animationField);
  45. SerializedProperty skeletonDataProperty = parent.FindPropertyRelative(TargetAttribute.dataField);
  46. SerializedProperty skinProperty = parent.FindPropertyRelative(TargetAttribute.skinField);
  47. #if !CHANGE_BOUNDS_ON_ANIMATION_CHANGE
  48. Button updateBoundsButton = new Button(() => {
  49. UpdateMeshBounds(boundsProperty, animationProperty.stringValue,
  50. (SkeletonDataAsset)skeletonDataProperty.objectReferenceValue, skinProperty.stringValue);
  51. });
  52. updateBoundsButton.text = "Update Bounds";
  53. container.Add(updateBoundsButton);
  54. #else
  55. referenceMeshBounds.TrackPropertyValue(animationProperty, prop => {
  56. UpdateMeshBounds(boundsProperty, animationProperty.stringValue,
  57. (SkeletonDataAsset)skeletonDataProperty.objectReferenceValue, skinProperty.stringValue);
  58. });
  59. #endif
  60. container.Add(referenceMeshBounds);
  61. container.Bind(boundsProperty.serializedObject);
  62. return container;
  63. }
  64. protected void UpdateMeshBounds (SerializedProperty boundsProperty, string boundsAnimation,
  65. SkeletonDataAsset skeletonDataAsset, string skin) {
  66. if (!skeletonDataAsset)
  67. return;
  68. Bounds bounds = CalculateMeshBounds(boundsAnimation, skeletonDataAsset, skin);
  69. if (bounds.extents.x == 0 || bounds.extents.y == 0) {
  70. Debug.LogWarning("Please select different Initial Skin and Bounds Animation. Not setting reference " +
  71. "bounds as current combination (likely no attachments visible) leads to zero Mesh bounds.");
  72. bounds.center = Vector3.zero;
  73. bounds.extents = Vector3.one * 2f;
  74. }
  75. boundsProperty.boundsValue = bounds;
  76. boundsProperty.serializedObject.ApplyModifiedProperties();
  77. }
  78. protected Bounds CalculateMeshBounds (string animationName, SkeletonDataAsset skeletonDataAsset, string skin) {
  79. SkeletonData skeletonData = skeletonDataAsset.GetSkeletonData(false);
  80. Skeleton skeleton = new Skeleton(skeletonData);
  81. if (!string.IsNullOrEmpty(skin) && !string.Equals(skin, "default", System.StringComparison.Ordinal))
  82. skeleton.SetSkin(skin);
  83. skeleton.SetSlotsToSetupPose();
  84. Spine.Animation animation = skeletonData.FindAnimation(animationName);
  85. if (animation != null)
  86. animation.Apply(skeleton, -1, 0, false, null, 1.0f, MixBlend.First, MixDirection.In);
  87. skeleton.Update(0f);
  88. skeleton.UpdateWorldTransform(Skeleton.Physics.Update);
  89. float x, y, width, height;
  90. SkeletonClipping clipper = new SkeletonClipping();
  91. float[] vertexBuffer = null;
  92. skeleton.GetBounds(out x, out y, out width, out height, ref vertexBuffer, clipper);
  93. if (x == int.MaxValue) {
  94. return new Bounds();
  95. }
  96. Bounds bounds = new Bounds();
  97. Vector2 halfSize = new Vector2(width * 0.5f, height * 0.5f);
  98. bounds.center = new Vector3(x + halfSize.x, -y - halfSize.y, 0.0f);
  99. bounds.extents = new Vector3(halfSize.x, halfSize.y, 0.0f);
  100. return bounds;
  101. }
  102. }
  103. }