1
This commit is contained in:
119
unity/Assets/Script/Managers/SoundManager.cs
Normal file
119
unity/Assets/Script/Managers/SoundManager.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Audio;
|
||||
|
||||
namespace Manager
|
||||
{
|
||||
public class SoundManager : MonoSingleton<SoundManager>
|
||||
{
|
||||
public AudioMixer audioMixer;//»ìÒôÆ÷
|
||||
public AudioSource MusicSource;
|
||||
public AudioSource SoundSource;
|
||||
|
||||
const string MusicPath = "Music/";
|
||||
const string SoundPath = "Sound/";
|
||||
|
||||
private bool musicOn;
|
||||
public bool MusicOn
|
||||
{
|
||||
get { return musicOn; }
|
||||
set
|
||||
{
|
||||
musicOn = value;
|
||||
this.MusicMute(!musicOn);
|
||||
}
|
||||
}
|
||||
private bool soundOn;
|
||||
public bool SoundOn
|
||||
{
|
||||
get { return soundOn; }
|
||||
set
|
||||
{
|
||||
soundOn = value;
|
||||
this.SoundMute(!soundOn);
|
||||
}
|
||||
}
|
||||
|
||||
private int musicVolume;
|
||||
public int MusicVolume
|
||||
{
|
||||
get { return musicVolume; }
|
||||
set
|
||||
{
|
||||
musicVolume = value;
|
||||
if (musicOn) this.SetVolume("MusicVolume", musicVolume);
|
||||
}
|
||||
}
|
||||
private int soundVolume;
|
||||
public int SoundVolume
|
||||
{
|
||||
get { return soundVolume; }
|
||||
set
|
||||
{
|
||||
soundVolume = value;
|
||||
if (soundOn) this.SetVolume("SoundVolume", soundVolume);
|
||||
}
|
||||
}
|
||||
|
||||
public void SoundMute(bool mute)
|
||||
{
|
||||
this.SetVolume("SoundVolume", mute ? 0 : soundVolume);
|
||||
}
|
||||
public void MusicMute(bool mute)
|
||||
{
|
||||
this.SetVolume("MusicVolume", mute ? 0 : musicVolume);
|
||||
}
|
||||
|
||||
void SetVolume(string name, int value)
|
||||
{
|
||||
float volume = value * 0.5f - 50f;
|
||||
this.audioMixer.SetFloat(name, volume);
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
this.MusicVolume = SystemConfig.MusicVolume;
|
||||
this.SoundVolume = SystemConfig.SoundVolume;
|
||||
this.MusicOn = SystemConfig.MusicOn;
|
||||
this.soundOn = SystemConfig.SoundOn;
|
||||
}
|
||||
|
||||
public void PlayMusic(string name)
|
||||
{
|
||||
AudioClip clip = Resloader.Load<AudioClip>(MusicPath + name);
|
||||
if (clip == null)
|
||||
{
|
||||
Debug.LogWarningFormat("PlayMusic:{0} not existed", name);
|
||||
return;
|
||||
}
|
||||
if (MusicSource.isPlaying)
|
||||
{
|
||||
MusicSource.Stop();
|
||||
}
|
||||
MusicSource.clip = clip;
|
||||
MusicSource.Play();
|
||||
}
|
||||
public void PlaySound(string name)
|
||||
{
|
||||
AudioClip clip = Resloader.Load<AudioClip>(SoundPath + name);
|
||||
if (clip == null)
|
||||
{
|
||||
Debug.LogWarningFormat("PlaySound:{0} not existed", name);
|
||||
return;
|
||||
}
|
||||
SoundSource.PlayOneShot(clip);
|
||||
}
|
||||
|
||||
public void PlayEffect(AudioSource audio,AudioClip clip)
|
||||
{
|
||||
if (clip == null)
|
||||
{
|
||||
Debug.LogWarningFormat("PlaySound:{0} not existed", name);
|
||||
return;
|
||||
}
|
||||
audio.PlayOneShot(clip);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user