MusicPlayer.cs 761 B

123456789101112131415161718192021222324252627282930
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [RequireComponent(typeof(AudioSource))]
  5. public class MusicPlayer : MonoBehaviour {
  6. private AudioSource m_AudioSource = null;
  7. [SerializeField] private AudioClip m_DefaultAudioClip = null;
  8. private static MusicPlayer m_Instance = null;
  9. public static MusicPlayer Instance {
  10. get { return m_Instance; }
  11. }
  12. void Awake() {
  13. m_Instance = this;
  14. m_AudioSource = GetComponent<AudioSource>();
  15. }
  16. public void PlayMusic() {
  17. m_AudioSource.clip = m_DefaultAudioClip;
  18. m_AudioSource.Play();
  19. }
  20. public void PlayMusic(AudioClip audioClip) {
  21. m_AudioSource.clip = audioClip;
  22. m_AudioSource.Play();
  23. }
  24. }