2024-08-11 07:20:58 +08:00
|
|
|
/*
|
|
|
|
* author: mark joshwel
|
|
|
|
* date: 11/8/2024
|
|
|
|
* description: option menu script for handling credits menu button functions
|
|
|
|
*/
|
|
|
|
|
2024-08-11 16:27:33 +08:00
|
|
|
using Unity.Mathematics;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Audio;
|
|
|
|
using UnityEngine.Serialization;
|
2024-08-11 07:20:58 +08:00
|
|
|
using UnityEngine.UIElements;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// class managing the credits menu and button function invocations
|
|
|
|
/// </summary>
|
|
|
|
public class ScreenOptionsMenu : CommonMenu
|
|
|
|
{
|
2024-08-11 16:27:33 +08:00
|
|
|
public AudioMixer mixer;
|
|
|
|
|
2024-08-11 07:20:58 +08:00
|
|
|
/// <summary>
|
|
|
|
/// button to return to the main menu
|
|
|
|
/// </summary>
|
|
|
|
public Button ButtonReturn;
|
|
|
|
|
2024-08-11 14:52:08 +08:00
|
|
|
/// <summary>
|
|
|
|
/// slider for master (music + sfx) volume
|
|
|
|
/// </summary>
|
|
|
|
public Slider SliderAudioMaster;
|
|
|
|
|
2024-08-11 07:20:58 +08:00
|
|
|
/// <summary>
|
|
|
|
/// slider for music volume
|
|
|
|
/// </summary>
|
|
|
|
public Slider SliderAudioMusic;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// slider for sfx volume
|
|
|
|
/// </summary>
|
|
|
|
public Slider SliderAudioSfx;
|
2024-08-11 15:51:21 +08:00
|
|
|
|
2024-08-11 07:20:58 +08: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
|
|
|
|
ButtonReturn = UI.Q<Button>("ButtonReturn");
|
|
|
|
ButtonReturn.clicked += PlayClick;
|
2024-08-11 15:51:21 +08:00
|
|
|
ButtonReturn.clicked += OverlayPauseMenu.OptionReturnToMainMenu;
|
2024-08-11 07:20:58 +08:00
|
|
|
|
|
|
|
// get the music slider from the ui root
|
2024-08-11 14:52:08 +08:00
|
|
|
SliderAudioMaster = UI.Q<Slider>("MasterSlider");
|
|
|
|
// TODO: and set the initial value to the current music volume
|
|
|
|
// SliderAudioMusic.value = Audio.GetMusicVolume() * 100;
|
|
|
|
// and subscribe appropriate functions
|
|
|
|
SliderAudioMaster.RegisterCallback<ChangeEvent<float>>(OptionSetMasterVolume);
|
2024-08-11 15:51:21 +08:00
|
|
|
|
2024-08-11 14:52:08 +08:00
|
|
|
// get the music slider from the ui root
|
|
|
|
SliderAudioMusic = UI.Q<Slider>("MusicSlider");
|
2024-08-11 07:20:58 +08:00
|
|
|
// TODO: and set the initial value to the current music volume
|
2024-08-11 16:27:33 +08:00
|
|
|
|
2024-08-11 07:20:58 +08:00
|
|
|
// and subscribe appropriate functions
|
|
|
|
SliderAudioMusic.RegisterCallback<ChangeEvent<float>>(OptionSetMusicVolume);
|
2024-08-11 15:51:21 +08:00
|
|
|
|
2024-08-11 07:20:58 +08:00
|
|
|
// get the sfx slider from the ui root
|
2024-08-11 14:52:08 +08:00
|
|
|
SliderAudioSfx = UI.Q<Slider>("SFXSlider");
|
2024-08-11 07:20:58 +08:00
|
|
|
// TODO: and set the initial value to the current sfx volume
|
|
|
|
// SliderAudioSfx.value = Audio.GetSfxVolume() * 100;
|
|
|
|
// and subscribe appropriate functions
|
|
|
|
SliderAudioSfx.RegisterCallback<ChangeEvent<float>>(OptionSetSfxVolume);
|
|
|
|
}
|
|
|
|
|
2024-08-11 16:27:33 +08:00
|
|
|
public void Start()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private static float ConvertVolume(float linearVolume)
|
|
|
|
{
|
|
|
|
return Mathf.Log10(math.max(0.0000001f, linearVolume / 100)) * 20;
|
|
|
|
}
|
|
|
|
|
2024-08-11 14:52:08 +08: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 16:27:33 +08:00
|
|
|
mixer.SetFloat("MasterVolume", ConvertVolume(evt.newValue));
|
2024-08-11 14:52:08 +08:00
|
|
|
}
|
2024-08-11 15:51:21 +08:00
|
|
|
|
2024-08-11 07:20:58 +08:00
|
|
|
/// <summary>
|
|
|
|
/// handle music volume slider change,
|
|
|
|
/// sets the music channel volume in the audio manager appropriately
|
|
|
|
/// </summary>
|
2024-08-11 14:52:08 +08:00
|
|
|
/// <param name="evt">change event</param>
|
2024-08-11 07:20:58 +08:00
|
|
|
private void OptionSetMusicVolume(ChangeEvent<float> evt)
|
|
|
|
{
|
2024-08-11 16:27:33 +08:00
|
|
|
mixer.SetFloat("MusicVolume", ConvertVolume(evt.newValue));
|
2024-08-11 07:20:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// handle sfx volume slider change,
|
|
|
|
/// sets the sfx channel volume in the audio manager appropriately
|
|
|
|
/// </summary>
|
2024-08-11 14:52:08 +08:00
|
|
|
/// <param name="evt">change event</param>
|
2024-08-11 07:20:58 +08:00
|
|
|
private void OptionSetSfxVolume(ChangeEvent<float> evt)
|
|
|
|
{
|
2024-08-11 16:27:33 +08:00
|
|
|
mixer.SetFloat("SFXVolume", ConvertVolume(evt.newValue));
|
2024-08-11 07:20:58 +08:00
|
|
|
}
|
|
|
|
}
|