mirror of
https://github.com/0x7c13/Pal3.Unity.git
synced 2026-11-27 18:08:09 +08:00
Add music and sfx volume settings + bug fixes
This commit is contained in:
1 parent
ad3a4a3f5c
commit
7f6f35b63b
7 files changed
+148
-37
No files matched your search
@@ -19,6 +19,7 @@ namespace Pal3.Audio
|
||||
using Data;
|
||||
using MetaData;
|
||||
using Scene;
|
||||
using Settings;
|
||||
using State;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -31,17 +32,20 @@ namespace Pal3.Audio
|
||||
ICommandExecutor<StopMusicCommand>,
|
||||
ICommandExecutor<ScenePreLoadingNotification>,
|
||||
ICommandExecutor<ScenePostLoadingNotification>,
|
||||
ICommandExecutor<ResetGameStateCommand>
|
||||
ICommandExecutor<ResetGameStateCommand>,
|
||||
ICommandExecutor<SettingChangedNotification>
|
||||
{
|
||||
public float DefaultMusicVolume { get; set; } = 0.45f;
|
||||
public float DefaultSoundVolume { get; set; } = 0.65f;
|
||||
|
||||
private Camera _mainCamera;
|
||||
private AudioSource _musicPlayer;
|
||||
private GameResourceProvider _resourceProvider;
|
||||
private SceneManager _sceneManager;
|
||||
private SettingsManager _settingsManager;
|
||||
|
||||
private const string STOP_MUSIC_NAME = "NONE";
|
||||
|
||||
private float _musicVolume;
|
||||
private float _sfxVolume;
|
||||
|
||||
private string _currentMusicClipName = string.Empty;
|
||||
private string _currentScriptMusic = string.Empty;
|
||||
private readonly List<string> _playingSfxSourceNames = new ();
|
||||
@@ -55,12 +59,17 @@ namespace Pal3.Audio
|
||||
public void Init(Camera mainCamera,
|
||||
GameResourceProvider resourceProvider,
|
||||
SceneManager sceneManager,
|
||||
AudioSource musicSource)
|
||||
AudioSource musicSource,
|
||||
SettingsManager settingsManager)
|
||||
{
|
||||
_mainCamera = mainCamera != null ? mainCamera : throw new ArgumentNullException(nameof(mainCamera));
|
||||
_resourceProvider = resourceProvider ?? throw new ArgumentNullException(nameof(resourceProvider));
|
||||
_sceneManager = sceneManager ?? throw new ArgumentNullException(nameof(sceneManager));
|
||||
_musicPlayer = musicSource != null ? musicSource : throw new ArgumentNullException(nameof(musicSource));
|
||||
_settingsManager = settingsManager ?? throw new ArgumentNullException(nameof(settingsManager));
|
||||
|
||||
_musicVolume = _settingsManager.MusicVolume;
|
||||
_sfxVolume = _settingsManager.SfxVolume;
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
@@ -73,6 +82,19 @@ namespace Pal3.Audio
|
||||
CommandExecutorRegistry<ICommand>.Instance.UnRegister(this);
|
||||
}
|
||||
|
||||
public void Execute(SettingChangedNotification command)
|
||||
{
|
||||
if (command.SettingName == nameof(_settingsManager.MusicVolume))
|
||||
{
|
||||
_musicVolume = _settingsManager.MusicVolume;
|
||||
_musicPlayer.volume = _musicVolume;
|
||||
}
|
||||
else if (command.SettingName == nameof(_settingsManager.SfxVolume))
|
||||
{
|
||||
_sfxVolume = _settingsManager.SfxVolume;
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator PlaySceneMusicAsync(string cityName, string sceneName)
|
||||
{
|
||||
var key = $"{cityName}_{sceneName}";
|
||||
@@ -240,7 +262,7 @@ namespace Pal3.Audio
|
||||
if (musicClip == null ||
|
||||
!string.Equals(_currentMusicClipName, musicName, StringComparison.OrdinalIgnoreCase)) yield break;
|
||||
|
||||
yield return PlayAudioClipAsync(_musicPlayer, musicClip, loopCount, 0f, DefaultMusicVolume,
|
||||
yield return PlayAudioClipAsync(_musicPlayer, musicClip, loopCount, 0f, _musicVolume,
|
||||
new CancellationToken(false)); // Should not stop music during scene switch
|
||||
}
|
||||
|
||||
@@ -315,7 +337,7 @@ namespace Pal3.Audio
|
||||
_resourceProvider.GetSfxFilePath(sfxName),
|
||||
sfxName, // use sfx name as audio source name
|
||||
loopCount: -1, // loop indefinitely
|
||||
DefaultSoundVolume,
|
||||
_sfxVolume,
|
||||
interval: 0f,
|
||||
_sceneAudioCts.Token));
|
||||
break;
|
||||
@@ -335,7 +357,7 @@ namespace Pal3.Audio
|
||||
sfxFilePath,
|
||||
sfxName, // use sfx name as audio source name
|
||||
loopCount,
|
||||
DefaultSoundVolume,
|
||||
_sfxVolume,
|
||||
interval: 0f,
|
||||
cancellationToken));
|
||||
break;
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Copyright (c) 2021-2023, Jiaqi Liu. All rights reserved.
|
||||
// See LICENSE file in the project root for license information.
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
|
||||
namespace Pal3.Command.InternalCommands
|
||||
{
|
||||
public class SettingChangedNotification : ICommand
|
||||
{
|
||||
public SettingChangedNotification(string settingName)
|
||||
{
|
||||
SettingName = settingName;
|
||||
}
|
||||
|
||||
public string SettingName { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 563cdf9c0046474190dbcca8b8e31578
|
||||
timeCreated: 1679231726
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
namespace Pal3
|
||||
{
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Actor;
|
||||
@@ -105,6 +106,7 @@ namespace Pal3
|
||||
private readonly TextureCache _textureCache = new ();
|
||||
|
||||
// Core game systems
|
||||
private SettingsManager _settingsManager;
|
||||
private ICpkFileSystem _fileSystem;
|
||||
private GameResourceProvider _gameResourceProvider;
|
||||
private FileSystemCacheManager _fileSystemCacheManager;
|
||||
@@ -150,10 +152,9 @@ namespace Pal3
|
||||
private MazeSkipper _mazeSkipper;
|
||||
private StorySelector _storySelector;
|
||||
|
||||
private SettingsManager _settingsManager;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_settingsManager = ServiceLocator.Instance.Get<SettingsManager>();
|
||||
_fileSystem = ServiceLocator.Instance.Get<ICpkFileSystem>();
|
||||
_gameResourceProvider = ServiceLocator.Instance.Get<GameResourceProvider>();
|
||||
_gameResourceProvider.UseTextureCache(_textureCache);
|
||||
@@ -237,7 +238,8 @@ namespace Pal3
|
||||
_audioManager.Init(mainCamera,
|
||||
_gameResourceProvider,
|
||||
_sceneManager,
|
||||
musicSource);
|
||||
musicSource,
|
||||
_settingsManager);
|
||||
ServiceLocator.Instance.Register(_audioManager);
|
||||
|
||||
_informationManager = gameObject.AddComponent<InformationManager>();
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace Pal3.Settings
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.NotSupportedException("Unsupported type: " + typeof(T).Name);
|
||||
throw new NotSupportedException("Unsupported type: " + typeof(T).Name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace Pal3.Settings
|
||||
set
|
||||
{
|
||||
if (_vSyncCount != value &&
|
||||
_vSyncCount >= 0)
|
||||
value >= 0)
|
||||
{
|
||||
_vSyncCount = value;
|
||||
NotifyPropertyChanged();
|
||||
@@ -46,7 +46,7 @@ namespace Pal3.Settings
|
||||
set
|
||||
{
|
||||
if (_antiAliasing != value &&
|
||||
_antiAliasing >= 0)
|
||||
value >= 0)
|
||||
{
|
||||
_antiAliasing = value;
|
||||
NotifyPropertyChanged();
|
||||
@@ -61,7 +61,7 @@ namespace Pal3.Settings
|
||||
set
|
||||
{
|
||||
if (_targetFrameRate != value &&
|
||||
_targetFrameRate > 0 || _targetFrameRate == -1)
|
||||
value > 0 || value == -1)
|
||||
{
|
||||
_targetFrameRate = value;
|
||||
NotifyPropertyChanged();
|
||||
@@ -76,7 +76,7 @@ namespace Pal3.Settings
|
||||
set
|
||||
{
|
||||
if (Math.Abs(_resolutionScale - value) > float.Epsilon &&
|
||||
_resolutionScale is > 0 and <= 1.0f)
|
||||
value is > 0 and <= 1.0f)
|
||||
{
|
||||
_resolutionScale = value;
|
||||
NotifyPropertyChanged();
|
||||
@@ -99,6 +99,36 @@ namespace Pal3.Settings
|
||||
}
|
||||
}
|
||||
|
||||
private float _musicVolume = 0.5f; // Default to half volume
|
||||
public float MusicVolume
|
||||
{
|
||||
get => _musicVolume;
|
||||
set
|
||||
{
|
||||
if (Math.Abs(_musicVolume - value) > float.Epsilon &&
|
||||
value is >= 0 and <= 1.0f)
|
||||
{
|
||||
_musicVolume = value;
|
||||
NotifyPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private float _sfxVolume = 0.5f; // Default to half volume
|
||||
public float SfxVolume
|
||||
{
|
||||
get => _sfxVolume;
|
||||
set
|
||||
{
|
||||
if (Math.Abs(_sfxVolume - value) > float.Epsilon &&
|
||||
value is >= 0 and <= 1.0f)
|
||||
{
|
||||
_sfxVolume = value;
|
||||
NotifyPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool _isRealtimeLightingAndShadowsEnabled = false; // Default to false for realtime lighting and shadows
|
||||
public bool IsRealtimeLightingAndShadowsEnabled
|
||||
{
|
||||
|
||||
@@ -9,6 +9,8 @@ namespace Pal3.Settings
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using Command;
|
||||
using Command.InternalCommands;
|
||||
using Core.Utils;
|
||||
using IngameDebugConsole;
|
||||
using MetaData;
|
||||
@@ -36,6 +38,10 @@ namespace Pal3.Settings
|
||||
"设置分辨率缩放设定(0.1:10%分辨率,0.5:50%分辨率,1.0:100%分辨率)", _ => ResolutionScale = _);
|
||||
DebugLogConsole.AddCommand<int>("Settings.FullScreenMode",
|
||||
"设置全屏模式设定(0:Windows独占全屏,1:全屏,2:MacOS全屏,3:窗口", _ => FullScreenMode = (FullScreenMode) _);
|
||||
DebugLogConsole.AddCommand<float>("Settings.MusicVolume",
|
||||
"设置音乐音量设定(0.0:静音,1.0:最大音量)", _ => MusicVolume = _);
|
||||
DebugLogConsole.AddCommand<float>("Settings.SfxVolume",
|
||||
"设置音效音量设定(0.0:静音,1.0:最大音量)", _ => SfxVolume = _);
|
||||
DebugLogConsole.AddCommand<bool>("Settings.IsRealtimeLightingAndShadowsEnabled",
|
||||
"设置实时光照和阴影设定(true:开启,false:关闭)", _ => IsRealtimeLightingAndShadowsEnabled = _);
|
||||
DebugLogConsole.AddCommand<bool>("Settings.IsAmbientOcclusionEnabled",
|
||||
@@ -55,52 +61,61 @@ namespace Pal3.Settings
|
||||
|
||||
private void OnPropertyChanged(object sender, PropertyChangedEventArgs args)
|
||||
{
|
||||
if (args.PropertyName == nameof(VSyncCount))
|
||||
string settingName = args.PropertyName;
|
||||
|
||||
CommandDispatcher<ICommand>.Instance.Dispatch(new SettingChangedNotification(settingName));
|
||||
|
||||
if (settingName == nameof(VSyncCount))
|
||||
{
|
||||
QualitySettings.vSyncCount = VSyncCount;
|
||||
_settingsStore.Set(args.PropertyName, VSyncCount);
|
||||
_settingsStore.Set(settingName, VSyncCount);
|
||||
}
|
||||
else if (args.PropertyName == nameof(AntiAliasing))
|
||||
else if (settingName == nameof(AntiAliasing))
|
||||
{
|
||||
QualitySettings.antiAliasing = AntiAliasing;
|
||||
_settingsStore.Set(args.PropertyName, AntiAliasing);
|
||||
_settingsStore.Set(settingName, AntiAliasing);
|
||||
}
|
||||
else if (args.PropertyName == nameof(TargetFrameRate))
|
||||
else if (settingName == nameof(TargetFrameRate))
|
||||
{
|
||||
Application.targetFrameRate = TargetFrameRate;
|
||||
_settingsStore.Set(args.PropertyName, TargetFrameRate);
|
||||
_settingsStore.Set(settingName, TargetFrameRate);
|
||||
}
|
||||
else if (args.PropertyName == nameof(ResolutionScale))
|
||||
else if (settingName == nameof(ResolutionScale))
|
||||
{
|
||||
Screen.SetResolution(
|
||||
(int) (Screen.currentResolution.width * ResolutionScale),
|
||||
(int) (Screen.currentResolution.height * ResolutionScale),
|
||||
Screen.fullScreenMode);
|
||||
_settingsStore.Set(args.PropertyName, ResolutionScale);
|
||||
_settingsStore.Set(settingName, ResolutionScale);
|
||||
}
|
||||
else if (args.PropertyName == nameof(FullScreenMode))
|
||||
else if (settingName == nameof(FullScreenMode))
|
||||
{
|
||||
Screen.fullScreenMode = FullScreenMode;
|
||||
_settingsStore.Set(args.PropertyName, FullScreenMode);
|
||||
_settingsStore.Set(settingName, FullScreenMode);
|
||||
}
|
||||
else if (args.PropertyName == nameof(IsRealtimeLightingAndShadowsEnabled))
|
||||
else if (settingName == nameof(MusicVolume))
|
||||
{
|
||||
// TODO: Implement this
|
||||
_settingsStore.Set(args.PropertyName, IsRealtimeLightingAndShadowsEnabled);
|
||||
_settingsStore.Set(settingName, MusicVolume);
|
||||
}
|
||||
else if (args.PropertyName == nameof(IsAmbientOcclusionEnabled))
|
||||
else if (settingName == nameof(SfxVolume))
|
||||
{
|
||||
// TODO: Implement this
|
||||
_settingsStore.Set(args.PropertyName, IsAmbientOcclusionEnabled);
|
||||
_settingsStore.Set(settingName, SfxVolume);
|
||||
}
|
||||
else if (args.PropertyName == nameof(IsVoiceOverEnabled))
|
||||
else if (settingName == nameof(IsRealtimeLightingAndShadowsEnabled))
|
||||
{
|
||||
// TODO: Implement this
|
||||
_settingsStore.Set(args.PropertyName, IsVoiceOverEnabled);
|
||||
_settingsStore.Set(settingName, IsRealtimeLightingAndShadowsEnabled);
|
||||
}
|
||||
else if (args.PropertyName == nameof(GameDataFolderPath))
|
||||
else if (settingName == nameof(IsAmbientOcclusionEnabled))
|
||||
{
|
||||
_settingsStore.Set(args.PropertyName, GameDataFolderPath);
|
||||
_settingsStore.Set(settingName, IsAmbientOcclusionEnabled);
|
||||
}
|
||||
else if (settingName == nameof(IsVoiceOverEnabled))
|
||||
{
|
||||
_settingsStore.Set(settingName, IsVoiceOverEnabled);
|
||||
}
|
||||
else if (settingName == nameof(GameDataFolderPath))
|
||||
{
|
||||
_settingsStore.Set(settingName, GameDataFolderPath);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,6 +195,26 @@ namespace Pal3.Settings
|
||||
: FullScreenMode.FullScreenWindow;
|
||||
}
|
||||
|
||||
if (_settingsStore.TryGet(nameof(MusicVolume), out float musicVolume))
|
||||
{
|
||||
MusicVolume = musicVolume;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 50% music volume by default
|
||||
MusicVolume = 0.5f;
|
||||
}
|
||||
|
||||
if (_settingsStore.TryGet(nameof(SfxVolume), out float sfxVolume))
|
||||
{
|
||||
SfxVolume = sfxVolume;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 50% sfx volume by default
|
||||
SfxVolume = 0.5f;
|
||||
}
|
||||
|
||||
if (_settingsStore.TryGet(nameof(IsRealtimeLightingAndShadowsEnabled),
|
||||
out bool isRealtimeLightingAndShadowsEnabled))
|
||||
{
|
||||
@@ -247,6 +282,8 @@ namespace Pal3.Settings
|
||||
|
||||
public void PrintSettings()
|
||||
{
|
||||
Debug.Log("Current game settings:");
|
||||
|
||||
foreach (PropertyInfo property in typeof(SettingsBase).GetProperties())
|
||||
{
|
||||
Debug.Log($"{property.Name}: {property.GetValue(this)}");
|
||||
|
||||
Reference in new issue
Block a user