Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. /// <summary>
  2. /// Instantiate a new sound from a prefab into the scene, also calls passed in UnityAction when audio has finished playing
  3. /// </summary>
  4. public GameObject InstantiateSound(GameObject soundPrefab, GameObject destinationObject = null, UnityAction finishedPlayingCallback = null)
  5. {
  6. //Null check
  7. if (!soundPrefab) return null;
  8.  
  9. GameObject sound = Instantiate(soundPrefab) as GameObject;
  10. AudioOptions endOfAudioStateTrigger = sound.GetComponent<AudioOptions>();
  11. if (!endOfAudioStateTrigger)
  12. {
  13. Debug.LogError("Error: Please add a EndOfAudioStateTrigger component to " + sound.name);
  14. return null;
  15. }
  16.  
  17. //Setup event and callback function
  18. endOfAudioStateTrigger.finishedPlayingEvent = new UnityEvent();
  19. endOfAudioStateTrigger.finishedPlayingEvent.AddListener(finishedPlayingCallback);
  20.  
  21. if (destinationObject)
  22. {
  23. sound.transform.parent = destinationObject.transform;
  24. sound.transform.position = destinationObject.transform.position;
  25. }
  26. else {
  27. sound.transform.parent = this.transform;
  28. sound.transform.position = this.transform.position;
  29. }
  30.  
  31. sound.name = soundPrefab.name;
  32.  
  33. return sound;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement