-
Notifications
You must be signed in to change notification settings - Fork 9
/
MuteInBackground.cs
45 lines (40 loc) · 1.46 KB
/
MuteInBackground.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using BepInEx.Configuration;
using BepInEx.Logging;
using UnityEngine;
namespace BepInEx
{
/// <summary>
/// Mute the game in background
/// </summary>
[BepInPlugin(GUID, PluginName, Version)]
public partial class MuteInBackground : BaseUnityPlugin
{
public const string GUID = "BepInEx.MuteInBackground";
public const string PluginName = "Mute In Background";
public const string Version = "1.1";
internal static new ManualLogSource Logger;
internal static float? OriginalVolume = null;
public static ConfigEntry<bool> ConfigMuteInBackground { get; private set; }
internal void Awake()
{
Logger = base.Logger;
ConfigMuteInBackground = Config.Bind("Config", "Mute In Background", false, "Whether to mute the game when in the background, i.e. alt-tabbed.");
}
internal void OnApplicationFocus(bool hasFocus)
{
if (hasFocus)
{
//Restore the original volume if one was previously set
if (OriginalVolume != null)
AudioListener.volume = (float)OriginalVolume;
OriginalVolume = null;
}
else if (ConfigMuteInBackground.Value)
{
//Store the original volume and set the volume to zero
OriginalVolume = AudioListener.volume;
AudioListener.volume = 0;
}
}
}
}