我知道这很旧,但我想免费分享我的 IntroLoop 课程!默认情况下,此类从头开始播放文件,从 introBoundary 循环到 loopBoundary 就像 5argon 的一样。我添加了一些新功能,编码时间:30 分钟。一,你可以只播放一个循环,没有介绍。二,与默认值相同,但您可以在任何地方开始,而不仅仅是从头开始,并且仍然有 intro 和循环。第三,您可以通过在构造 intro 循环对象后将 IntroLoop.playOnce 设置为 false 来播放从 a 点到 b 点的文件而不循环。因此,现在您可以将所有音频放入一个文件中。
使用示例:将音频附加到脚本对象后,将音频组件拖到公共字段,并发送到构造函数中,您可以从同一个音频文件中制作多个 IntroLoop 对象。此示例将从 5 秒标记开始播放前奏,并从 10 秒标记循环到 20 秒标记:
//Example of use
//Construct
IntroLoop clip = new IntroLoop(audioSource,5f,10f,20f);
//no intro just loop
IntroLoop clip2 = new IntroLoop(audioSource,10f,20f,false);
//you can set it to play once
clip2.playOnce = true;
//call to start
clip.start();
//call once a frame, this resets the loop if the time hits the loop boundary
//or stops playing if playOnce = true
clip.checkTime();
//call to stop
clip.stop();
/* **************************************************** */
//The Music IntroLoop Class handles looping music, and playing an intro to the loop
public class IntroLoop {
private AudioSource source;
private float startBoundary;
private float introBoundary;
private float loopBoundary;
//set to play a clip once
public bool playOnce = false;
//play from start for intro
public IntroLoop(AudioSource source, float introBoundary, float loopBoundary) {
this.source = source;
this.startBoundary = 0;
this.introBoundary = introBoundary;
this.loopBoundary = loopBoundary;
}
//play from start for intro or just loop
public IntroLoop(AudioSource source, float introBoundary, float loopBoundary, bool playIntro) {
this.source = source;
this.startBoundary = playIntro?0:introBoundary;
this.introBoundary = introBoundary;
this.loopBoundary = loopBoundary;
}
//play from startBoundary for intro, then loop
public IntroLoop(AudioSource source, float startBoundary, float introBoundary, float loopBoundary) {
this.source = source;
this.startBoundary = startBoundary;
this.introBoundary = introBoundary;
this.loopBoundary = loopBoundary;
}
//call to start
public void start() { this.source.time = this.startBoundary; this.source.Play(); }
//call every frame
public void checkTime() {
Debug.Log(this.source.time);
if (this.source.time >= this.loopBoundary) {
if (!this.playOnce) { this.source.time = introBoundary; }
}
}
//call to stop
public void stop() { this.source.Stop(); }
}
//The Music IntroLoop Class
/* **************************************************** */