【问题标题】:Unity Playlist in orderUnity 播放列表按顺序排列
【发布时间】:2020-06-06 12:54:14
【问题描述】:

我有一个随机播放歌曲的播放列表,但我想按顺序播放或随机播放。 任何帮助将不胜感激:)

公开课音乐: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);
}

}

【问题讨论】:

  • 如何/何时首先填充剪辑?
  • 回答我自己的问题:只需将曲目拖放到统一编辑器中的数组中

标签: unity5 audio-player


【解决方案1】:

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

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);
    }
}

【讨论】:

    【解决方案2】:

    上一个答案无效。它返回了几个错误。 我已经修改了您的脚本并使用 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 2014-01-05
      相关资源
      最近更新 更多