2

There might be similar questions to this one but I couldn't find any useful.

I have a simple scrips which has a list of classes that contains audio clips, id, and event system individually for each one of those elements.

I need to make that audio clip fields to list and make them play by order until the last one then stop and fire event.

Here's my code for only one audio clip slot: (Also i tried audio clip array but its just playing the first one on the audio clip array)

public class MainAudioManager : MonoBehaviour {


public List<soundInfo> currentsoundinfoList = new List<soundInfo>();

AudioSource audioSource;

soundInfo curentSoundInfo;

float audioLenght;


void Start () {


    audioSource = gameObject.GetComponent<AudioSource> ();

}


public void SetAudioToPlay (int ID) {


    for (int i = 0; i < currentsoundinfoList.Count; i++) {

        if (currentsoundinfoList [i].ID == ID) {

            curentSoundInfo = currentsoundinfoList[i];

            audioSource.clip = curentSoundInfo.clipToPlay;

            audioSource.Play ();

            audioLenght = audioSource.clip.length;

            StartCoroutine ("waitnigTillEbd");


            return;

        }

    }


}


IEnumerator waitnigTillEbd () {



    yield return new WaitForSeconds (audioLenght + 1f);

    curentSoundInfo.eventOnSound.Invoke ();

}


[System.Serializable]

public class soundInfo {


    public string name;

    public int ID;

    public AudioClip clipToPlay;

    public UnityEvent eventOnSound;

}

}

pourya011
  • 91
  • 2
  • 13
  • What is the even connected to? – user14492 Dec 05 '19 at 03:21
  • Do you mean event? That is the event system of unity which will fire events in run time in specific times , in my case it will fire on each listofclips end iteration (list of audios played compeletly) – pourya011 Dec 05 '19 at 07:12

1 Answers1

1

alright i found the solution by my self. here is the final result for creating list of sound with event onstop attached to each one of them individually and also playable by ID from another event and classes ,for anyone who need it

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.Events;


[RequireComponent(typeof(AudioSource))]

public class MainAudioManager : MonoBehaviour {


public List<soundInfo> soundList = new List<soundInfo>();

AudioSource audioSource;

soundInfo curentSoundInfo;

float audioLenght;


void Start () {


    audioSource = gameObject.GetComponent<AudioSource> ();

}


public void SetAudioToPlay (int ID) {



        for (int i = 0; i < soundList.Count; i++) {


            if (soundList [i].ID == ID) {

                curentSoundInfo = soundList [i];

                StartCoroutine(playSequencely());

                return;

        }

    }

}

  IEnumerator playSequencely () {


    yield return null;


    for (int cnt = 0; cnt < curentSoundInfo.clipsToPlay.Length; cnt++) {


        audioSource.clip = curentSoundInfo.clipsToPlay [cnt];

        audioSource.Play ();


        while (audioSource.isPlaying) {


            yield return null;

        }


        }

    //Debug.Log ("Last Audio Is Playing");

    curentSoundInfo.onStop.Invoke ();

    }

} 


[System.Serializable]

public class soundInfo {


    public string name;

    public int ID;

    [TextArea (2, 8)] public string About;

    public AudioClip[] clipsToPlay;

    //public float delayBetween;

    public UnityEvent onStop;

}
pourya011
  • 91
  • 2
  • 13