AudioSystem.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.spatialBlend = 1f;
  46. audioS.minDistance = 1f;
  47. audioS.maxDistance = 50f;
  48. AudioClip impactOld = audioS.clip;
  49. if(impact != null) {
  50. audioS.PlayOneShot(impact, volumeScale);
  51. audioS.clip = impactOld??impact;
  52. }
  53. }
  54. }
  55. }