diff --git a/spine-tk2d/Assets/Spine/BoneComponent.cs b/spine-tk2d/Assets/Spine/BoneComponent.cs new file mode 100644 index 000000000..e24350b70 --- /dev/null +++ b/spine-tk2d/Assets/Spine/BoneComponent.cs @@ -0,0 +1,67 @@ +/****************************************************************************** + * Spine Runtimes Software License + * Version 2 + * + * Copyright (c) 2013, Esoteric Software + * All rights reserved. + * + * You are granted a perpetual, non-exclusive, non-sublicensable and + * non-transferable license to install, execute and perform the Spine Runtimes + * Software (the "Software") solely for internal use. Without the written + * permission of Esoteric Software, you may not (a) modify, translate, adapt or + * otherwise create derivative works, improvements of the Software or develop + * new applications using the Software or (b) remove, delete, alter or obscure + * any trademarks or any copyright, trademark, patent or other intellectual + * property or proprietary rights notices on or in the Software, including + * any copy thereof. Redistributions in binary or source form must include + * this license and terms. THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTARE BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *****************************************************************************/ + +using System; +using System.IO; +using System.Collections.Generic; +using UnityEngine; +using Spine; + +/// Sets a GameObject's transform to match a bone on a Spine skeleton. +[ExecuteInEditMode] +[AddComponentMenu("Spine/BoneComponent")] +public class BoneComponent : MonoBehaviour { + public SkeletonComponent skeletonComponent; + public Bone bone; + + /// If a bone isn't set, boneName is used to find the bone. + public String boneName; + + public void LateUpdate () { + if (skeletonComponent == null) return; + if (bone == null) { + if (boneName == null) return; + bone = skeletonComponent.skeleton.FindBone(boneName); + if (bone == null) { + Debug.Log("Bone not found: " + boneName, this); + return; + } + } + if (transform.parent == skeletonComponent.transform) { + transform.localPosition = new Vector3(bone.worldX, bone.worldY, transform.localPosition.z); + Vector3 rotation = transform.localRotation.eulerAngles; + transform.localRotation = Quaternion.Euler(rotation.x, rotation.y, bone.worldRotation); + } else { + // Best effort to set this GameObject's transform when it isn't a child of the SkeletonComponent. + transform.position = skeletonComponent.transform.TransformPoint(new Vector3(bone.worldX, bone.worldY, transform.position.z)); + Vector3 rotation = skeletonComponent.transform.rotation.eulerAngles; + transform.rotation = Quaternion.Euler(rotation.x, rotation.y, + skeletonComponent.transform.rotation.eulerAngles.z + bone.worldRotation); + } + } +} \ No newline at end of file diff --git a/spine-tk2d/Assets/Spine/BoneComponent.cs.meta b/spine-tk2d/Assets/Spine/BoneComponent.cs.meta new file mode 100644 index 000000000..f2c537209 --- /dev/null +++ b/spine-tk2d/Assets/Spine/BoneComponent.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d08aa5caf52eae44ba81cc81ca1dad5d +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/spine-tk2d/Assets/Spine/Editor/BoneComponentInspector.cs b/spine-tk2d/Assets/Spine/Editor/BoneComponentInspector.cs new file mode 100644 index 000000000..c30ff1ace --- /dev/null +++ b/spine-tk2d/Assets/Spine/Editor/BoneComponentInspector.cs @@ -0,0 +1,72 @@ +/****************************************************************************** + * Spine Runtimes Software License + * Version 2 + * + * Copyright (c) 2013, Esoteric Software + * All rights reserved. + * + * You are granted a perpetual, non-exclusive, non-sublicensable and + * non-transferable license to install, execute and perform the Spine Runtimes + * Software (the "Software") solely for internal use. Without the written + * permission of Esoteric Software, you may not (a) modify, translate, adapt or + * otherwise create derivative works, improvements of the Software or develop + * new applications using the Software or (b) remove, delete, alter or obscure + * any trademarks or any copyright, trademark, patent or other intellectual + * property or proprietary rights notices on or in the Software, including + * any copy thereof. Redistributions in binary or source form must include + * this license and terms. THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTARE BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *****************************************************************************/ + +using System; +using UnityEditor; +using UnityEngine; + +[CustomEditor(typeof(BoneComponent))] +public class BoneComponentInspector : Editor { + private SerializedProperty boneName, skeletonComponent; + + void OnEnable () { + skeletonComponent = serializedObject.FindProperty("skeletonComponent"); + boneName = serializedObject.FindProperty("boneName"); + } + + override public void OnInspectorGUI () { + serializedObject.Update(); + BoneComponent component = (BoneComponent)target; + + EditorGUILayout.PropertyField(skeletonComponent); + + if (component.skeletonComponent != null) { + String[] bones = new String[component.skeletonComponent.skeleton.Data.Bones.Count + 1]; + bones[0] = ""; + for (int i = 0; i < bones.Length - 1; i++) + bones[i + 1] = component.skeletonComponent.skeleton.Data.Bones[i].Name; + Array.Sort(bones); + int boneIndex = Math.Max(0, Array.IndexOf(bones, boneName.stringValue)); + + EditorGUILayout.BeginHorizontal(); + EditorGUILayout.LabelField("Bone"); + EditorGUIUtility.LookLikeControls(); + boneIndex = EditorGUILayout.Popup(boneIndex, bones); + EditorGUILayout.EndHorizontal(); + + boneName.stringValue = bones[boneIndex];; + } + + if (serializedObject.ApplyModifiedProperties() || + (Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed") + ) { + component.bone = null; + component.LateUpdate(); + } + } +} diff --git a/spine-tk2d/Assets/Spine/Editor/BoneComponentInspector.cs.meta b/spine-tk2d/Assets/Spine/Editor/BoneComponentInspector.cs.meta new file mode 100644 index 000000000..7a90e80a3 --- /dev/null +++ b/spine-tk2d/Assets/Spine/Editor/BoneComponentInspector.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 99e3e8311529bbc48a5c3a5b9714e162 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/spine-tk2d/Assets/Spine/Editor/Menus.cs b/spine-tk2d/Assets/Spine/Editor/Menus.cs index 2737e1f9b..fdd3d26c5 100644 --- a/spine-tk2d/Assets/Spine/Editor/Menus.cs +++ b/spine-tk2d/Assets/Spine/Editor/Menus.cs @@ -64,26 +64,4 @@ public class Menus { EditorUtility.FocusProjectWindow(); Selection.activeObject = gameObject; } - - [MenuItem("Component/Spine SkeletonComponent")] - static public void CreateSkeletonComponent () { - Selection.activeGameObject.AddComponent(typeof(SkeletonComponent)); - } - - [MenuItem("Component/Spine SkeletonAnimation")] - static public void CreateSkeletonAnimation () { - Selection.activeGameObject.AddComponent(typeof(SkeletonAnimation)); - } - - [MenuItem("Component/Spine SkeletonComponent", true)] - static public bool ValidateCreateSkeletonComponent () { - return Selection.activeGameObject != null - && Selection.activeGameObject.GetComponent(typeof(SkeletonComponent)) == null - && Selection.activeGameObject.GetComponent(typeof(SkeletonAnimation)) == null; - } - - [MenuItem("Component/Spine SkeletonAnimation", true)] - static public bool ValidateCreateSkeletonAnimation () { - return ValidateCreateSkeletonComponent(); - } } diff --git a/spine-tk2d/Assets/Spine/SkeletonAnimation.cs b/spine-tk2d/Assets/Spine/SkeletonAnimation.cs index 5c9cd68f6..e7376fa7b 100644 --- a/spine-tk2d/Assets/Spine/SkeletonAnimation.cs +++ b/spine-tk2d/Assets/Spine/SkeletonAnimation.cs @@ -34,6 +34,7 @@ using Spine; /** Extends SkeletonComponent to apply an animation. */ [ExecuteInEditMode, RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] +[AddComponentMenu("Spine/SkeletonAnimation")] public class SkeletonAnimation : SkeletonComponent { public bool loop; public Spine.AnimationState state; diff --git a/spine-tk2d/Assets/Spine/SkeletonComponent.cs b/spine-tk2d/Assets/Spine/SkeletonComponent.cs index 5868419f1..8813c138c 100644 --- a/spine-tk2d/Assets/Spine/SkeletonComponent.cs +++ b/spine-tk2d/Assets/Spine/SkeletonComponent.cs @@ -34,6 +34,7 @@ using Spine; /** Renders a skeleton. Extend to apply animations, get bones and manipulate them, etc. */ [ExecuteInEditMode, RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] +[AddComponentMenu("Spine/SkeletonComponent")] public class SkeletonComponent : MonoBehaviour { public SkeletonDataAsset skeletonDataAsset; public Skeleton skeleton; diff --git a/spine-tk2d/Assets/examples/spineboy/spineboy.unity b/spine-tk2d/Assets/examples/spineboy/spineboy.unity index 3313c26fa..6bf827569 100644 Binary files a/spine-tk2d/Assets/examples/spineboy/spineboy.unity and b/spine-tk2d/Assets/examples/spineboy/spineboy.unity differ diff --git a/spine-unity/Assets/Spine/BoneComponent.cs b/spine-unity/Assets/Spine/BoneComponent.cs new file mode 100644 index 000000000..e24350b70 --- /dev/null +++ b/spine-unity/Assets/Spine/BoneComponent.cs @@ -0,0 +1,67 @@ +/****************************************************************************** + * Spine Runtimes Software License + * Version 2 + * + * Copyright (c) 2013, Esoteric Software + * All rights reserved. + * + * You are granted a perpetual, non-exclusive, non-sublicensable and + * non-transferable license to install, execute and perform the Spine Runtimes + * Software (the "Software") solely for internal use. Without the written + * permission of Esoteric Software, you may not (a) modify, translate, adapt or + * otherwise create derivative works, improvements of the Software or develop + * new applications using the Software or (b) remove, delete, alter or obscure + * any trademarks or any copyright, trademark, patent or other intellectual + * property or proprietary rights notices on or in the Software, including + * any copy thereof. Redistributions in binary or source form must include + * this license and terms. THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTARE BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *****************************************************************************/ + +using System; +using System.IO; +using System.Collections.Generic; +using UnityEngine; +using Spine; + +/// Sets a GameObject's transform to match a bone on a Spine skeleton. +[ExecuteInEditMode] +[AddComponentMenu("Spine/BoneComponent")] +public class BoneComponent : MonoBehaviour { + public SkeletonComponent skeletonComponent; + public Bone bone; + + /// If a bone isn't set, boneName is used to find the bone. + public String boneName; + + public void LateUpdate () { + if (skeletonComponent == null) return; + if (bone == null) { + if (boneName == null) return; + bone = skeletonComponent.skeleton.FindBone(boneName); + if (bone == null) { + Debug.Log("Bone not found: " + boneName, this); + return; + } + } + if (transform.parent == skeletonComponent.transform) { + transform.localPosition = new Vector3(bone.worldX, bone.worldY, transform.localPosition.z); + Vector3 rotation = transform.localRotation.eulerAngles; + transform.localRotation = Quaternion.Euler(rotation.x, rotation.y, bone.worldRotation); + } else { + // Best effort to set this GameObject's transform when it isn't a child of the SkeletonComponent. + transform.position = skeletonComponent.transform.TransformPoint(new Vector3(bone.worldX, bone.worldY, transform.position.z)); + Vector3 rotation = skeletonComponent.transform.rotation.eulerAngles; + transform.rotation = Quaternion.Euler(rotation.x, rotation.y, + skeletonComponent.transform.rotation.eulerAngles.z + bone.worldRotation); + } + } +} \ No newline at end of file diff --git a/spine-unity/Assets/Spine/BoneComponent.cs.meta b/spine-unity/Assets/Spine/BoneComponent.cs.meta new file mode 100644 index 000000000..a6a16dd66 --- /dev/null +++ b/spine-unity/Assets/Spine/BoneComponent.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 487b42efe96d8cc408a757541ea3f169 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/spine-unity/Assets/Spine/Editor/BoneComponentInspector.cs b/spine-unity/Assets/Spine/Editor/BoneComponentInspector.cs new file mode 100644 index 000000000..c30ff1ace --- /dev/null +++ b/spine-unity/Assets/Spine/Editor/BoneComponentInspector.cs @@ -0,0 +1,72 @@ +/****************************************************************************** + * Spine Runtimes Software License + * Version 2 + * + * Copyright (c) 2013, Esoteric Software + * All rights reserved. + * + * You are granted a perpetual, non-exclusive, non-sublicensable and + * non-transferable license to install, execute and perform the Spine Runtimes + * Software (the "Software") solely for internal use. Without the written + * permission of Esoteric Software, you may not (a) modify, translate, adapt or + * otherwise create derivative works, improvements of the Software or develop + * new applications using the Software or (b) remove, delete, alter or obscure + * any trademarks or any copyright, trademark, patent or other intellectual + * property or proprietary rights notices on or in the Software, including + * any copy thereof. Redistributions in binary or source form must include + * this license and terms. THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTARE BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *****************************************************************************/ + +using System; +using UnityEditor; +using UnityEngine; + +[CustomEditor(typeof(BoneComponent))] +public class BoneComponentInspector : Editor { + private SerializedProperty boneName, skeletonComponent; + + void OnEnable () { + skeletonComponent = serializedObject.FindProperty("skeletonComponent"); + boneName = serializedObject.FindProperty("boneName"); + } + + override public void OnInspectorGUI () { + serializedObject.Update(); + BoneComponent component = (BoneComponent)target; + + EditorGUILayout.PropertyField(skeletonComponent); + + if (component.skeletonComponent != null) { + String[] bones = new String[component.skeletonComponent.skeleton.Data.Bones.Count + 1]; + bones[0] = ""; + for (int i = 0; i < bones.Length - 1; i++) + bones[i + 1] = component.skeletonComponent.skeleton.Data.Bones[i].Name; + Array.Sort(bones); + int boneIndex = Math.Max(0, Array.IndexOf(bones, boneName.stringValue)); + + EditorGUILayout.BeginHorizontal(); + EditorGUILayout.LabelField("Bone"); + EditorGUIUtility.LookLikeControls(); + boneIndex = EditorGUILayout.Popup(boneIndex, bones); + EditorGUILayout.EndHorizontal(); + + boneName.stringValue = bones[boneIndex];; + } + + if (serializedObject.ApplyModifiedProperties() || + (Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed") + ) { + component.bone = null; + component.LateUpdate(); + } + } +} diff --git a/spine-unity/Assets/Spine/Editor/BoneComponentInspector.cs.meta b/spine-unity/Assets/Spine/Editor/BoneComponentInspector.cs.meta new file mode 100644 index 000000000..1b03fab20 --- /dev/null +++ b/spine-unity/Assets/Spine/Editor/BoneComponentInspector.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: dca21b77745bb4245a4e2e012c282ce6 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/spine-unity/Assets/Spine/Editor/Menus.cs b/spine-unity/Assets/Spine/Editor/Menus.cs index 8d86a9c36..dcacb74aa 100644 --- a/spine-unity/Assets/Spine/Editor/Menus.cs +++ b/spine-unity/Assets/Spine/Editor/Menus.cs @@ -69,26 +69,4 @@ public class SpineEditor { EditorUtility.FocusProjectWindow(); Selection.activeObject = gameObject; } - - [MenuItem("Component/Spine SkeletonComponent")] - static public void CreateSkeletonComponent () { - Selection.activeGameObject.AddComponent(typeof(SkeletonComponent)); - } - - [MenuItem("Component/Spine SkeletonAnimation")] - static public void CreateSkeletonAnimation () { - Selection.activeGameObject.AddComponent(typeof(SkeletonAnimation)); - } - - [MenuItem("Component/Spine SkeletonComponent", true)] - static public bool ValidateCreateSkeletonComponent () { - return Selection.activeGameObject != null - && Selection.activeGameObject.GetComponent(typeof(SkeletonComponent)) == null - && Selection.activeGameObject.GetComponent(typeof(SkeletonAnimation)) == null; - } - - [MenuItem("Component/Spine SkeletonAnimation", true)] - static public bool ValidateCreateSkeletonAnimation () { - return ValidateCreateSkeletonComponent(); - } } diff --git a/spine-unity/Assets/Spine/SkeletonAnimation.cs b/spine-unity/Assets/Spine/SkeletonAnimation.cs index fb5971fcb..64d82b75f 100644 --- a/spine-unity/Assets/Spine/SkeletonAnimation.cs +++ b/spine-unity/Assets/Spine/SkeletonAnimation.cs @@ -34,6 +34,7 @@ using Spine; /** Extends SkeletonComponent to apply an animation. */ [ExecuteInEditMode, RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] +[AddComponentMenu("Spine/SkeletonAnimation")] public class SkeletonAnimation : SkeletonComponent { public bool loop; public Spine.AnimationState state; diff --git a/spine-unity/Assets/Spine/SkeletonComponent.cs b/spine-unity/Assets/Spine/SkeletonComponent.cs index fe27dceb5..0c7cc4ada 100644 --- a/spine-unity/Assets/Spine/SkeletonComponent.cs +++ b/spine-unity/Assets/Spine/SkeletonComponent.cs @@ -34,6 +34,7 @@ using Spine; /** Renders a skeleton. Extend to apply animations, get bones and manipulate them, etc. */ [ExecuteInEditMode, RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] +[AddComponentMenu("Spine/SkeletonComponent")] public class SkeletonComponent : MonoBehaviour { public SkeletonDataAsset skeletonDataAsset; public Skeleton skeleton; diff --git a/spine-unity/Assets/examples/spineboy/spineboy.unity b/spine-unity/Assets/examples/spineboy/spineboy.unity index 49501911a..0eef947c9 100644 Binary files a/spine-unity/Assets/examples/spineboy/spineboy.unity and b/spine-unity/Assets/examples/spineboy/spineboy.unity differ