Browse Source

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

Fenrisul 10 năm trước cách đây
mục cha
commit
dc5873876b

+ 19 - 5
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>();
+		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;
 			}
 		}
 	}

+ 7 - 7
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 Awake() {
+		GetComponent<SkeletonRenderer>().OnReset += Apply;
+	}
 
-	Atlas atlas;
 
-	void Start() {
+	void Apply(SkeletonRenderer skeletonRenderer) {
 		atlas = atlasAsset.GetAtlas();
-		this.skeletonRenderer = GetComponent<SkeletonRenderer>();
-
+		
 		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;
 		}
 	}

+ 13 - 3
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 () {