LMQT/Assets/Scripts/AudioManager.cs

40 lines
982 B
C#

using UnityEngine;
using UnityEngine.UI;
public class AudioManager : MonoBehaviour
{
public static AudioManager instance;
private float originalVolume;
public Image t;
void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
return;
}
// 保证子物体也不被销毁
foreach (Transform child in transform)
{
DontDestroyOnLoad(child.gameObject); // 对所有子物体调用 DontDestroyOnLoad
}
originalVolume = AudioListener.volume;
}
public void Mute()
{
if (AudioListener.volume != 0)
{
AudioListener.volume = 0f;
t.sprite = Resources.Load<Sprite>("mute");
}
else
{
AudioListener.volume = originalVolume;
t.sprite = Resources.Load<Sprite>("unmute");
}
}
}