The problem of playing audio array in Unity3D

There is a problem in today's work. An audio array audioclip [] is loaded in unity3d. How to play this audio array in turn? At present, three schemes are proposed. Write them down and see if there are any shortcomings. Please point out and improve them
The source code is as follows:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class AnimManager : MonoBehaviour {


    private AudioSource myAudioSource;

    private string resourcePath;

    private UnityEngine.Object[] resObjs;

    private AudioClip[] audios;

    // Use this for initialization
    void Start () {

       if (myAudioSource == null)
        {
            myAudioSource = this.gameObject.AddComponent<AudioSource>();
        }
        resToAudios();

    }

     //Play the audio according to the input index. This is the second method
     #regionThe second
     private void PlayAudio(int index)
     {
        //I am too lazy to write an index judgment for the sake of strict procedure  
         myAudioSource.clip = null;

        myAudioSource.clip = audios[index];

        myAudioSource.Play();

       // myAudioSource.PlayOneShot(audios[index]);

         myAudioSource.loop = false;

        currentIndex++;
     }

    //A temporary index
    private int currentIndex = 0;
    //How to play
    public void PlayAudio()
    {
        if (!myAudioSource.isPlaying)
        {
            PlayAudio(currentIndex);
        }
    }
     //void Update()
    //{
    //    PlayAudio();
    //}
    #endregion

    //Load resources under resources path. Here is just a demonstration under resources path. The specific way to load resources is not discussed here
    private void LoadAllAudios()
    {
        resourcePath = SceneManager.GetActiveScene().name;
        resObjs = Resources.LoadAll(resourcePath,typeof(AudioClip));
    }
   //Convert the loaded resource obj to the type of audioclip you want to obtain. It's better to write it as a generic type here. The specific method will not be described in detail
    private void resToAudios()
    {
        LoadAllAudios();
        audios = new AudioClip[resObjs.Length];
        for (int i = 0; i < resObjs.Length; i++)
        {

            audios[i] = resObjs[i] as AudioClip;
        }
    }

    //This is the first way to load with a coroutine,
    #region
    IEnumerator IEPlayAudio(int index)
    {
        if (index >= audios.Length)
        {
            index = audios.Length - 1;
        }
        currentIndex = index;

        myAudioSource.clip = null;

        myAudioSource.clip = audios[currentIndex];

        myAudioSource.Play();

        // myAudioSource.PlayOneShot(audios[index]);

        myAudioSource.loop = false;

        yield return new WaitForSeconds(myAudioSource.clip.length);

        currentIndex++;
        if (currentIndex != audios.Length)
        {
           //Recursive playback
            StartCoroutine(IEPlayAudio(currentIndex));
        }
        else
        {
            StopAllCoroutines();
        }
    }



    public void SelectedAudio(int index)
    {
        StopAllCoroutines();
        StartCoroutine(IEPlayAudio(index));
    }
    #endregion
}

The third way is to read the configuration table. You need to fill in the configuration table, make a timer, or get the frame number of animation playback, and play the audio in the frame or second. There are many ways to read the table, which will not be repeated here. If you have any questions, you can use Q me 599580970

Posted by LordTyphon on Sat, 30 May 2020 22:45:04 -0700