EffectSound.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using UnityEngine;
  2. using System.Collections;
  3. public class EffectSound : MonoBehaviour {
  4. public GameObject effectSoundShotPrefab;
  5. //Battle
  6. public AudioClip explode;
  7. private float volume = 1;
  8. //Use this for initialization
  9. void Awake ()
  10. {
  11. GameObject.DontDestroyOnLoad(this.gameObject);
  12. }
  13. public void Play(AudioClip clip, float volume=1f, float duration=float.MaxValue)
  14. {
  15. if(clip == null || this.volume == 0)
  16. return;
  17. GameObject soundObj = Instantiate(effectSoundShotPrefab) as GameObject;
  18. AudioSource audioSource = soundObj.GetComponent<AudioSource>();
  19. audioSource.volume = volume;
  20. audioSource.clip = clip;
  21. audioSource.Play();
  22. soundObj.GetComponent<EffectSoundShoot>().duration = duration;
  23. }
  24. public void Play(string path, float volume=1f, float duration=float.MaxValue)
  25. {
  26. AudioClip clip = Resources.Load<AudioClip>("AudioClip/"+path);
  27. Play (clip, volume, duration);
  28. }
  29. public void SetVolume(float volume)
  30. {
  31. this.volume = volume;
  32. }
  33. public float GetVolume()
  34. {
  35. return this.volume;
  36. }
  37. }