Modified SkeletonRenderer to destroy mesh1 and mesh2 when disabled if the gameobject its attached to is no longer visible.

This commit is contained in:
Fenrisul 2015-01-26 22:02:55 -08:00
parent 7638ed96ff
commit dc5873876b
3 changed files with 40 additions and 16 deletions

View File

@ -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>();
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;
}
}
}

View File

@ -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<SkeletonRenderer>();
void Awake() {
GetComponent<SkeletonRenderer>().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;
}
}

View File

@ -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 () {