[unity] Assorted cleanup.

This commit is contained in:
pharan 2018-10-08 04:55:47 +08:00
parent 230ae89d66
commit 910e88bbb3
13 changed files with 126 additions and 139 deletions

View File

@ -341,12 +341,6 @@ namespace Spine.Unity.Editor {
public SerializedSortingProperties (Renderer r) : this(new SerializedObject(r)) {} public SerializedSortingProperties (Renderer r) : this(new SerializedObject(r)) {}
public SerializedSortingProperties (Object[] renderers) : this(new SerializedObject(renderers)) {} public SerializedSortingProperties (Object[] renderers) : this(new SerializedObject(renderers)) {}
/// <summary>
/// Initializes a new instance of the
/// <see cref="Spine.Unity.Editor.SpineInspectorUtility.SerializedSortingProperties"/> struct.
/// </summary>
/// <param name="rendererSerializedObject">SerializedObject of the renderer. Use
/// <see cref="Spine.Unity.Editor.SpineInspectorUtility.GetRenderersSerializedObject"/> to easily generate this.</param>
public SerializedSortingProperties (SerializedObject rendererSerializedObject) { public SerializedSortingProperties (SerializedObject rendererSerializedObject) {
renderer = rendererSerializedObject; renderer = rendererSerializedObject;
sortingLayerID = renderer.FindProperty("m_SortingLayerID"); sortingLayerID = renderer.FindProperty("m_SortingLayerID");

View File

@ -53,7 +53,6 @@ namespace Spine.Unity.Editor {
bool canCreateHingeChain = false; bool canCreateHingeChain = false;
Dictionary<Slot, List<BoundingBoxAttachment>> boundingBoxTable = new Dictionary<Slot, List<BoundingBoxAttachment>>(); Dictionary<Slot, List<BoundingBoxAttachment>> boundingBoxTable = new Dictionary<Slot, List<BoundingBoxAttachment>>();
//string currentSkinName = "";
void OnEnable () { void OnEnable () {
mode = this.serializedObject.FindProperty("mode"); mode = this.serializedObject.FindProperty("mode");
@ -81,7 +80,6 @@ namespace Spine.Unity.Editor {
if (skeleton.Skin == null) if (skeleton.Skin == null)
skin = skeleton.Data.DefaultSkin; skin = skeleton.Data.DefaultSkin;
//currentSkinName = skin.Name;
for(int i = 0; i < slotCount; i++){ for(int i = 0; i < slotCount; i++){
Slot slot = skeletonUtility.skeletonRenderer.skeleton.Slots.Items[i]; Slot slot = skeletonUtility.skeletonRenderer.skeleton.Slots.Items[i];
if (slot.Bone == utilityBone.bone) { if (slot.Bone == utilityBone.bone) {
@ -103,7 +101,7 @@ namespace Spine.Unity.Editor {
void EvaluateFlags () { void EvaluateFlags () {
utilityBone = (SkeletonUtilityBone)target; utilityBone = (SkeletonUtilityBone)target;
skeletonUtility = utilityBone.skeletonUtility; skeletonUtility = utilityBone.hierarchy;
if (Selection.objects.Length == 1) { if (Selection.objects.Length == 1) {
containsFollows = utilityBone.mode == SkeletonUtilityBone.Mode.Follow; containsFollows = utilityBone.mode == SkeletonUtilityBone.Mode.Follow;
@ -146,7 +144,7 @@ namespace Spine.Unity.Editor {
using (new GUILayout.HorizontalScope()) { using (new GUILayout.HorizontalScope()) {
EditorGUILayout.PrefixLabel("Bone"); EditorGUILayout.PrefixLabel("Bone");
if (GUILayout.Button(str, EditorStyles.popup)) { if (GUILayout.Button(str, EditorStyles.popup)) {
BoneSelectorContextMenu(str, ((SkeletonUtilityBone)target).skeletonUtility.skeletonRenderer.skeleton.Bones, "<None>", TargetBoneSelected); BoneSelectorContextMenu(str, ((SkeletonUtilityBone)target).hierarchy.skeletonRenderer.skeleton.Bones, "<None>", TargetBoneSelected);
} }
} }
} }
@ -166,7 +164,7 @@ namespace Spine.Unity.Editor {
using (new GUILayout.HorizontalScope()) { using (new GUILayout.HorizontalScope()) {
EditorGUILayout.Space(); EditorGUILayout.Space();
using (new EditorGUI.DisabledGroupScope(multiObject || !utilityBone.valid || utilityBone.bone == null || utilityBone.bone.Children.Count == 0)) { using (new EditorGUI.DisabledGroupScope(multiObject || !utilityBone.valid || utilityBone.bone == null || utilityBone.bone.Children.Count == 0)) {
if (GUILayout.Button(SpineInspectorUtility.TempContent("Add Child", Icons.bone), GUILayout.MinWidth(120), GUILayout.Height(24))) if (GUILayout.Button(SpineInspectorUtility.TempContent("Add Child Bone", Icons.bone), GUILayout.MinWidth(120), GUILayout.Height(24)))
BoneSelectorContextMenu("", utilityBone.bone.Children, "<Recursively>", SpawnChildBoneSelected); BoneSelectorContextMenu("", utilityBone.bone.Children, "<Recursively>", SpawnChildBoneSelected);
} }
using (new EditorGUI.DisabledGroupScope(multiObject || !utilityBone.valid || utilityBone.bone == null || containsOverrides)) { using (new EditorGUI.DisabledGroupScope(multiObject || !utilityBone.valid || utilityBone.bone == null || containsOverrides)) {
@ -260,12 +258,12 @@ namespace Spine.Unity.Editor {
GameObject go = skeletonUtility.SpawnBoneRecursively(bone, utilityBone.transform, utilityBone.mode, utilityBone.position, utilityBone.rotation, utilityBone.scale); GameObject go = skeletonUtility.SpawnBoneRecursively(bone, utilityBone.transform, utilityBone.mode, utilityBone.position, utilityBone.rotation, utilityBone.scale);
SkeletonUtilityBone[] newUtilityBones = go.GetComponentsInChildren<SkeletonUtilityBone>(); SkeletonUtilityBone[] newUtilityBones = go.GetComponentsInChildren<SkeletonUtilityBone>();
foreach (SkeletonUtilityBone utilBone in newUtilityBones) foreach (SkeletonUtilityBone utilBone in newUtilityBones)
SkeletonUtilityInspector.AttachIcon(utilBone); SkeletonGameObjectsInspector.AttachIcon(utilBone);
} }
} else { } else {
var bone = (Bone)obj; var bone = (Bone)obj;
GameObject go = skeletonUtility.SpawnBone(bone, utilityBone.transform, utilityBone.mode, utilityBone.position, utilityBone.rotation, utilityBone.scale); GameObject go = skeletonUtility.SpawnBone(bone, utilityBone.transform, utilityBone.mode, utilityBone.position, utilityBone.rotation, utilityBone.scale);
SkeletonUtilityInspector.AttachIcon(go.GetComponent<SkeletonUtilityBone>()); SkeletonGameObjectsInspector.AttachIcon(go.GetComponent<SkeletonUtilityBone>());
Selection.activeGameObject = go; Selection.activeGameObject = go;
EditorGUIUtility.PingObject(go); EditorGUIUtility.PingObject(go);
} }
@ -274,7 +272,7 @@ namespace Spine.Unity.Editor {
void SpawnOverride () { void SpawnOverride () {
GameObject go = skeletonUtility.SpawnBone(utilityBone.bone, utilityBone.transform.parent, SkeletonUtilityBone.Mode.Override, utilityBone.position, utilityBone.rotation, utilityBone.scale); GameObject go = skeletonUtility.SpawnBone(utilityBone.bone, utilityBone.transform.parent, SkeletonUtilityBone.Mode.Override, utilityBone.position, utilityBone.rotation, utilityBone.scale);
go.name = go.name + " [Override]"; go.name = go.name + " [Override]";
SkeletonUtilityInspector.AttachIcon(go.GetComponent<SkeletonUtilityBone>()); SkeletonGameObjectsInspector.AttachIcon(go.GetComponent<SkeletonUtilityBone>());
Selection.activeGameObject = go; Selection.activeGameObject = go;
EditorGUIUtility.PingObject(go); EditorGUIUtility.PingObject(go);
} }

View File

@ -41,18 +41,17 @@ namespace Spine.Unity.Editor {
using Icons = SpineEditorUtilities.Icons; using Icons = SpineEditorUtilities.Icons;
[CustomEditor(typeof(SkeletonUtility))] [CustomEditor(typeof(SkeletonUtility))]
public class SkeletonUtilityInspector : UnityEditor.Editor { public class SkeletonGameObjectsInspector : UnityEditor.Editor {
SkeletonUtility skeletonUtility; SkeletonUtility skeletonGameObjects;
Skeleton skeleton; Skeleton skeleton;
SkeletonRenderer skeletonRenderer; SkeletonRenderer skeletonRenderer;
bool isPrefab; bool isPrefab;
readonly GUIContent SpawnHierarchyButtonLabel = new GUIContent("Spawn Hierarchy", Icons.skeleton);
GUIContent SpawnHierarchyButtonLabel = new GUIContent("Spawn Hierarchy", Icons.skeleton);
void OnEnable () { void OnEnable () {
skeletonUtility = (SkeletonUtility)target; skeletonGameObjects = (SkeletonUtility)target;
skeletonRenderer = skeletonUtility.GetComponent<SkeletonRenderer>(); skeletonRenderer = skeletonGameObjects.GetComponent<SkeletonRenderer>();
skeleton = skeletonRenderer.Skeleton; skeleton = skeletonRenderer.Skeleton;
if (skeleton == null) { if (skeleton == null) {
@ -77,9 +76,13 @@ namespace Spine.Unity.Editor {
return; return;
} }
skeletonUtility.boneRoot = (Transform)EditorGUILayout.ObjectField("Bone Root", skeletonUtility.boneRoot, typeof(Transform), true); EditorGUILayout.PropertyField(serializedObject.FindProperty("boneRoot"), SpineInspectorUtility.TempContent("Skeleton Root"));
bool hasRootBone = skeletonGameObjects.boneRoot != null;
if (!hasRootBone)
EditorGUILayout.HelpBox("No hierarchy found. Use Spawn Hierarchy to generate GameObjects for bones.", MessageType.Info);
bool hasRootBone = skeletonUtility.boneRoot != null;
using (new EditorGUI.DisabledGroupScope(hasRootBone)) { using (new EditorGUI.DisabledGroupScope(hasRootBone)) {
if (SpineInspectorUtility.LargeCenteredButton(SpawnHierarchyButtonLabel)) if (SpineInspectorUtility.LargeCenteredButton(SpawnHierarchyButtonLabel))
SpawnHierarchyContextMenu(); SpawnHierarchyContextMenu();
@ -87,37 +90,37 @@ namespace Spine.Unity.Editor {
if (hasRootBone) { if (hasRootBone) {
if (SpineInspectorUtility.CenteredButton(new GUIContent("Remove Hierarchy"))) { if (SpineInspectorUtility.CenteredButton(new GUIContent("Remove Hierarchy"))) {
Undo.RegisterCompleteObjectUndo(skeletonUtility, "Remove Hierarchy"); Undo.RegisterCompleteObjectUndo(skeletonGameObjects, "Remove Hierarchy");
Undo.DestroyObjectImmediate(skeletonUtility.boneRoot.gameObject); Undo.DestroyObjectImmediate(skeletonGameObjects.boneRoot.gameObject);
skeletonUtility.boneRoot = null; skeletonGameObjects.boneRoot = null;
} }
} }
} }
void SpawnHierarchyContextMenu () { void SpawnHierarchyContextMenu () {
GenericMenu menu = new GenericMenu(); var menu = new GenericMenu();
menu.AddItem(new GUIContent("Follow"), false, SpawnFollowHierarchy); menu.AddItem(new GUIContent("Follow all bones"), false, SpawnFollowHierarchy);
menu.AddItem(new GUIContent("Follow (Root Only)"), false, SpawnFollowHierarchyRootOnly); menu.AddItem(new GUIContent("Follow (Root Only)"), false, SpawnFollowHierarchyRootOnly);
menu.AddSeparator(""); menu.AddSeparator("");
menu.AddItem(new GUIContent("Override"), false, SpawnOverrideHierarchy); menu.AddItem(new GUIContent("Override all bones"), false, SpawnOverrideHierarchy);
menu.AddItem(new GUIContent("Override (Root Only)"), false, SpawnOverrideHierarchyRootOnly); menu.AddItem(new GUIContent("Override (Root Only)"), false, SpawnOverrideHierarchyRootOnly);
menu.ShowAsContext(); menu.ShowAsContext();
} }
public static void AttachIcon (SkeletonUtilityBone utilityBone) { public static void AttachIcon (SkeletonUtilityBone boneComponent) {
Skeleton skeleton = utilityBone.skeletonUtility.skeletonRenderer.skeleton; Skeleton skeleton = boneComponent.hierarchy.skeletonRenderer.skeleton;
Texture2D icon = utilityBone.bone.Data.Length == 0 ? Icons.nullBone : Icons.boneNib; Texture2D icon = boneComponent.bone.Data.Length == 0 ? Icons.nullBone : Icons.boneNib;
foreach (IkConstraint c in skeleton.IkConstraints) foreach (IkConstraint c in skeleton.IkConstraints)
if (c.Target == utilityBone.bone) { if (c.Target == boneComponent.bone) {
icon = Icons.constraintNib; icon = Icons.constraintNib;
break; break;
} }
typeof(EditorGUIUtility).InvokeMember("SetIconForObject", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic, null, null, new object[2] { typeof(EditorGUIUtility).InvokeMember("SetIconForObject", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic, null, null, new object[2] {
utilityBone.gameObject, boneComponent.gameObject,
icon icon
}); });
} }
@ -131,23 +134,23 @@ namespace Spine.Unity.Editor {
} }
void SpawnFollowHierarchy () { void SpawnFollowHierarchy () {
Selection.activeGameObject = skeletonUtility.SpawnHierarchy(SkeletonUtilityBone.Mode.Follow, true, true, true); Selection.activeGameObject = skeletonGameObjects.SpawnHierarchy(SkeletonUtilityBone.Mode.Follow, true, true, true);
AttachIconsToChildren(skeletonUtility.boneRoot); AttachIconsToChildren(skeletonGameObjects.boneRoot);
} }
void SpawnFollowHierarchyRootOnly () { void SpawnFollowHierarchyRootOnly () {
Selection.activeGameObject = skeletonUtility.SpawnRoot(SkeletonUtilityBone.Mode.Follow, true, true, true); Selection.activeGameObject = skeletonGameObjects.SpawnRoot(SkeletonUtilityBone.Mode.Follow, true, true, true);
AttachIconsToChildren(skeletonUtility.boneRoot); AttachIconsToChildren(skeletonGameObjects.boneRoot);
} }
void SpawnOverrideHierarchy () { void SpawnOverrideHierarchy () {
Selection.activeGameObject = skeletonUtility.SpawnHierarchy(SkeletonUtilityBone.Mode.Override, true, true, true); Selection.activeGameObject = skeletonGameObjects.SpawnHierarchy(SkeletonUtilityBone.Mode.Override, true, true, true);
AttachIconsToChildren(skeletonUtility.boneRoot); AttachIconsToChildren(skeletonGameObjects.boneRoot);
} }
void SpawnOverrideHierarchyRootOnly () { void SpawnOverrideHierarchyRootOnly () {
Selection.activeGameObject = skeletonUtility.SpawnRoot(SkeletonUtilityBone.Mode.Override, true, true, true); Selection.activeGameObject = skeletonGameObjects.SpawnRoot(SkeletonUtilityBone.Mode.Override, true, true, true);
AttachIconsToChildren(skeletonUtility.boneRoot); AttachIconsToChildren(skeletonGameObjects.boneRoot);
} }
} }

View File

@ -1,8 +1,18 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: f1b3b4b945939a54ea0b23d3396115fb guid: f1b3b4b945939a54ea0b23d3396115fb
timeCreated: 1536403985
licenseType: Pro
MonoImporter: MonoImporter:
serializedVersion: 2 serializedVersion: 2
defaultReferences: [] defaultReferences:
- multiplyMaterialTemplate: {fileID: 2100000, guid: 53bf0ab317d032d418cf1252d68f51df,
type: 2}
- screenMaterialTemplate: {fileID: 2100000, guid: 73f0f46d3177c614baf0fa48d646a9be,
type: 2}
- additiveMaterialTemplate: {fileID: 2100000, guid: 4deba332d47209e4780b3c5fcf0e3745,
type: 2}
- skeletonJSON: {instanceID: 0}
- controller: {instanceID: 0}
executionOrder: 0 executionOrder: 0
icon: {fileID: 2800000, guid: 68defdbc95b30a74a9ad396bfc9a2277, type: 3} icon: {fileID: 2800000, guid: 68defdbc95b30a74a9ad396bfc9a2277, type: 3}
userData: userData:

View File

@ -65,10 +65,8 @@ namespace Spine.Unity {
#endregion #endregion
[NonSerialized] public bool valid; [NonSerialized] public bool valid;
/// <summary>
/// The bone.
/// </summary>
[NonSerialized] public Bone bone; [NonSerialized] public Bone bone;
Transform skeletonTransform; Transform skeletonTransform;
bool skeletonTransformIsParent; bool skeletonTransformIsParent;

View File

@ -58,7 +58,6 @@ namespace Spine.Unity {
[System.NonSerialized] public readonly List<Slot> separatorSlots = new List<Slot>(); [System.NonSerialized] public readonly List<Slot> separatorSlots = new List<Slot>();
[Range(-0.1f, 0f)] public float zSpacing; [Range(-0.1f, 0f)] public float zSpacing;
//public bool renderMeshes = true;
public bool useClipping = true; public bool useClipping = true;
public bool immutableTriangles = false; public bool immutableTriangles = false;
public bool pmaVertexColors = true; public bool pmaVertexColors = true;
@ -311,7 +310,9 @@ namespace Spine.Unity {
// STEP 3. Move the mesh data into a UnityEngine.Mesh =========================================================================== // STEP 3. Move the mesh data into a UnityEngine.Mesh ===========================================================================
var currentMesh = currentSmartMesh.mesh; var currentMesh = currentSmartMesh.mesh;
meshGenerator.FillVertexData(currentMesh); meshGenerator.FillVertexData(currentMesh);
rendererBuffers.UpdateSharedMaterials(workingSubmeshInstructions); rendererBuffers.UpdateSharedMaterials(workingSubmeshInstructions);
if (updateTriangles) { // Check if the triangles should also be updated. if (updateTriangles) { // Check if the triangles should also be updated.
meshGenerator.FillTriangles(currentMesh); meshGenerator.FillTriangles(currentMesh);
meshRenderer.sharedMaterials = rendererBuffers.GetUpdatedSharedMaterialsArray(); meshRenderer.sharedMaterials = rendererBuffers.GetUpdatedSharedMaterialsArray();

View File

@ -36,8 +36,6 @@ namespace Spine.Unity {
event UpdateBonesDelegate UpdateLocal; event UpdateBonesDelegate UpdateLocal;
event UpdateBonesDelegate UpdateWorld; event UpdateBonesDelegate UpdateWorld;
event UpdateBonesDelegate UpdateComplete; event UpdateBonesDelegate UpdateComplete;
//void LateUpdate ();
Skeleton Skeleton { get; } Skeleton Skeleton { get; }
} }

View File

@ -805,6 +805,12 @@ namespace Spine.Unity.Modules.AttachmentTools {
skin.AddAttachment(slotIndex, keyName, attachment); skin.AddAttachment(slotIndex, keyName, attachment);
} }
/// <summary>Adds skin items from another skin. For items that already exist, the previous values are replaced.</summary>
public static void AddAttachments (this Skin skin, Skin otherSkin) {
if (otherSkin == null) return;
otherSkin.CopyTo(skin, true, false);
}
/// <summary>Gets an attachment from the skin for the specified slot index and name.</summary> /// <summary>Gets an attachment from the skin for the specified slot index and name.</summary>
public static Attachment GetAttachment (this Skin skin, string slotName, string keyName, Skeleton skeleton) { public static Attachment GetAttachment (this Skin skin, string slotName, string keyName, Skeleton skeleton) {
int slotIndex = skeleton.FindSlotIndex(slotName); int slotIndex = skeleton.FindSlotIndex(slotName);
@ -835,6 +841,7 @@ namespace Spine.Unity.Modules.AttachmentTools {
skin.Attachments.Clear(); skin.Attachments.Clear();
} }
//[System.Obsolete]
public static void Append (this Skin destination, Skin source) { public static void Append (this Skin destination, Skin source) {
source.CopyTo(destination, true, false); source.CopyTo(destination, true, false);
} }

View File

@ -42,9 +42,7 @@ namespace Spine.Unity.Modules {
Vector3 centerPoint; Vector3 centerPoint;
protected override void OnEnable () { protected override void OnEnable () {
if (!Application.isPlaying) if (!Application.isPlaying) return;
return;
base.OnEnable(); base.OnEnable();
Bounds centerBounds = new Bounds(eyes[0].localPosition, Vector3.zero); Bounds centerBounds = new Bounds(eyes[0].localPosition, Vector3.zero);
@ -58,19 +56,14 @@ namespace Spine.Unity.Modules {
} }
protected override void OnDisable () { protected override void OnDisable () {
if (!Application.isPlaying) if (!Application.isPlaying) return;
return;
base.OnDisable(); base.OnDisable();
} }
public override void DoUpdate () { public override void DoUpdate () {
if (target != null) targetPosition = target.position;
if (target != null)
targetPosition = target.position;
Vector3 goal = targetPosition; Vector3 goal = targetPosition;
Vector3 center = transform.TransformPoint(centerPoint); Vector3 center = transform.TransformPoint(centerPoint);
Vector3 dir = goal - center; Vector3 dir = goal - center;

View File

@ -105,8 +105,8 @@ namespace Spine.Unity.Modules {
v.y = Mathf.Clamp(v.y, Mathf.Min(lastHitY, hitY), float.MaxValue); v.y = Mathf.Clamp(v.y, Mathf.Min(lastHitY, hitY), float.MaxValue);
transform.position = v; transform.position = v;
utilBone.bone.X = transform.localPosition.x; bone.bone.X = transform.localPosition.x;
utilBone.bone.Y = transform.localPosition.y; bone.bone.Y = transform.localPosition.y;
lastHitY = hitY; lastHitY = hitY;
} }

View File

@ -37,7 +37,7 @@ using Spine;
namespace Spine.Unity { namespace Spine.Unity {
[RequireComponent(typeof(ISkeletonAnimation))] [RequireComponent(typeof(ISkeletonAnimation))]
[ExecuteInEditMode] [ExecuteInEditMode]
public class SkeletonUtility : MonoBehaviour { public sealed class SkeletonUtility : MonoBehaviour {
#region BoundingBoxAttachment #region BoundingBoxAttachment
public static PolygonCollider2D AddBoundingBoxGameObject (Skeleton skeleton, string skinName, string slotName, string attachmentName, Transform parent, bool isTrigger = true) { public static PolygonCollider2D AddBoundingBoxGameObject (Skeleton skeleton, string skinName, string slotName, string attachmentName, Transform parent, bool isTrigger = true) {
@ -122,23 +122,19 @@ namespace Spine.Unity {
void Update () { void Update () {
var skeleton = skeletonRenderer.skeleton; var skeleton = skeletonRenderer.skeleton;
if (boneRoot != null && skeleton != null) { if (skeleton != null && boneRoot != null) {
boneRoot.localScale = new Vector3(skeleton.scaleX, skeleton.scaleY, 1f); boneRoot.localScale = new Vector3(skeleton.scaleX, skeleton.scaleY, 1f);
} }
} }
[HideInInspector] [HideInInspector] public SkeletonRenderer skeletonRenderer;
public SkeletonRenderer skeletonRenderer; [HideInInspector] public ISkeletonAnimation skeletonAnimation;
[HideInInspector] [System.NonSerialized] public List<SkeletonUtilityBone> boneComponents = new List<SkeletonUtilityBone>();
public ISkeletonAnimation skeletonAnimation; [System.NonSerialized] public List<SkeletonUtilityConstraint> constraintComponents = new List<SkeletonUtilityConstraint>();
[System.NonSerialized]
public List<SkeletonUtilityBone> utilityBones = new List<SkeletonUtilityBone>();
[System.NonSerialized]
public List<SkeletonUtilityConstraint> utilityConstraints = new List<SkeletonUtilityConstraint>();
protected bool hasTransformBones; bool hasOverrideBones;
protected bool hasUtilityConstraints; bool hasConstraints;
protected bool needToReprocessBones; bool needToReprocessBones;
void OnEnable () { void OnEnable () {
if (skeletonRenderer == null) { if (skeletonRenderer == null) {
@ -176,36 +172,34 @@ namespace Spine.Unity {
} }
void HandleRendererReset (SkeletonRenderer r) { void HandleRendererReset (SkeletonRenderer r) {
if (OnReset != null) if (OnReset != null) OnReset();
OnReset();
CollectBones(); CollectBones();
} }
public void RegisterBone (SkeletonUtilityBone bone) { public void RegisterBone (SkeletonUtilityBone bone) {
if (utilityBones.Contains(bone)) if (boneComponents.Contains(bone)) {
return; return;
else { } else {
utilityBones.Add(bone); boneComponents.Add(bone);
needToReprocessBones = true; needToReprocessBones = true;
} }
} }
public void UnregisterBone (SkeletonUtilityBone bone) { public void UnregisterBone (SkeletonUtilityBone bone) {
utilityBones.Remove(bone); boneComponents.Remove(bone);
} }
public void RegisterConstraint (SkeletonUtilityConstraint constraint) { public void RegisterConstraint (SkeletonUtilityConstraint constraint) {
if (utilityConstraints.Contains(constraint)) if (constraintComponents.Contains(constraint))
return; return;
else { else {
utilityConstraints.Add(constraint); constraintComponents.Add(constraint);
needToReprocessBones = true; needToReprocessBones = true;
} }
} }
public void UnregisterConstraint (SkeletonUtilityConstraint constraint) { public void UnregisterConstraint (SkeletonUtilityConstraint constraint) {
utilityConstraints.Remove(constraint); constraintComponents.Remove(constraint);
} }
public void CollectBones () { public void CollectBones () {
@ -222,31 +216,31 @@ namespace Spine.Unity {
for (int i = 0, n = transformConstraints.Count; i < n; i++) for (int i = 0, n = transformConstraints.Count; i < n; i++)
constraintTargets.Add(transformConstraints.Items[i].target); constraintTargets.Add(transformConstraints.Items[i].target);
var utilityBones = this.utilityBones; var boneComponents = this.boneComponents;
for (int i = 0, n = utilityBones.Count; i < n; i++) { for (int i = 0, n = boneComponents.Count; i < n; i++) {
var b = utilityBones[i]; var b = boneComponents[i];
if (b.bone == null) continue; if (b.bone == null) continue;
hasTransformBones |= (b.mode == SkeletonUtilityBone.Mode.Override); hasOverrideBones |= (b.mode == SkeletonUtilityBone.Mode.Override);
hasUtilityConstraints |= constraintTargets.Contains(b.bone); hasConstraints |= constraintTargets.Contains(b.bone);
} }
hasUtilityConstraints |= utilityConstraints.Count > 0; hasConstraints |= constraintComponents.Count > 0;
if (skeletonAnimation != null) { if (skeletonAnimation != null) {
skeletonAnimation.UpdateWorld -= UpdateWorld; skeletonAnimation.UpdateWorld -= UpdateWorld;
skeletonAnimation.UpdateComplete -= UpdateComplete; skeletonAnimation.UpdateComplete -= UpdateComplete;
if (hasTransformBones || hasUtilityConstraints) if (hasOverrideBones || hasConstraints)
skeletonAnimation.UpdateWorld += UpdateWorld; skeletonAnimation.UpdateWorld += UpdateWorld;
if (hasUtilityConstraints) if (hasConstraints)
skeletonAnimation.UpdateComplete += UpdateComplete; skeletonAnimation.UpdateComplete += UpdateComplete;
} }
needToReprocessBones = false; needToReprocessBones = false;
} else { } else {
utilityBones.Clear(); boneComponents.Clear();
utilityConstraints.Clear(); constraintComponents.Clear();
} }
} }
@ -254,18 +248,18 @@ namespace Spine.Unity {
if (needToReprocessBones) if (needToReprocessBones)
CollectBones(); CollectBones();
var utilityBones = this.utilityBones; var boneComponents = this.boneComponents;
if (utilityBones == null) return; if (boneComponents == null) return;
for (int i = 0, n = utilityBones.Count; i < n; i++) for (int i = 0, n = boneComponents.Count; i < n; i++)
utilityBones[i].transformLerpComplete = false; boneComponents[i].transformLerpComplete = false;
UpdateAllBones(SkeletonUtilityBone.UpdatePhase.Local); UpdateAllBones(SkeletonUtilityBone.UpdatePhase.Local);
} }
void UpdateWorld (ISkeletonAnimation anim) { void UpdateWorld (ISkeletonAnimation anim) {
UpdateAllBones(SkeletonUtilityBone.UpdatePhase.World); UpdateAllBones(SkeletonUtilityBone.UpdatePhase.World);
for (int i = 0, n = utilityConstraints.Count; i < n; i++) for (int i = 0, n = constraintComponents.Count; i < n; i++)
utilityConstraints[i].DoUpdate(); constraintComponents[i].DoUpdate();
} }
void UpdateComplete (ISkeletonAnimation anim) { void UpdateComplete (ISkeletonAnimation anim) {
@ -276,17 +270,17 @@ namespace Spine.Unity {
if (boneRoot == null) if (boneRoot == null)
CollectBones(); CollectBones();
var utilityBones = this.utilityBones; var boneComponents = this.boneComponents;
if (utilityBones == null) return; if (boneComponents == null) return;
for (int i = 0, n = utilityBones.Count; i < n; i++) for (int i = 0, n = boneComponents.Count; i < n; i++)
utilityBones[i].DoUpdate(phase); boneComponents[i].DoUpdate(phase);
} }
public Transform GetBoneRoot () { public Transform GetBoneRoot () {
if (boneRoot != null) if (boneRoot != null)
return boneRoot; return boneRoot;
boneRoot = new GameObject("SkeletonUtility-Root").transform; boneRoot = new GameObject("SkeletonUtility-SkeletonRoot").transform;
boneRoot.parent = transform; boneRoot.parent = transform;
boneRoot.localPosition = Vector3.zero; boneRoot.localPosition = Vector3.zero;
boneRoot.localRotation = Quaternion.identity; boneRoot.localRotation = Quaternion.identity;
@ -326,10 +320,11 @@ namespace Spine.Unity {
public GameObject SpawnBone (Bone bone, Transform parent, SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca) { public GameObject SpawnBone (Bone bone, Transform parent, SkeletonUtilityBone.Mode mode, bool pos, bool rot, bool sca) {
GameObject go = new GameObject(bone.Data.Name); GameObject go = new GameObject(bone.Data.Name);
go.transform.parent = parent; var goTransform = go.transform;
goTransform.parent = parent;
SkeletonUtilityBone b = go.AddComponent<SkeletonUtilityBone>(); SkeletonUtilityBone b = go.AddComponent<SkeletonUtilityBone>();
b.skeletonUtility = this; b.hierarchy = this;
b.position = pos; b.position = pos;
b.rotation = rot; b.rotation = rot;
b.scale = sca; b.scale = sca;
@ -341,13 +336,9 @@ namespace Spine.Unity {
b.valid = true; b.valid = true;
if (mode == SkeletonUtilityBone.Mode.Override) { if (mode == SkeletonUtilityBone.Mode.Override) {
if (rot) if (rot) goTransform.localRotation = Quaternion.Euler(0, 0, b.bone.AppliedRotation);
go.transform.localRotation = Quaternion.Euler(0, 0, b.bone.AppliedRotation); if (pos) goTransform.localPosition = new Vector3(b.bone.X, b.bone.Y, 0);
goTransform.localScale = new Vector3(b.bone.scaleX, b.bone.scaleY, 0);
if (pos)
go.transform.localPosition = new Vector3(b.bone.X, b.bone.Y, 0);
go.transform.localScale = new Vector3(b.bone.scaleX, b.bone.scaleY, 0);
} }
return go; return go;

View File

@ -28,15 +28,13 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/ *****************************************************************************/
// Contributed by: Mitch Thompson
using UnityEngine; using UnityEngine;
using Spine; using Spine;
namespace Spine.Unity { namespace Spine.Unity {
/// <summary>Sets a GameObject's transform to match a bone on a Spine skeleton.</summary> /// <summary>Sets a GameObject's transform to match a bone on a Spine skeleton.</summary>
[ExecuteInEditMode] [ExecuteInEditMode]
[AddComponentMenu("Spine/SkeletonUtilityBone")] [AddComponentMenu("Spine/SkeletonGameObjectsBone")]
public class SkeletonUtilityBone : MonoBehaviour { public class SkeletonUtilityBone : MonoBehaviour {
public enum Mode { public enum Mode {
Follow, Follow,
@ -59,7 +57,7 @@ namespace Spine.Unity {
public float overrideAlpha = 1; public float overrideAlpha = 1;
#endregion #endregion
[System.NonSerialized] public SkeletonUtility skeletonUtility; [System.NonSerialized] public SkeletonUtility hierarchy;
[System.NonSerialized] public Bone bone; [System.NonSerialized] public Bone bone;
[System.NonSerialized] public bool transformLerpComplete; [System.NonSerialized] public bool transformLerpComplete;
[System.NonSerialized] public bool valid; [System.NonSerialized] public bool valid;
@ -71,23 +69,21 @@ namespace Spine.Unity {
public void Reset () { public void Reset () {
bone = null; bone = null;
cachedTransform = transform; cachedTransform = transform;
valid = skeletonUtility != null && skeletonUtility.skeletonRenderer != null && skeletonUtility.skeletonRenderer.valid; valid = hierarchy != null && hierarchy.skeletonRenderer != null && hierarchy.skeletonRenderer.valid;
if (!valid) if (!valid)
return; return;
skeletonTransform = skeletonUtility.transform; skeletonTransform = hierarchy.transform;
skeletonUtility.OnReset -= HandleOnReset; hierarchy.OnReset -= HandleOnReset;
skeletonUtility.OnReset += HandleOnReset; hierarchy.OnReset += HandleOnReset;
DoUpdate(UpdatePhase.Local); DoUpdate(UpdatePhase.Local);
} }
void OnEnable () { void OnEnable () {
skeletonUtility = transform.GetComponentInParent<SkeletonUtility>(); hierarchy = transform.GetComponentInParent<SkeletonUtility>();
if (hierarchy == null) return;
if (skeletonUtility == null) hierarchy.RegisterBone(this);
return; hierarchy.OnReset += HandleOnReset;
skeletonUtility.RegisterBone(this);
skeletonUtility.OnReset += HandleOnReset;
} }
void HandleOnReset () { void HandleOnReset () {
@ -95,9 +91,9 @@ namespace Spine.Unity {
} }
void OnDisable () { void OnDisable () {
if (skeletonUtility != null) { if (hierarchy != null) {
skeletonUtility.OnReset -= HandleOnReset; hierarchy.OnReset -= HandleOnReset;
skeletonUtility.UnregisterBone(this); hierarchy.UnregisterBone(this);
} }
} }
@ -107,7 +103,7 @@ namespace Spine.Unity {
return; return;
} }
var skeleton = skeletonUtility.skeletonRenderer.skeleton; var skeleton = hierarchy.skeletonRenderer.skeleton;
if (bone == null) { if (bone == null) {
if (string.IsNullOrEmpty(boneName)) return; if (string.IsNullOrEmpty(boneName)) return;

View File

@ -28,25 +28,23 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/ *****************************************************************************/
// Contributed by: Mitch Thompson
using UnityEngine; using UnityEngine;
namespace Spine.Unity { namespace Spine.Unity {
[RequireComponent(typeof(SkeletonUtilityBone)), ExecuteInEditMode] [RequireComponent(typeof(SkeletonUtilityBone)), ExecuteInEditMode]
public abstract class SkeletonUtilityConstraint : MonoBehaviour { public abstract class SkeletonUtilityConstraint : MonoBehaviour {
protected SkeletonUtilityBone utilBone; protected SkeletonUtilityBone bone;
protected SkeletonUtility skeletonUtility; protected SkeletonUtility hierarchy;
protected virtual void OnEnable () { protected virtual void OnEnable () {
utilBone = GetComponent<SkeletonUtilityBone>(); bone = GetComponent<SkeletonUtilityBone>();
skeletonUtility = transform.GetComponentInParent<SkeletonUtility>(); hierarchy = transform.GetComponentInParent<SkeletonUtility>();
skeletonUtility.RegisterConstraint(this); hierarchy.RegisterConstraint(this);
} }
protected virtual void OnDisable () { protected virtual void OnDisable () {
skeletonUtility.UnregisterConstraint(this); hierarchy.UnregisterConstraint(this);
} }
public abstract void DoUpdate (); public abstract void DoUpdate ();