Browse Source

[unity] Fixed IAnimationStateComponent missing (destroyed) component exception due to interface null check instead of using overloaded UnityEngine.Object.operator==. Closes #1972.
IAnimationStateComponent throws exception due to interface null check #1972

Harald Csaszar 3 years ago
parent
commit
4b70b1489c

+ 2 - 2
spine-unity/Assets/Spine Examples/Scripts/Sample Components/BoneLocalOverride.cs

@@ -53,8 +53,8 @@ namespace Spine.Unity.Examples {
 #if UNITY_EDITOR
 		void OnValidate () {
 			if (Application.isPlaying) return;
-			spineComponent = spineComponent ?? GetComponent<ISkeletonAnimation>();
-			if (spineComponent == null) return;
+			if (spineComponent == null) spineComponent = GetComponent<ISkeletonAnimation>();
+			if (spineComponent.IsNullOrDestroyed()) return;
 			if (bone != null) bone.SetToSetupPose();
 			OverrideLocal(spineComponent);
 		}

+ 6 - 4
spine-unity/Assets/Spine Examples/Scripts/Sample Components/SpineEventUnityHandler.cs

@@ -48,9 +48,11 @@ namespace Spine.Unity.Prototyping {
 		IAnimationStateComponent animationStateComponent;
 
 		void Start () {
-			skeletonComponent = skeletonComponent ?? GetComponent<ISkeletonComponent>();
+			if (skeletonComponent == null)
+				skeletonComponent = GetComponent<ISkeletonComponent>();
 			if (skeletonComponent == null) return;
-			animationStateComponent = animationStateComponent ?? skeletonComponent as IAnimationStateComponent;
+			if (animationStateComponent == null)
+				animationStateComponent = skeletonComponent as IAnimationStateComponent;
 			if (animationStateComponent == null) return;
 			var skeleton = skeletonComponent.Skeleton;
 			if (skeleton == null) return;
@@ -66,8 +68,8 @@ namespace Spine.Unity.Prototyping {
 		}
 
 		void OnDestroy () {
-			animationStateComponent = animationStateComponent ?? GetComponent<IAnimationStateComponent>();
-			if (animationStateComponent == null) return;
+			if (animationStateComponent == null) animationStateComponent = GetComponent<IAnimationStateComponent>();
+			if (animationStateComponent.IsNullOrDestroyed()) return;
 
 			var state = animationStateComponent.AnimationState;
 			foreach (var ep in events) {

+ 14 - 6
spine-unity/Assets/Spine/Runtime/spine-unity/ISkeletonAnimation.cs

@@ -39,8 +39,16 @@ namespace Spine.Unity {
 
 	public delegate void UpdateBonesDelegate (ISkeletonAnimation animated);
 
+	public interface ISpineComponent { }
+	public static class ISpineComponentExtensions {
+		public static bool IsNullOrDestroyed (this ISpineComponent component) {
+			if (component == null) return true;
+			return (UnityEngine.Object)component == null;
+		}
+	}
+
 	/// <summary>A Spine-Unity Component that animates a Skeleton but not necessarily with a Spine.AnimationState.</summary>
-	public interface ISkeletonAnimation {
+	public interface ISkeletonAnimation : ISpineComponent {
 		event UpdateBonesDelegate UpdateLocal;
 		event UpdateBonesDelegate UpdateWorld;
 		event UpdateBonesDelegate UpdateComplete;
@@ -48,13 +56,13 @@ namespace Spine.Unity {
 	}
 
 	/// <summary>Holds a reference to a SkeletonDataAsset.</summary>
-	public interface IHasSkeletonDataAsset {
+	public interface IHasSkeletonDataAsset : ISpineComponent {
 		/// <summary>Gets the SkeletonDataAsset of the Spine Component.</summary>
 		SkeletonDataAsset SkeletonDataAsset { get; }
 	}
 
 	/// <summary>A Spine-Unity Component that manages a Spine.Skeleton instance, instantiated from a SkeletonDataAsset.</summary>
-	public interface ISkeletonComponent {
+	public interface ISkeletonComponent : ISpineComponent {
 		/// <summary>Gets the SkeletonDataAsset of the Spine Component.</summary>
 		//[System.Obsolete]
 		SkeletonDataAsset SkeletonDataAsset { get; }
@@ -64,18 +72,18 @@ namespace Spine.Unity {
 	}
 
 	/// <summary>A Spine-Unity Component that uses a Spine.AnimationState to animate its skeleton.</summary>
-	public interface IAnimationStateComponent {
+	public interface IAnimationStateComponent : ISpineComponent {
 		/// <summary>Gets the Spine.AnimationState of the animated Spine Component. This is equivalent to SkeletonAnimation.state.</summary>
 		AnimationState AnimationState { get; }
 	}
 
 	/// <summary>A Spine-Unity Component that holds a reference to a SkeletonRenderer.</summary>
-	public interface IHasSkeletonRenderer {
+	public interface IHasSkeletonRenderer : ISpineComponent {
 		SkeletonRenderer SkeletonRenderer { get; }
 	}
 
 	/// <summary>A Spine-Unity Component that holds a reference to an ISkeletonComponent.</summary>
-	public interface IHasSkeletonComponent {
+	public interface IHasSkeletonComponent : ISpineComponent {
 		ISkeletonComponent SkeletonComponent { get; }
 	}
 }

+ 5 - 5
spine-unity/Modules/com.esotericsoftware.spine.timeline/Runtime/SpineAnimationState/SpineAnimationStateMixerBehaviour.cs

@@ -75,7 +75,7 @@ namespace Spine.Unity.Playables {
 		}
 
 		protected void HandlePause (Playable playable) {
-			if (animationStateComponent == null) return;
+			if (animationStateComponent.IsNullOrDestroyed()) return;
 
 			TrackEntry current = animationStateComponent.AnimationState.GetCurrent(trackIndex);
 			if (current != null && current == timelineStartedTrackEntry) {
@@ -86,7 +86,7 @@ namespace Spine.Unity.Playables {
 		}
 
 		protected void HandleResume (Playable playable) {
-			if (animationStateComponent == null) return;
+			if (animationStateComponent.IsNullOrDestroyed()) return;
 
 			TrackEntry current = animationStateComponent.AnimationState.GetCurrent(trackIndex);
 			if (current != null && current == pausedTrackEntry) {
@@ -95,7 +95,7 @@ namespace Spine.Unity.Playables {
 		}
 
 		protected void HandleClipEnd () {
-			if (animationStateComponent == null) return;
+			if (animationStateComponent.IsNullOrDestroyed()) return;
 
 			var state = animationStateComponent.AnimationState;
 			if (endAtClipEnd &&
@@ -116,7 +116,7 @@ namespace Spine.Unity.Playables {
 			var skeletonGraphic = playerData as SkeletonGraphic;
 			animationStateComponent = playerData as IAnimationStateComponent;
 			var skeletonComponent = playerData as ISkeletonComponent;
-			if (animationStateComponent == null || skeletonComponent == null) return;
+			if (animationStateComponent.IsNullOrDestroyed() || skeletonComponent == null) return;
 
 			var skeleton = skeletonComponent.Skeleton;
 			var state = animationStateComponent.AnimationState;
@@ -226,7 +226,7 @@ namespace Spine.Unity.Playables {
 			SkeletonAnimation skeletonAnimation, SkeletonGraphic skeletonGraphic) {
 
 			if (Application.isPlaying) return;
-			if (skeletonComponent == null || animationStateComponent == null) return;
+			if (animationStateComponent.IsNullOrDestroyed() || skeletonComponent == null) return;
 
 			int inputCount = playable.GetInputCount();
 			int lastNonZeroWeightTrack = -1;