Eonirr

Untitled

Mar 19th, 2026
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. // FADEOUT
  2. public IEnumerator FadeOut(AudioSource audioSource, float duration)
  3. {
  4. float startVolume = audioSource.volume;
  5.  
  6. float time = 0f;
  7. while (time < duration)
  8. {
  9. time += Time.deltaTime;
  10. audioSource.volume = Mathf.Lerp(startVolume, 0f, time / duration);
  11. yield return null;
  12. }
  13.  
  14. audioSource.volume = 0f;
  15. audioSource.Stop();
  16. }
  17.  
  18. // FADEIN
  19. public IEnumerator FadeIn(AudioSource audioSource, float duration, float targetVolume = 1f)
  20. {
  21. float time = 0f;
  22. audioSource.volume = 0f;
  23. audioSource.Play();
  24.  
  25. while (time < duration)
  26. {
  27. time += Time.deltaTime;
  28. audioSource.volume = Mathf.Lerp(0f, targetVolume, time / duration);
  29. yield return null;
  30. }
  31.  
  32. audioSource.volume = targetVolume;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment