我有一个播放列表可以随机播放歌曲,但是我想按顺序播放它们或者播放它们。 任何帮助将不胜感激:)
公共课音乐:MonoBehaviour {
public AudioClip[] clips;
private AudioSource audiosource;
void Start()
{
audiosource = FindObjectOfType<AudioSource>();
audiosource.loop = false;
}
void Update()
{
if(!audiosource.isPlaying)
{
audiosource.clip = GetRandomClip();
audiosource.Play();
}
}
private AudioClip GetRandomClip()
{
return clips[Random.Range(0, clips.Length)];
}
private void Awake()
{
DontDestroyOnLoad(transform.gameObject);
}
}
答案 0 :(得分:0)
我没有解决你的问题,这不是那么简单吗?
foreach ( var partition in partitions )
{
var filename = string.Format( "file_{0}.xml", partition.Key ));
// write the partition to the file
}
答案 1 :(得分:0)
上一个答案无效。它返回了几个错误。 我已经修改了您的脚本并在Unity 2018.2.12f1中对其进行了测试。
这应该添加到带有音频源组件的空游戏对象中。 将音频剪辑拖放到剪辑字段以创建列表。
public bool randomPlay = false; // checkbox for random play
public AudioClip[] clips;
private AudioSource audioSource;
int clipOrder = 0; // for ordered playlist
void Start () {
audioSource = GetComponent<AudioSource> ();
audioSource.loop = false;
}
void Update () {
if (!audioSource.isPlaying) {
// if random play is selected
if (randomPlay == true) {
audioSource.clip = GetRandomClip ();
audioSource.Play ();
// if random play is not selected
} else {
audioSource.clip = GetNextClip ();
audioSource.Play ();
}
}
}
// function to get a random clip
private AudioClip GetRandomClip () {
return clips[Random.Range (0, clips.Length)];
}
// function to get the next clip in order, then repeat from the beginning of the list.
private AudioClip GetNextClip () {
if (clipOrder >= clips.Length - 1) {
clipOrder = 0;
} else {
clipOrder += 1;
}
return clips[clipOrder];
}
void Awake () {
DontDestroyOnLoad (transform.gameObject);
}