Skip to content

Instantly share code, notes, and snippets.

@llamacademy
Last active March 14, 2024 08:47
Show Gist options
  • Save llamacademy/d723d04a7b2b98e3105ba1402dc850fd to your computer and use it in GitHub Desktop.
Save llamacademy/d723d04a7b2b98e3105ba1402dc850fd to your computer and use it in GitHub Desktop.
Introduction to Coroutines in Unity scripts from https://www.youtube.com/watch?v=FfgI0BJ38P4. If you get value from LlamAcademy, consider becoming a Patreon supporter at https://www.patreon.com/llamacademy
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Image))]
public class FadeAlpha : MonoBehaviour
{
public bool UseRealtimeWait = false;
private Image Image;
private Coroutine Coroutine;
private void OnGUI()
{
if (GUI.Button(new Rect(new Vector2(25, 25), new Vector2(150, 40)), "Stop Fading"))
{
if (Coroutine != null)
{
StopCoroutine(Coroutine);
}
}
if (GUI.Button(new Rect(new Vector2(25, 75), new Vector2(150, 40)), "Start Fading"))
{
StartFading();
}
}
private void Awake()
{
Image = GetComponent<Image>();
}
void StartFading()
{
Coroutine = StartCoroutine(FadeOutAlpha());
}
IEnumerator FadeOutAlpha()
{
// Uncomment this line if you want to compare WaitForSeondsRealtime vs WaitForSeconds
// Time.timeScale = 0.5f;
if (UseRealtimeWait)
{
WaitForSecondsRealtime Wait = new WaitForSecondsRealtime(1 / 30f);
for (float alpha = 1; alpha >= 0; alpha -= 0.01f)
{
Color color = Image.color;
color.a = alpha;
Image.color = color;
yield return Wait;
}
}
else
{
WaitForSeconds Wait = new WaitForSeconds(1 / 30f);
for (float alpha = 1; alpha >= 0; alpha -= 0.01f)
{
Color color = Image.color;
color.a = alpha;
Image.color = color;
yield return Wait;
}
}
}
}
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Image))]
public class FadeAlphaWithTarget : MonoBehaviour
{
public bool WaitUntilTargetIsInVisible;
public Image DependencyImage;
private Image Image;
private void Awake()
{
Application.targetFrameRate = 60;
Image = GetComponent<Image>();
}
void Start()
{
StartCoroutine(FadeAlpha());
}
IEnumerator FadeAlpha()
{
if (WaitUntilTargetIsInVisible)
{
yield return new WaitUntil(() => DependencyImage.color.a <= 0.01f);
}
else
{
yield return new WaitWhile(() => DependencyImage.color.a > 0);
}
for (float alpha = 1; alpha >= 0; alpha -= 0.01f)
{
Color color = Image.color;
color.a = alpha;
Image.color = color;
yield return null;
}
}
}
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Image))]
public class FadeWaitingForFixedUpdate : MonoBehaviour
{
private Image Image;
private void Awake()
{
Image = GetComponent<Image>();
}
void Start()
{
StartCoroutine(FadeAlpha());
}
IEnumerator FadeAlpha()
{
WaitForFixedUpdate Wait = new WaitForFixedUpdate();
for (float alpha = 1; alpha >= 0; alpha -= 0.01f)
{
Color color = Image.color;
color.a = alpha;
Image.color = color;
Debug.Log("1. Waiting for Fixed Update");
yield return Wait;
Debug.Log("3. Fixed Update Completed");
}
}
private void FixedUpdate()
{
Debug.Log("2. Fixed Update");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment