Deozaan

IntroLoop.cs

Jun 2nd, 2020
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.20 KB | None | 0 0
  1. //Example of use
  2. //Construct
  3. IntroLoop clip = new IntroLoop(audioSource,5f,10f,20f);
  4. //no intro just loop
  5. IntroLoop clip2 = new IntroLoop(audioSource,10f,20f,false);
  6.  
  7. //you can set it to play once
  8. clip2.playOnce = true;    
  9.  
  10. //call to start
  11. clip.start();
  12.  
  13. //call once a frame, this resets the loop if the time hits the loop boundary
  14. //or stops playing if playOnce = true
  15. clip.checkTime();
  16.  
  17. //call to stop
  18. clip.stop();
  19.  
  20.  
  21. /* **************************************************** */
  22. //The Music IntroLoop Class handles looping music, and playing an intro to the loop
  23. public class IntroLoop {
  24.     private AudioSource source;
  25.     private float startBoundary;
  26.     private float introBoundary;
  27.     private float loopBoundary;
  28.     //set to play a clip once
  29.     public bool playOnce = false;
  30.  
  31.     //play from start for intro
  32.     public IntroLoop(AudioSource source, float introBoundary, float loopBoundary) {
  33.         this.source = source;
  34.         this.startBoundary = 0;
  35.         this.introBoundary = introBoundary;
  36.         this.loopBoundary = loopBoundary;
  37.     }
  38.     //play from start for intro or just loop
  39.     public IntroLoop(AudioSource source, float introBoundary, float loopBoundary, bool playIntro) {
  40.         this.source = source;
  41.         this.startBoundary = playIntro?0:introBoundary;
  42.         this.introBoundary = introBoundary;
  43.         this.loopBoundary = loopBoundary;
  44.     }
  45.     //play from startBoundary for intro, then loop
  46.     public IntroLoop(AudioSource source, float startBoundary, float introBoundary, float loopBoundary) {
  47.         this.source = source;
  48.         this.startBoundary = startBoundary;
  49.         this.introBoundary = introBoundary;
  50.         this.loopBoundary = loopBoundary;
  51.     }
  52.     //call to start
  53.     public void start() { this.source.time = this.startBoundary; this.source.Play(); }
  54.     //call every frame
  55.     public void checkTime() {
  56.         Debug.Log(this.source.time);
  57.         if (this.source.time >= this.loopBoundary) {
  58.             if (!this.playOnce) { this.source.time = introBoundary; }
  59.         }
  60.     }
  61.     //call to stop
  62.     public void stop() { this.source.Stop(); }  
  63. }
  64. //The Music IntroLoop Class
  65. /* **************************************************** */
Add Comment
Please, Sign In to add comment