Space Ad Mobile 9.5.1
Space Ad Mobile makes ads easy to add into your mobile game: one unified workflow for LevelPlay, AdMob, Yandex.Ads across banners, interstitials and rewarded.
Loading...
Searching...
No Matches
FullscreenAdAudioController.cs
1using System;
2using System.Collections.Generic;
3using UnityEngine;
4
5namespace SCG.SpaceAdMobile
6{
13 internal static class FullscreenAdAudioController
14 {
15 #region Fields
16
17 private static readonly HashSet<AudioMuteLease> activeLeases = new();
18 private static float previousVolume;
19 private static bool muteApplied;
20
21 #endregion
22
23 #region Lifecycle
24
25 static FullscreenAdAudioController()
26 {
27 SpaceAdMobileBase.ApplicationQuit -= Cleanup;
28 SpaceAdMobileBase.ApplicationQuit += Cleanup;
29 }
30
31 #endregion
32
33 #region Internal API
34
43 internal static IDisposable Acquire(AdUnitBase adUnit)
44 {
45 if (adUnit == null || !Application.isPlaying || !ShouldMute(adUnit))
46 return null;
47
48 var lease = new AudioMuteLease();
49 activeLeases.Add(lease);
50
51 if (activeLeases.Count != 1) return lease;
52 previousVolume = AudioListener.volume;
53 AudioListener.volume = 0f;
54 muteApplied = true;
55
56 return lease;
57 }
58
63 internal static void Cleanup()
64 {
65 activeLeases.Clear();
66 RestoreAudioIfUnused();
67 }
68
69 #endregion
70
71 #region Private Helpers
72
73 private static bool ShouldMute(AdUnitBase adUnit)
74 {
75 var autoMuteAdType = adUnit.AdType.ToAutoMuteAdType();
76 var options = SpaceAdMobileBase.Options;
77 return options != null &&
78 (options.AutoMuteAdTypes & autoMuteAdType) != AutoMuteAdTypes.None;
79 }
80
81 private static void Release(AudioMuteLease lease)
82 {
83 if (!activeLeases.Remove(lease))
84 return;
85
86 RestoreAudioIfUnused();
87 }
88
89 private static void RestoreAudioIfUnused()
90 {
91 if (activeLeases.Count != 0 || !muteApplied)
92 return;
93
94 AudioListener.volume = previousVolume;
95 muteApplied = false;
96 }
97
98 #endregion
99
100 #region Lease
101
105 private sealed class AudioMuteLease : IDisposable
106 {
107 private bool disposed;
108
112 public void Dispose()
113 {
114 if (disposed)
115 return;
116
117 disposed = true;
118 Release(this);
119 }
120 }
121
122 #endregion
123 }
124}
AutoMuteAdTypes
Specifies fullscreen ad formats that mute Unity audio while the ad is displayed.