SkeletonBakingWindow.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, 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. using System.Collections;
  30. using System.Collections.Generic;
  31. using UnityEditor;
  32. using UnityEngine;
  33. namespace Spine.Unity.Editor {
  34. using Editor = UnityEditor.Editor;
  35. using Icons = SpineEditorUtilities.Icons;
  36. public class SkeletonBakingWindow : EditorWindow {
  37. const bool IsUtilityWindow = true;
  38. [MenuItem("CONTEXT/SkeletonDataAsset/Skeleton Baking", false, 5000)]
  39. public static void Init (MenuCommand command) {
  40. var window = EditorWindow.GetWindow<SkeletonBakingWindow>(IsUtilityWindow);
  41. window.minSize = new Vector2(330f, 530f);
  42. window.maxSize = new Vector2(600f, 1000f);
  43. window.titleContent = new GUIContent("Skeleton Baking", Icons.spine);
  44. window.skeletonDataAsset = command.context as SkeletonDataAsset;
  45. window.Show();
  46. }
  47. public SkeletonDataAsset skeletonDataAsset;
  48. [SpineSkin(dataField: "skeletonDataAsset")]
  49. public string skinToBake = "default";
  50. // Settings
  51. bool bakeAnimations = false;
  52. bool bakeIK = true;
  53. SendMessageOptions bakeEventOptions;
  54. SerializedObject so;
  55. Skin bakeSkin;
  56. void DataAssetChanged () {
  57. bakeSkin = null;
  58. }
  59. void OnGUI () {
  60. so = so ?? new SerializedObject(this);
  61. EditorGUIUtility.wideMode = true;
  62. EditorGUILayout.LabelField("Spine Skeleton Prefab Baking", EditorStyles.boldLabel);
  63. const string BakingWarningMessage = "\nSkeleton baking is not the primary use case for Spine skeletons." +
  64. "\nUse baking if you have specialized uses, such as simplified skeletons with movement driven by physics." +
  65. "\n\nBaked Skeletons do not support the following:" +
  66. "\n\tDisabled rotation or scale inheritance" +
  67. "\n\tLocal Shear" +
  68. "\n\tAll Constraint types" +
  69. "\n\tWeighted mesh verts with more than 4 bound bones" +
  70. "\n\nBaked Animations do not support the following:" +
  71. "\n\tMesh Deform Keys" +
  72. "\n\tColor Keys" +
  73. "\n\tDraw Order Keys" +
  74. "\n\nAnimation Curves are sampled at 60fps and are not realtime." +
  75. "\nConstraint animations are also baked into animation curves." +
  76. "\nSee SkeletonBaker.cs comments for full details.\n";
  77. EditorGUILayout.HelpBox(BakingWarningMessage, MessageType.Info, true);
  78. EditorGUI.BeginChangeCheck();
  79. var skeletonDataAssetProperty = so.FindProperty("skeletonDataAsset");
  80. EditorGUILayout.PropertyField(skeletonDataAssetProperty, SpineInspectorUtility.TempContent("SkeletonDataAsset", Icons.spine));
  81. if (EditorGUI.EndChangeCheck()) {
  82. so.ApplyModifiedProperties();
  83. DataAssetChanged();
  84. }
  85. EditorGUILayout.Space();
  86. if (skeletonDataAsset == null) return;
  87. var skeletonData = skeletonDataAsset.GetSkeletonData(false);
  88. if (skeletonData == null) return;
  89. bool hasExtraSkins = skeletonData.Skins.Count > 1;
  90. using (new SpineInspectorUtility.BoxScope(false)) {
  91. EditorGUILayout.LabelField(skeletonDataAsset.name, EditorStyles.boldLabel);
  92. using (new SpineInspectorUtility.IndentScope()) {
  93. EditorGUILayout.LabelField(SpineInspectorUtility.TempContent("Bones: " + skeletonData.Bones.Count, Icons.bone));
  94. EditorGUILayout.LabelField(SpineInspectorUtility.TempContent("Slots: " + skeletonData.Slots.Count, Icons.slotRoot));
  95. if (hasExtraSkins) {
  96. EditorGUILayout.LabelField(SpineInspectorUtility.TempContent("Skins: " + skeletonData.Skins.Count, Icons.skinsRoot));
  97. EditorGUILayout.LabelField(SpineInspectorUtility.TempContent("Current skin attachments: " + (bakeSkin == null ? 0 : bakeSkin.Attachments.Count), Icons.skinPlaceholder));
  98. } else if (skeletonData.Skins.Count == 1) {
  99. EditorGUILayout.LabelField(SpineInspectorUtility.TempContent("Skins: 1 (only default Skin)", Icons.skinsRoot));
  100. }
  101. int totalAttachments = 0;
  102. foreach (var s in skeletonData.Skins)
  103. totalAttachments += s.Attachments.Count;
  104. EditorGUILayout.LabelField(SpineInspectorUtility.TempContent("Total Attachments: " + totalAttachments, Icons.genericAttachment));
  105. }
  106. }
  107. using (new SpineInspectorUtility.BoxScope(false)) {
  108. EditorGUILayout.LabelField("Animations", EditorStyles.boldLabel);
  109. EditorGUILayout.LabelField(SpineInspectorUtility.TempContent("Animations: " + skeletonData.Animations.Count, Icons.animation));
  110. using (new SpineInspectorUtility.IndentScope()) {
  111. bakeAnimations = EditorGUILayout.Toggle(SpineInspectorUtility.TempContent("Bake Animations", Icons.animationRoot), bakeAnimations);
  112. using (new EditorGUI.DisabledScope(!bakeAnimations)) {
  113. using (new SpineInspectorUtility.IndentScope()) {
  114. bakeIK = EditorGUILayout.Toggle(SpineInspectorUtility.TempContent("Bake IK", Icons.constraintIK), bakeIK);
  115. bakeEventOptions = (SendMessageOptions)EditorGUILayout.EnumPopup(SpineInspectorUtility.TempContent("Event Options", Icons.userEvent), bakeEventOptions);
  116. }
  117. }
  118. }
  119. }
  120. EditorGUILayout.Space();
  121. if (!string.IsNullOrEmpty(skinToBake) && UnityEngine.Event.current.type == EventType.Repaint)
  122. bakeSkin = skeletonData.FindSkin(skinToBake) ?? skeletonData.DefaultSkin;
  123. var prefabIcon = EditorGUIUtility.FindTexture("PrefabModel Icon");
  124. if (hasExtraSkins) {
  125. EditorGUI.BeginChangeCheck();
  126. EditorGUILayout.PropertyField(so.FindProperty("skinToBake"));
  127. if (EditorGUI.EndChangeCheck()) {
  128. so.ApplyModifiedProperties();
  129. Repaint();
  130. }
  131. if (SpineInspectorUtility.LargeCenteredButton(SpineInspectorUtility.TempContent(string.Format("Bake Skeleton with Skin ({0})", (bakeSkin == null ? "default" : bakeSkin.Name)), prefabIcon))) {
  132. SkeletonBaker.BakeToPrefab(skeletonDataAsset, new ExposedList<Skin>(new[] { bakeSkin }), "", bakeAnimations, bakeIK, bakeEventOptions);
  133. }
  134. if (SpineInspectorUtility.LargeCenteredButton(SpineInspectorUtility.TempContent(string.Format("Bake All ({0} skins)", skeletonData.Skins.Count), prefabIcon))) {
  135. SkeletonBaker.BakeToPrefab(skeletonDataAsset, skeletonData.Skins, "", bakeAnimations, bakeIK, bakeEventOptions);
  136. }
  137. } else {
  138. if (SpineInspectorUtility.LargeCenteredButton(SpineInspectorUtility.TempContent("Bake Skeleton", prefabIcon))) {
  139. SkeletonBaker.BakeToPrefab(skeletonDataAsset, new ExposedList<Skin>(new[] { bakeSkin }), "", bakeAnimations, bakeIK, bakeEventOptions);
  140. }
  141. }
  142. }
  143. }
  144. }