added initial logic for the volume sliders

This commit is contained in:
Timur Nizamov
2025-12-02 22:40:17 +02:00
parent 0b4854373b
commit f414e10d94
43 changed files with 365 additions and 422 deletions

View File

@@ -11,26 +11,42 @@ using STOP_MODE = FMOD.Studio.STOP_MODE;
public class AudioManager : MonoBehaviour
{
//VCAs (VCA buses) are mainly used for volume control
//Regular routing buses routing are for the remaining logic, for example, within the FMOD itself to put FMOD events inside the Mixer (Mixer Routing).
//Regular routing buses are used when we make some attenuation based on parameters and write the logic in code for Unity.
private VCA masterVCA;
private VCA musicVCA;
private VCA sfxVCA;
private VCA uiVCA;
private VCA ambienceVCA;
[SerializeField]
[Header("Volume")]
[Range(0, 1)]
public float MasterVolume = 1;
public float MasterVolume = 0.5f;
[Range(0, 1)]
public float MusicVolume = 1;
public float MusicVolume = 0.5f;
[Range(0, 1)]
public float SFXVolume = 1;
public float AmbienceVolume = 0.5f;
[Range(0, 1)]
public float UIVolume = 1;
public float SFXVolume = 0.5f;
[Range(0, 1)]
public float UIVolume = 0.5f;
[Range(0, 1)]
private Bus masterBus;
private Bus ambientBus;
private Bus musicBus;
private Bus sfxBus;
private Bus reverbBus;
private Bus uiBus;
const string sid = "00000000-0000-0000-0000-000000000000";
static readonly Guid nullGuid = new Guid(sid);
@@ -86,15 +102,47 @@ public class AudioManager : MonoBehaviour
_instance = instance;
DontDestroyOnLoad(_instance.gameObject);
_instance.eventInstances = new List<EventInstance>();
_instance.masterBus = RuntimeManager.GetBus("bus:/");
_instance.musicBus = RuntimeManager.GetBus("bus:/Music");
_instance.ambientBus = RuntimeManager.GetBus("bus:/Ambiences");
_instance.sfxBus = RuntimeManager.GetBus("bus:/SFX");
_instance.uiBus = RuntimeManager.GetBus("bus:/UI");
// _instance.MasterVolume = SaveManager.Instance.systemData.MasterVolume / 100f;
// _instance.SFXVolume = SaveManager.Instance.systemData.SFXVolume / 100f;
// _instance.MusicVolume = SaveManager.Instance.systemData.MusicVolume / 100f;
// _instance.UIVolume = SaveManager.Instance.systemData.UIVolume / 100f;
_instance.masterVCA = RuntimeManager.GetVCA("vca:/Master");
_instance.musicVCA = RuntimeManager.GetVCA("vca:/Music");
_instance.ambienceVCA = RuntimeManager.GetVCA("vca:/Ambiences");
_instance.sfxVCA = RuntimeManager.GetVCA("vca:/SFX");
_instance.uiVCA = RuntimeManager.GetVCA("vca:/UI");
}
public void SetMasterVCA(float value)
{
masterVCA.setVolume(value);
}
public void SetMusicVCA(float value)
{
musicVCA.setVolume(value);
}
public void SetAmbientVCA(float value)
{
ambienceVCA.setVolume(value);
}
public void SetSFXVCA(float value)
{
sfxVCA.setVolume(value);
}
public void SetUIVCA(float value)
{
uiVCA.setVolume(value);
}
private void Awake()
@@ -116,10 +164,19 @@ public class AudioManager : MonoBehaviour
private void Update()
{
musicBus.setVolume(MusicVolume);
sfxBus.setVolume(SFXVolume);
uiBus.setVolume(UIVolume);
masterBus.setVolume(MasterVolume);
//musicBus.setVolume(MusicVolume);
//ambientBus.setVolume(AmbienceVolume);
//sfxBus.setVolume(SFXVolume);
//uiBus.setVolume(UIVolume);
//masterBus.setVolume(MasterVolume);
//VCA volumes
masterVCA.setVolume(MasterVolume);
ambienceVCA.setVolume(AmbienceVolume);
musicVCA.setVolume(MusicVolume);
sfxVCA.setVolume(SFXVolume);
uiVCA.setVolume(UIVolume);
}
public static bool IsEventReferenceValid(EventReference eventReference)
@@ -190,17 +247,6 @@ public class AudioManager : MonoBehaviour
return;
}
musicEventInstance = CreateInstance(musicEventReference);
timelineInfo = new TimelineInfo();
// Explicitly create the delegate object and assign it to a member so it doesn't get freed
// by the garbage collected while it's being used
beatCallback = new EVENT_CALLBACK(BeatEventCallback);
// Pin the class that will store the data modified during the callback
timelineHandle = GCHandle.Alloc(timelineInfo);
// Pass the object through the userdata of the instance
musicEventInstance.setUserData(GCHandle.ToIntPtr(timelineHandle));
musicEventInstance.setCallback(beatCallback, EVENT_CALLBACK_TYPE.TIMELINE_BEAT | EVENT_CALLBACK_TYPE.TIMELINE_MARKER);
}
public void SetMusicParameter(string parameter, float value)
@@ -278,45 +324,6 @@ public class AudioManager : MonoBehaviour
}
}
// taken from https://www.fmod.com/docs/2.02/unity/examples-timeline-callbacks.html
[AOT.MonoPInvokeCallback(typeof(EVENT_CALLBACK))]
static FMOD.RESULT BeatEventCallback(EVENT_CALLBACK_TYPE type, IntPtr instancePtr, IntPtr parameterPtr)
{
EventInstance instance = new(instancePtr);
// Retrieve the user data
FMOD.RESULT result = instance.getUserData(out IntPtr timelineInfoPtr);
if (result != FMOD.RESULT.OK)
{
Debug.LogError("Timeline Callback error: " + result);
}
else if (timelineInfoPtr != IntPtr.Zero)
{
// Get the object to store beat and marker details
GCHandle timelineHandle = GCHandle.FromIntPtr(timelineInfoPtr);
TimelineInfo timelineInfo = (TimelineInfo)timelineHandle.Target;
switch (type)
{
case EVENT_CALLBACK_TYPE.TIMELINE_MARKER:
{
var parameter = (TIMELINE_MARKER_PROPERTIES)Marshal.PtrToStructure(parameterPtr, typeof(TIMELINE_MARKER_PROPERTIES));
timelineInfo.LastMarker = parameter.name;
OnNewBGMMarker?.Invoke(parameter.name);
break;
}
case EVENT_CALLBACK_TYPE.DESTROYED:
{
// Now the event has been destroyed, unpin the timeline memory so it can be garbage collected
timelineHandle.Free();
break;
}
}
}
return FMOD.RESULT.OK;
}
private void OnDestroy()
{
CleanUp();