From 596e9e3290a667b6ccafb3aea877ef92616e306c Mon Sep 17 00:00:00 2001 From: azukiidx Date: Mon, 4 Mar 2024 19:49:07 +0900 Subject: [PATCH 1/2] Fixed exception when played earlier than Awake/Start --- .../Runtime/src/UI/AnimatedImage.cs | 64 +++++++++++++++---- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/unity/RLottieUnity/Assets/LottiePlugin/Runtime/src/UI/AnimatedImage.cs b/unity/RLottieUnity/Assets/LottiePlugin/Runtime/src/UI/AnimatedImage.cs index 5bd1533..4b02d49 100644 --- a/unity/RLottieUnity/Assets/LottiePlugin/Runtime/src/UI/AnimatedImage.cs +++ b/unity/RLottieUnity/Assets/LottiePlugin/Runtime/src/UI/AnimatedImage.cs @@ -1,3 +1,4 @@ +using System; using System.Collections; using UnityEngine; using UnityEngine.UI; @@ -26,25 +27,23 @@ public sealed class AnimatedImage : MonoBehaviour private LottieAnimation _lottieAnimation; private Coroutine _renderLottieAnimationCoroutine; - private WaitForEndOfFrame _waitForEndOfFrame; - - private void Awake() - { - Transform = transform; - _waitForEndOfFrame = new WaitForEndOfFrame(); - } - + private readonly WaitForEndOfFrame _waitForEndOfFrame = new WaitForEndOfFrame(); + private bool _reservedPlay = false; + private void Start() { if (_animationJson == null) { return; } - if (_rawImage == null) + + if (IsInitialized()) { - _rawImage = GetComponent(); + return; } - CreateIfNeededAndReturnLottieAnimation(); + + Initialize(); + if (_playOnAwake) { Play(); @@ -54,22 +53,65 @@ private void Start() _lottieAnimation.DrawOneFrame(0); } } + + private void OnEnable() + { + if (_reservedPlay) + { + Play(); + } + } + private void OnDestroy() { + _reservedPlay = false; DisposeLottieAnimation(); } + private void Initialize() + { + Transform = transform; + + if (_rawImage == null) + { + _rawImage = GetComponent(); + } + CreateIfNeededAndReturnLottieAnimation(); + } + private bool IsInitialized() + { + return _lottieAnimation != null && _rawImage != null; + } + public void Play() { + if (!IsInitialized()) + { + Initialize(); + } + + if (!isActiveAndEnabled) + { + _reservedPlay = true; + return; + } if (_renderLottieAnimationCoroutine != null) { StopCoroutine(_renderLottieAnimationCoroutine); } _lottieAnimation.Play(); _renderLottieAnimationCoroutine = StartCoroutine(RenderLottieAnimationCoroutine()); + + _reservedPlay = false; } + public void Stop() { + if (!IsInitialized()) + { + Initialize(); + } + if (_renderLottieAnimationCoroutine != null) { StopCoroutine(_renderLottieAnimationCoroutine); From 93ec8126334f1fb0002763700fe17c9c907d7def Mon Sep 17 00:00:00 2001 From: azukiidx Date: Sat, 23 Mar 2024 17:37:22 +0900 Subject: [PATCH 2/2] fix logic --- .../Assets/LottiePlugin/Runtime/src/UI/AnimatedImage.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/unity/RLottieUnity/Assets/LottiePlugin/Runtime/src/UI/AnimatedImage.cs b/unity/RLottieUnity/Assets/LottiePlugin/Runtime/src/UI/AnimatedImage.cs index 4b02d49..2ef8c7d 100644 --- a/unity/RLottieUnity/Assets/LottiePlugin/Runtime/src/UI/AnimatedImage.cs +++ b/unity/RLottieUnity/Assets/LottiePlugin/Runtime/src/UI/AnimatedImage.cs @@ -37,13 +37,11 @@ private void Start() return; } - if (IsInitialized()) + if (!IsInitialized()) { - return; + Initialize(); } - Initialize(); - if (_playOnAwake) { Play();