首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按顺序排列的Unity播放列表

按顺序排列的Unity播放列表
EN

Stack Overflow用户
提问于 2017-11-26 16:53:02
回答 2查看 2K关注 0票数 0

我有一个播放列表,可以对歌曲进行混洗,但我想按顺序播放或混洗。如有任何帮助,我们将不胜感激:)

公开课音乐: MonoBehaviour {

代码语言:javascript
复制
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);
}

}

EN

回答 2

Stack Overflow用户

发布于 2017-11-29 02:52:24

我不明白你的问题,不是就这么简单吗?

代码语言:javascript
复制
public class Music : MonoBehaviour 
{
    public AudioClip[] clips;
    private AudioSource audiosource;
    public bool randomPlay = false;
    private int currentClipIndex = 0;

    void Start()
    {
        audiosource = FindObjectOfType<AudioSource>();
        audiosource.loop = false;
    }

    void Update()
    {
        if(!audiosource.isPlaying)
        {
            AudioClip nextClip;
            if (randomPlay)
            {
                nextClip = GetRandomClip();
            }
            else
            {
                nextClip = GetNextClip();
            }
            currentClipIndex = clips.IndexOf(nextClip);
            audiosource.clip = nextClip;
            audiosource.Play();
        }
    }

    private AudioClip GetRandomClip()
    {
        return clips[Random.Range(0, clips.Length)];
    }

    private AudioClip GetNextClip()
    {
        return clips[(currentClipIndex + 1) % clips.Length)];
    }

    private void Awake()
    {
        DontDestroyOnLoad(transform.gameObject);
    }
}
票数 0
EN

Stack Overflow用户

发布于 2018-12-09 00:08:50

之前的答案不起作用。它返回了几个错误。我已经修改了您的脚本,并使用Unity 2018.2.12f1进行了测试。

这应该添加到一个带有音频源组件的空游戏对象中。将音频剪辑拖放到剪辑字段以创建列表。

代码语言:javascript
复制
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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47494447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档