diff --git a/spine-unity/Assets/Spine Examples/Scripts/Sample Components/BoneLocalOverride.cs b/spine-unity/Assets/Spine Examples/Scripts/Sample Components/BoneLocalOverride.cs index 66ddd2af7..42b317bfc 100644 --- a/spine-unity/Assets/Spine Examples/Scripts/Sample Components/BoneLocalOverride.cs +++ b/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(); - if (spineComponent == null) return; + if (spineComponent == null) spineComponent = GetComponent(); + if (spineComponent.IsNullOrDestroyed()) return; if (bone != null) bone.SetToSetupPose(); OverrideLocal(spineComponent); } diff --git a/spine-unity/Assets/Spine Examples/Scripts/Sample Components/SpineEventUnityHandler.cs b/spine-unity/Assets/Spine Examples/Scripts/Sample Components/SpineEventUnityHandler.cs index 2457495c5..92a380760 100644 --- a/spine-unity/Assets/Spine Examples/Scripts/Sample Components/SpineEventUnityHandler.cs +++ b/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(); + if (skeletonComponent == null) + skeletonComponent = GetComponent(); 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(); - if (animationStateComponent == null) return; + if (animationStateComponent == null) animationStateComponent = GetComponent(); + if (animationStateComponent.IsNullOrDestroyed()) return; var state = animationStateComponent.AnimationState; foreach (var ep in events) { diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/ISkeletonAnimation.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/ISkeletonAnimation.cs index 90486e381..f571e12e5 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/ISkeletonAnimation.cs +++ b/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; + } + } + /// A Spine-Unity Component that animates a Skeleton but not necessarily with a Spine.AnimationState. - public interface ISkeletonAnimation { + public interface ISkeletonAnimation : ISpineComponent { event UpdateBonesDelegate UpdateLocal; event UpdateBonesDelegate UpdateWorld; event UpdateBonesDelegate UpdateComplete; @@ -48,13 +56,13 @@ namespace Spine.Unity { } /// Holds a reference to a SkeletonDataAsset. - public interface IHasSkeletonDataAsset { + public interface IHasSkeletonDataAsset : ISpineComponent { /// Gets the SkeletonDataAsset of the Spine Component. SkeletonDataAsset SkeletonDataAsset { get; } } /// A Spine-Unity Component that manages a Spine.Skeleton instance, instantiated from a SkeletonDataAsset. - public interface ISkeletonComponent { + public interface ISkeletonComponent : ISpineComponent { /// Gets the SkeletonDataAsset of the Spine Component. //[System.Obsolete] SkeletonDataAsset SkeletonDataAsset { get; } @@ -64,18 +72,18 @@ namespace Spine.Unity { } /// A Spine-Unity Component that uses a Spine.AnimationState to animate its skeleton. - public interface IAnimationStateComponent { + public interface IAnimationStateComponent : ISpineComponent { /// Gets the Spine.AnimationState of the animated Spine Component. This is equivalent to SkeletonAnimation.state. AnimationState AnimationState { get; } } /// A Spine-Unity Component that holds a reference to a SkeletonRenderer. - public interface IHasSkeletonRenderer { + public interface IHasSkeletonRenderer : ISpineComponent { SkeletonRenderer SkeletonRenderer { get; } } /// A Spine-Unity Component that holds a reference to an ISkeletonComponent. - public interface IHasSkeletonComponent { + public interface IHasSkeletonComponent : ISpineComponent { ISkeletonComponent SkeletonComponent { get; } } } diff --git a/spine-unity/Modules/com.esotericsoftware.spine.timeline/Runtime/SpineAnimationState/SpineAnimationStateMixerBehaviour.cs b/spine-unity/Modules/com.esotericsoftware.spine.timeline/Runtime/SpineAnimationState/SpineAnimationStateMixerBehaviour.cs index 0bfae9f15..0d97a9ee8 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.timeline/Runtime/SpineAnimationState/SpineAnimationStateMixerBehaviour.cs +++ b/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;