Эх сурвалжийг харах

[unity] Fixed Update potentially not being called after Initialize in the first frame after instantiation in certain conditions. Closes #1646.

Harald Csaszar 5 жил өмнө
parent
commit
16c81c7b97

+ 1 - 1
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;

+ 8 - 2
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</summary>
 		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();
+		}
 	}
 
 }

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

+ 15 - 2
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<Animator>(), 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]