From dc5873876b75d5dc950271bd4e662ece132a8b68 Mon Sep 17 00:00:00 2001 From: Fenrisul Date: Mon, 26 Jan 2015 22:02:55 -0800 Subject: [PATCH] Modified SkeletonRenderer to destroy mesh1 and mesh2 when disabled if the gameobject its attached to is no longer visible. --- .../Examples/Scripts/FootSoldierExample.cs | 24 +++++++++++++++---- .../Assets/spine-unity/AtlasRegionAttacher.cs | 16 ++++++------- .../Assets/spine-unity/SkeletonRenderer.cs | 16 ++++++++++--- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/spine-unity/Assets/Examples/Scripts/FootSoldierExample.cs b/spine-unity/Assets/Examples/Scripts/FootSoldierExample.cs index 655445869..3a7152ef5 100644 --- a/spine-unity/Assets/Examples/Scripts/FootSoldierExample.cs +++ b/spine-unity/Assets/Examples/Scripts/FootSoldierExample.cs @@ -42,6 +42,9 @@ public class FootSoldierExample : MonoBehaviour { [SpineAnimation] public string attackAnimation; + [SpineAnimation] + public string moveAnimation; + [SpineSlot] public string eyesSlot; @@ -54,22 +57,33 @@ public class FootSoldierExample : MonoBehaviour { [Range(0, 0.2f)] public float blinkDuration = 0.05f; + public float moveSpeed = 3; + private SkeletonAnimation skeletonAnimation; void Awake() { skeletonAnimation = GetComponent(); + skeletonAnimation.OnReset += Apply; } - void Start() { - skeletonAnimation.state.SetAnimation(0, idleAnimation, true); + void Apply(SkeletonRenderer skeletonRenderer) { StartCoroutine("Blink"); } void Update() { if (Input.GetKey(KeyCode.Space)) { - if (skeletonAnimation.state.GetCurrent(0).Animation.Name != attackAnimation) { - skeletonAnimation.state.SetAnimation(0, attackAnimation, false); - skeletonAnimation.state.AddAnimation(0, idleAnimation, true, 0); + skeletonAnimation.AnimationName = attackAnimation; + } else { + if (Input.GetKey(KeyCode.RightArrow)) { + skeletonAnimation.AnimationName = moveAnimation; + skeletonAnimation.skeleton.FlipX = false; + transform.Translate(moveSpeed * Time.deltaTime, 0, 0); + } else if(Input.GetKey(KeyCode.LeftArrow)) { + skeletonAnimation.AnimationName = moveAnimation; + skeletonAnimation.skeleton.FlipX = true; + transform.Translate(-moveSpeed * Time.deltaTime, 0, 0); + } else { + skeletonAnimation.AnimationName = idleAnimation; } } } diff --git a/spine-unity/Assets/spine-unity/AtlasRegionAttacher.cs b/spine-unity/Assets/spine-unity/AtlasRegionAttacher.cs index 193ab754d..d3824f521 100644 --- a/spine-unity/Assets/spine-unity/AtlasRegionAttacher.cs +++ b/spine-unity/Assets/spine-unity/AtlasRegionAttacher.cs @@ -17,16 +17,16 @@ public class AtlasRegionAttacher : MonoBehaviour { public AtlasAsset atlasAsset; public SlotRegionPair[] attachments; - [HideInInspector] - public SkeletonRenderer skeletonRenderer; - - Atlas atlas; - void Start() { - atlas = atlasAsset.GetAtlas(); - this.skeletonRenderer = GetComponent(); + void Awake() { + GetComponent().OnReset += Apply; + } + + void Apply(SkeletonRenderer skeletonRenderer) { + atlas = atlasAsset.GetAtlas(); + AtlasAttachmentLoader loader = new AtlasAttachmentLoader(atlas); float scaleMultiplier = skeletonRenderer.skeletonDataAsset.scale; @@ -41,7 +41,7 @@ public class AtlasRegionAttacher : MonoBehaviour { regionAttachment.SetColor(new Color(1, 1, 1, 1)); regionAttachment.UpdateOffset(); - var slot = this.skeletonRenderer.skeleton.FindSlot(entry.slot); + var slot = skeletonRenderer.skeleton.FindSlot(entry.slot); slot.Attachment = regionAttachment; } } diff --git a/spine-unity/Assets/spine-unity/SkeletonRenderer.cs b/spine-unity/Assets/spine-unity/SkeletonRenderer.cs index 07cabff32..ad4d0137a 100644 --- a/spine-unity/Assets/spine-unity/SkeletonRenderer.cs +++ b/spine-unity/Assets/spine-unity/SkeletonRenderer.cs @@ -117,9 +117,19 @@ public class SkeletonRenderer : MonoBehaviour { if (OnReset != null) OnReset(this); } - - public void Awake () { - Reset(); + + public virtual void OnEnable() { + if(mesh1 == null || mesh2 == null) + Reset(); + } + + public virtual void OnDisable() { + if (Application.isPlaying && gameObject.activeInHierarchy == false) { + if (mesh1 != null) + Destroy(mesh1); + if (mesh2 != null) + Destroy(mesh2); + } } private Mesh newMesh () {