maxhacker11

SoundManager.cs

Jan 2nd, 2024
2,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.90 KB | Source Code | 0 0
  1. using UnityEngine;
  2.  
  3. public class SoundManager : MonoBehaviour
  4. {
  5.     public static SoundManager Instance;
  6.  
  7.     [SerializeField]
  8.     private SoundLibrary sfxLibrary;
  9.     [SerializeField]
  10.     private AudioSource sfx2DSource;
  11.  
  12.     private void Awake()
  13.     {
  14.         if (Instance != null)
  15.         {
  16.             Destroy(gameObject);
  17.         }
  18.         else
  19.         {
  20.             Instance = this;
  21.             DontDestroyOnLoad(gameObject);
  22.         }
  23.     }
  24.  
  25.     public void PlaySound3D(AudioClip clip, Vector3 pos)
  26.     {
  27.         if (clip != null)
  28.         {
  29.             AudioSource.PlayClipAtPoint(clip, pos);
  30.         }
  31.     }
  32.  
  33.     public void PlaySound3D(string soundName, Vector3 pos)
  34.     {
  35.         PlaySound3D(sfxLibrary.GetClipFromName(soundName), pos);
  36.     }
  37.  
  38.     public void PlaySound2D(string soundName)
  39.     {
  40.         sfx2DSource.PlayOneShot(sfxLibrary.GetClipFromName(soundName));
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment