Added BoneComponent.

closes #166
This commit is contained in:
NathanSweet 2014-02-05 20:23:34 +01:00
parent 86093e7d0d
commit 38e54e686d
16 changed files with 314 additions and 44 deletions

View File

@ -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;
/// <summary>Sets a GameObject's transform to match a bone on a Spine skeleton.</summary>
[ExecuteInEditMode]
[AddComponentMenu("Spine/BoneComponent")]
public class BoneComponent : MonoBehaviour {
public SkeletonComponent skeletonComponent;
public Bone bone;
/// <summary>If a bone isn't set, boneName is used to find the bone.</summary>
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);
}
}
}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d08aa5caf52eae44ba81cc81ca1dad5d
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

View File

@ -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] = "<None>";
for (int i = 0; i < bones.Length - 1; i++)
bones[i + 1] = component.skeletonComponent.skeleton.Data.Bones[i].Name;
Array.Sort<String>(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();
}
}
}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 99e3e8311529bbc48a5c3a5b9714e162
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

View File

@ -64,26 +64,4 @@ public class Menus {
EditorUtility.FocusProjectWindow(); EditorUtility.FocusProjectWindow();
Selection.activeObject = gameObject; 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();
}
} }

View File

@ -34,6 +34,7 @@ using Spine;
/** Extends SkeletonComponent to apply an animation. */ /** Extends SkeletonComponent to apply an animation. */
[ExecuteInEditMode, RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] [ExecuteInEditMode, RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
[AddComponentMenu("Spine/SkeletonAnimation")]
public class SkeletonAnimation : SkeletonComponent { public class SkeletonAnimation : SkeletonComponent {
public bool loop; public bool loop;
public Spine.AnimationState state; public Spine.AnimationState state;

View File

@ -34,6 +34,7 @@ using Spine;
/** Renders a skeleton. Extend to apply animations, get bones and manipulate them, etc. */ /** Renders a skeleton. Extend to apply animations, get bones and manipulate them, etc. */
[ExecuteInEditMode, RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] [ExecuteInEditMode, RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
[AddComponentMenu("Spine/SkeletonComponent")]
public class SkeletonComponent : MonoBehaviour { public class SkeletonComponent : MonoBehaviour {
public SkeletonDataAsset skeletonDataAsset; public SkeletonDataAsset skeletonDataAsset;
public Skeleton skeleton; public Skeleton skeleton;

View File

@ -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;
/// <summary>Sets a GameObject's transform to match a bone on a Spine skeleton.</summary>
[ExecuteInEditMode]
[AddComponentMenu("Spine/BoneComponent")]
public class BoneComponent : MonoBehaviour {
public SkeletonComponent skeletonComponent;
public Bone bone;
/// <summary>If a bone isn't set, boneName is used to find the bone.</summary>
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);
}
}
}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 487b42efe96d8cc408a757541ea3f169
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

View File

@ -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] = "<None>";
for (int i = 0; i < bones.Length - 1; i++)
bones[i + 1] = component.skeletonComponent.skeleton.Data.Bones[i].Name;
Array.Sort<String>(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();
}
}
}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: dca21b77745bb4245a4e2e012c282ce6
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

View File

@ -69,26 +69,4 @@ public class SpineEditor {
EditorUtility.FocusProjectWindow(); EditorUtility.FocusProjectWindow();
Selection.activeObject = gameObject; 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();
}
} }

View File

@ -34,6 +34,7 @@ using Spine;
/** Extends SkeletonComponent to apply an animation. */ /** Extends SkeletonComponent to apply an animation. */
[ExecuteInEditMode, RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] [ExecuteInEditMode, RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
[AddComponentMenu("Spine/SkeletonAnimation")]
public class SkeletonAnimation : SkeletonComponent { public class SkeletonAnimation : SkeletonComponent {
public bool loop; public bool loop;
public Spine.AnimationState state; public Spine.AnimationState state;

View File

@ -34,6 +34,7 @@ using Spine;
/** Renders a skeleton. Extend to apply animations, get bones and manipulate them, etc. */ /** Renders a skeleton. Extend to apply animations, get bones and manipulate them, etc. */
[ExecuteInEditMode, RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] [ExecuteInEditMode, RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
[AddComponentMenu("Spine/SkeletonComponent")]
public class SkeletonComponent : MonoBehaviour { public class SkeletonComponent : MonoBehaviour {
public SkeletonDataAsset skeletonDataAsset; public SkeletonDataAsset skeletonDataAsset;
public Skeleton skeleton; public Skeleton skeleton;