Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Instantiate a new sound from a prefab into the scene, also calls passed in UnityAction when audio has finished playing
- /// </summary>
- public GameObject InstantiateSound(GameObject soundPrefab, GameObject destinationObject = null, UnityAction finishedPlayingCallback = null)
- {
- //Null check
- if (!soundPrefab) return null;
- GameObject sound = Instantiate(soundPrefab) as GameObject;
- AudioOptions endOfAudioStateTrigger = sound.GetComponent<AudioOptions>();
- if (!endOfAudioStateTrigger)
- {
- Debug.LogError("Error: Please add a EndOfAudioStateTrigger component to " + sound.name);
- return null;
- }
- //Setup event and callback function
- endOfAudioStateTrigger.finishedPlayingEvent = new UnityEvent();
- endOfAudioStateTrigger.finishedPlayingEvent.AddListener(finishedPlayingCallback);
- if (destinationObject)
- {
- sound.transform.parent = destinationObject.transform;
- sound.transform.position = destinationObject.transform.position;
- }
- else {
- sound.transform.parent = this.transform;
- sound.transform.position = this.transform.position;
- }
- sound.name = soundPrefab.name;
- return sound;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement