From 16c81c7b97c529ef0035da255a7508a943911655 Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Mon, 23 Mar 2020 13:47:48 +0100 Subject: [PATCH] [unity] Fixed Update potentially not being called after Initialize in the first frame after instantiation in certain conditions. Closes #1646. --- .../spine-unity/Editor/Utility/AssetUtility.cs | 2 +- .../spine-unity/Components/SkeletonAnimation.cs | 10 ++++++++-- .../spine-unity/Components/SkeletonGraphic.cs | 7 +++++-- .../spine-unity/Components/SkeletonMecanim.cs | 17 +++++++++++++++-- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Utility/AssetUtility.cs b/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Utility/AssetUtility.cs index f57b37847..a32a5d4b7 100644 --- a/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Utility/AssetUtility.cs +++ b/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Utility/AssetUtility.cs @@ -1008,7 +1008,7 @@ namespace Spine.Unity.Editor { newSkeletonAnimation.Initialize(false); } catch (System.Exception e) { if (destroyInvalid) { - Debug.LogWarning("Editor-instantiated SkeletonAnimation threw an Exception. Destroying GameObject to prevent orphaned GameObject.", skeletonDataAsset); + Debug.LogWarning("Editor-instantiated SkeletonAnimation threw an Exception. Destroying GameObject to prevent orphaned GameObject.\n" + e.Message, skeletonDataAsset); GameObject.DestroyImmediate(go); } throw e; diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonAnimation.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonAnimation.cs index acbca444e..ce177e0d7 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonAnimation.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonAnimation.cs @@ -52,6 +52,7 @@ namespace Spine.Unity { /// This is the Spine.AnimationState object of this SkeletonAnimation. You can control animations through it. /// Note that this object, like .skeleton, is not guaranteed to exist in Awake. Do all accesses and caching to it in Start public Spine.AnimationState AnimationState { get { return this.state; } } + private bool wasUpdatedAfterInit = true; #endregion #region Bone Callbacks ISkeletonAnimation @@ -149,13 +150,12 @@ namespace Spine.Unity { public override void Initialize (bool overwrite) { if (valid && !overwrite) return; - base.Initialize(overwrite); if (!valid) return; - state = new Spine.AnimationState(skeletonDataAsset.GetAnimationStateData()); + wasUpdatedAfterInit = false; if (!string.IsNullOrEmpty(_animationName)) { var animationObject = skeletonDataAsset.GetSkeletonData(false).FindAnimation(_animationName); @@ -203,8 +203,14 @@ namespace Spine.Unity { if (_UpdateComplete != null) { _UpdateComplete(this); } + wasUpdatedAfterInit = true; } + public override void LateUpdate () { + // instantiation can happen from Update() after this component, leading to a missing Update() call. + if (!wasUpdatedAfterInit) Update(0); + base.LateUpdate(); + } } } diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs index 0579fee2a..9a8a0b05d 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs @@ -59,6 +59,7 @@ namespace Spine.Unity { public bool freeze; public bool unscaledTime; + private bool wasUpdatedAfterInit = true; private Texture baseTexture = null; #if UNITY_EDITOR @@ -212,9 +213,12 @@ namespace Spine.Unity { } if (UpdateComplete != null) UpdateComplete(this); + wasUpdatedAfterInit = true; } public void LateUpdate () { + // instantiation can happen from Update() after this component, leading to a missing Update() call. + if (!wasUpdatedAfterInit) Update(0); if (freeze) return; //this.SetVerticesDirty(); // Which is better? UpdateMesh(); @@ -313,6 +317,7 @@ namespace Spine.Unity { if (!string.IsNullOrEmpty(initialSkinName)) skeleton.SetSkin(initialSkinName); + wasUpdatedAfterInit = false; if (!string.IsNullOrEmpty(startingAnimation)) { var animationObject = skeletonDataAsset.GetSkeletonData(false).FindAnimation(startingAnimation); if (animationObject != null) { @@ -321,8 +326,6 @@ namespace Spine.Unity { if (!Application.isPlaying) Update(0f); #endif - if (freeze) - Update(0f); } } diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonMecanim.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonMecanim.cs index 6de3a9ea9..1300b8f41 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonMecanim.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonMecanim.cs @@ -36,6 +36,7 @@ namespace Spine.Unity { [SerializeField] protected MecanimTranslator translator; public MecanimTranslator Translator { get { return translator; } } + private bool wasUpdatedAfterInit = true; #region Bone Callbacks (ISkeletonAnimation) protected event UpdateBonesDelegate _UpdateLocal; @@ -61,12 +62,17 @@ namespace Spine.Unity { #endregion public override void Initialize (bool overwrite) { - if (valid && !overwrite) return; + if (valid && !overwrite) + return; + base.Initialize(overwrite); - if (!valid) return; + + if (!valid) + return; if (translator == null) translator = new MecanimTranslator(); translator.Initialize(GetComponent(), this.skeletonDataAsset); + wasUpdatedAfterInit = false; } public void Update () { @@ -106,6 +112,13 @@ namespace Spine.Unity { if (_UpdateComplete != null) _UpdateComplete(this); } + wasUpdatedAfterInit = true; + } + + public override void LateUpdate () { + // instantiation can happen from Update() after this component, leading to a missing Update() call. + if (!wasUpdatedAfterInit) Update(); + base.LateUpdate(); } [System.Serializable]