AudioSystem.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class AudioSystem : MonoBehaviour {
  5. private static AudioSystem instance;
  6. public static AudioSystem getInstance {
  7. get {
  8. if(instance == null) {
  9. instance = new GameObject("AudioSystem").AddComponent<AudioSystem>();
  10. }
  11. return instance;
  12. }
  13. }
  14. private SCAudiosConfig mSCAudiosConfig;
  15. public SCAudiosConfig SCAudiosConfig {
  16. get {
  17. if(mSCAudiosConfig == null) {
  18. mSCAudiosConfig = Resources.Load<SCAudiosConfig>("Configs/SCAudiosConfig");
  19. }
  20. return mSCAudiosConfig;
  21. }
  22. }
  23. public void PlayAudioOneShot(GameObject target, SCAudiosConfig.AudioType audioType, float volumeScale = 1f) {
  24. if(audioType == SCAudiosConfig.AudioType.Null) {
  25. //Debug.Log("audioType Null");
  26. return;
  27. }
  28. if(SCAudiosConfig == null) {
  29. Debug.Log("SCAudiosConfig Null");
  30. return;
  31. }
  32. if(target) {
  33. AudioClip impact = null;
  34. foreach(var audio in SCAudiosConfig.SCAudioList) {
  35. if(audio.audioType == audioType) {
  36. impact = audio.audioClip;
  37. volumeScale = audio.volume * volumeScale;
  38. break;
  39. }
  40. }
  41. AudioSource audioS = target.GetComponent<AudioSource>();
  42. if(audioS == null) {
  43. audioS = target.AddComponent<AudioSource>();
  44. }
  45. audioS.playOnAwake = false;
  46. audioS.spatialBlend = 1f;
  47. audioS.minDistance = 1f;
  48. audioS.maxDistance = 50f;
  49. AudioClip impactOld = audioS.clip;
  50. if(impact != null) {
  51. audioS.PlayOneShot(impact, volumeScale);
  52. audioS.clip = impactOld??impact;
  53. }
  54. }
  55. }
  56. }