This repository has been archived on 2024-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
sota/RunningLateGame/Assets/Scripts/ScreenOptionsMenu.cs

108 lines
3.5 KiB
C#
Raw Normal View History

/*
* author: mark joshwel
* date: 11/8/2024
* description: option menu script for handling credits menu button functions
*/
2024-08-11 08:27:33 +00:00
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UIElements;
/// <summary>
/// class managing the credits menu and button function invocations
/// </summary>
public class ScreenOptionsMenu : CommonMenu
{
2024-08-11 08:27:33 +00:00
public AudioMixer mixer;
/// <summary>
/// button to return to the main menu
/// </summary>
2024-08-11 11:57:50 +00:00
private Button _buttonReturn;
2024-08-11 06:52:08 +00:00
/// <summary>
/// slider for master (music + sfx) volume
/// </summary>
2024-08-11 11:57:50 +00:00
private Slider _sliderAudioMaster;
2024-08-11 06:52:08 +00:00
/// <summary>
/// slider for music volume
/// </summary>
2024-08-11 11:57:50 +00:00
private Slider _sliderAudioMusic;
/// <summary>
/// slider for sfx volume
/// </summary>
2024-08-11 11:57:50 +00:00
private Slider _sliderAudioSfx;
public void Start()
{
}
2024-08-11 07:51:21 +00:00
/// <summary>
/// function to associate a display state with the menu,
/// and subscribe button events to their respective functions
/// </summary>
public override void OnEnable()
{
// set the associated state and call the base OnEnable
associatedState = GameManager.DisplayState.ScreenOptionsMenu;
base.OnEnable();
// get the start button from the ui root and subscribe appropriate functions
2024-08-11 11:57:50 +00:00
_buttonReturn = UI.Q<Button>("ButtonReturn");
_buttonReturn.clicked += PlayClick;
_buttonReturn.clicked += OverlayPauseMenu.OptionReturnToMainMenu;
2024-08-11 06:52:08 +00:00
// TODO: and set the initial value to the current music volume
2024-08-11 11:57:50 +00:00
// get the music slider from the ui root and subscribe appropriate functions
_sliderAudioMaster = UI.Q<Slider>("MasterSlider");
_sliderAudioMaster.RegisterCallback<ChangeEvent<float>>(OptionSetMasterVolume);
2024-08-11 07:51:21 +00:00
// TODO: and set the initial value to the current music volume
2024-08-11 11:57:50 +00:00
// get the music slider from the ui root and subscribe appropriate functions
_sliderAudioMusic = UI.Q<Slider>("MusicSlider");
_sliderAudioMusic.RegisterCallback<ChangeEvent<float>>(OptionSetMusicVolume);
2024-08-11 08:27:33 +00:00
// TODO: and set the initial value to the current sfx volume
2024-08-11 11:57:50 +00:00
// get the sfx slider from the ui root and subscribe appropriate functions
_sliderAudioSfx = UI.Q<Slider>("SFXSlider");
_sliderAudioSfx.RegisterCallback<ChangeEvent<float>>(OptionSetSfxVolume);
2024-08-11 08:27:33 +00:00
}
private static float ConvertVolume(float linearVolume)
{
return Mathf.Log10(math.max(0.0000001f, linearVolume / 100)) * 20;
}
2024-08-11 06:52:08 +00:00
/// <summary>
/// handle music volume slider change,
/// sets the music channel volume in the audio manager appropriately
/// </summary>
/// <param name="evt">change event</param>
private void OptionSetMasterVolume(ChangeEvent<float> evt)
{
2024-08-11 08:27:33 +00:00
mixer.SetFloat("MasterVolume", ConvertVolume(evt.newValue));
2024-08-11 06:52:08 +00:00
}
2024-08-11 07:51:21 +00:00
/// <summary>
/// handle music volume slider change,
/// sets the music channel volume in the audio manager appropriately
/// </summary>
2024-08-11 06:52:08 +00:00
/// <param name="evt">change event</param>
private void OptionSetMusicVolume(ChangeEvent<float> evt)
{
2024-08-11 08:27:33 +00:00
mixer.SetFloat("MusicVolume", ConvertVolume(evt.newValue));
}
/// <summary>
/// handle sfx volume slider change,
/// sets the sfx channel volume in the audio manager appropriately
/// </summary>
2024-08-11 06:52:08 +00:00
/// <param name="evt">change event</param>
private void OptionSetSfxVolume(ChangeEvent<float> evt)
{
2024-08-11 08:27:33 +00:00
mixer.SetFloat("SFXVolume", ConvertVolume(evt.newValue));
}
}