Skip to content

Instantly share code, notes, and snippets.

@inoook
Created April 20, 2022 11:20
Show Gist options
  • Save inoook/6de3d834d1a2b13006e69b1d0d1c075a to your computer and use it in GitHub Desktop.
Save inoook/6de3d834d1a2b13006e69b1d0d1c075a to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using UnityEngine;
public class FadeTransition
{
[SerializeField] CanvasGroup canvasGroup = null;
public FadeTransition(CanvasGroup canvasGroup) {
this.canvasGroup = canvasGroup;
this.canvasGroup.alpha = 0;
}
public async Task StartFade(float time, float to, System.Action onComplete = null) {
var tween = LeanTween.value(canvasGroup.gameObject, canvasGroup.alpha, to, time).setOnUpdate((v) => {
canvasGroup.alpha = v;
});
await tween;
onComplete?.Invoke();
}
}
public static class LeanTweenExtension
{
public static TaskAwaiter<bool> GetAwaiter(this LTDescr leanTween)
{
var tcs = new TaskCompletionSource<bool>();
leanTween.setOnComplete(() => {
tcs.SetResult(true);
});
return tcs.Task.GetAwaiter();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment