Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed exception when played earlier than Awake/Start #28

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
Expand Down Expand Up @@ -26,25 +27,21 @@ 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<RawImage>();
Initialize();
}
CreateIfNeededAndReturnLottieAnimation();

if (_playOnAwake)
{
Play();
Expand All @@ -54,22 +51,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<RawImage>();
}
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);
Expand Down