Skip to content

Commit

Permalink
[unity] Fixed IAnimationStateComponent missing (destroyed) component …
Browse files Browse the repository at this point in the history
…exception due to interface null check instead of using overloaded UnityEngine.Object.operator==. Closes #1972.

IAnimationStateComponent throws exception due to interface null check #1972
  • Loading branch information
HaraldCsaszar committed Oct 27, 2021
1 parent 12cd0d7 commit 4b70b14
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public class BoneLocalOverride : MonoBehaviour {
#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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ public class EventPair {
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;
Expand All @@ -66,8 +68,8 @@ void Start () {
}

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) {
Expand Down
20 changes: 14 additions & 6 deletions spine-unity/Assets/Spine/Runtime/spine-unity/ISkeletonAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,30 @@ public enum UpdateMode {

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;
Skeleton Skeleton { get; }
}

/// <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; }
Expand All @@ -64,18 +72,18 @@ public interface ISkeletonComponent {
}

/// <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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public override void OnBehaviourPlay (Playable playable, FrameData info) {
}

protected void HandlePause (Playable playable) {
if (animationStateComponent == null) return;
if (animationStateComponent.IsNullOrDestroyed()) return;

TrackEntry current = animationStateComponent.AnimationState.GetCurrent(trackIndex);
if (current != null && current == timelineStartedTrackEntry) {
Expand All @@ -86,7 +86,7 @@ protected void HandlePause (Playable playable) {
}

protected void HandleResume (Playable playable) {
if (animationStateComponent == null) return;
if (animationStateComponent.IsNullOrDestroyed()) return;

TrackEntry current = animationStateComponent.AnimationState.GetCurrent(trackIndex);
if (current != null && current == pausedTrackEntry) {
Expand All @@ -95,7 +95,7 @@ protected void HandleResume (Playable playable) {
}

protected void HandleClipEnd () {
if (animationStateComponent == null) return;
if (animationStateComponent.IsNullOrDestroyed()) return;

var state = animationStateComponent.AnimationState;
if (endAtClipEnd &&
Expand All @@ -116,7 +116,7 @@ public override void ProcessFrame (Playable playable, FrameData info, object pla
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;
Expand Down Expand Up @@ -226,7 +226,7 @@ public void PreviewEditModePose (Playable playable,
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;
Expand Down

0 comments on commit 4b70b14

Please sign in to comment.