Skip to content

Ahmed7966/Custom-Yield-Instructions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Unity Custom Yield Instructions

A collection of practical, reusable custom yield instructions for Unity coroutines — designed to make coroutines more readable, expressive, and powerful than Unity’s built-in WaitForSeconds and WaitUntil.

These yields cover real-world gameplay, UI, animation, and audio workflows you hit every day in Unity projects.


✨ Why This Exists

Unity coroutines are great — until they aren’t.

Typical problems:

  • Repetitive while loops and polling logic
  • Hard-to-read timing and state checks
  • One-off coroutine code that isn’t reusable
  • Mixing gameplay logic with low-level timing details

This library solves that by providing clean, intention-revealing yield instructions you can reuse across projects.


✅ Benefits

  • 📖 Highly readable coroutines
  • 🔁 Reusable, composable yield instructions
  • 🎮 Designed for real gameplay, UI, and audio needs
  • 🧠 Reduces boilerplate and polling loops
  • ⏱️ Supports scaled and unscaled time
  • 🔌 Drop-in utility — no framework lock-in

📦 What’s Included

⏱️ Time & Flow Control

  • Wait for a specific number of frames
  • Wait for scaled or unscaled seconds
  • Wait until a custom condition is met
  • Wait while a condition remains true

🎞️ Animator & Animation

  • Wait for an animation state to start
  • Wait for animation completion
  • Wait for blend transitions to finish
  • Wait for normalized animation time

🖱️ UI & Input

  • Wait for button click
  • Wait for UI element readiness
  • Wait for hover / focus
  • Wait for cooldown completion

🔊 Audio

  • Wait for audio clip start
  • Wait for audio clip end
  • Wait for normalized playback time
  • Wait for fade-in / fade-out completion

🚀 Quick Examples

Audio Yields

IEnumerator AudioExample()
{
    Debug.Log("Start");

    audioSource.Play(clip);

    yield return new AudioYields.WaitForAudioEnd(audioSource);

    Debug.Log("Audio ended");
}

Animator Yields

IEnumerator AttackSequence()
{
    animator.SetTrigger("Attack");

    yield return new AnimatorYields.WaitForStateEnter(animator, "Attack");

    yield return new AnimatorYields.WaitForStateExit(animator, "Attack");

    DealDamage();
}

🏷️ Tags

Unity · Unity3D · Coroutines · CustomYieldInstruction ·
Animator · UI · Audio · Game Programming · Async Gameplay ·
Reusable Code · Gameplay Systems

About

A collection of some useful examples of custom yield instruction for unity

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages