RAYSER進捗(20230923)
本日はVContainerを用いて、BGMの音量設定のプロトタイプを作っていましたが、シーンにまたがるようにする処理がまだ実現できておらず、引き続き調査しようと思います。
using _RAYSER.Scripts.VContainer;
using UnityEngine;
using VContainer;
using VContainer.Unity;
namespace BGM.Volume
{
/// <summary>
/// 音量受信 タイトルシーンのGameObjectにアタッチ
/// </summary>
public class VolumeSceneTitleLifetimeScope : RootLifetimeScope
{
[SerializeField] private VolumeSlider _volumeSlider;
[SerializeField] private AudioSource audioSource;
protected override void Configure(IContainerBuilder builder)
{
base.Configure(builder);
builder.Register<VolumeService>(Lifetime.Singleton);
builder.Register<VolumeSceneTitlePresenter>(Lifetime.Singleton);
builder.RegisterEntryPoint<VolumeSceneTitlePresenter>();
builder.RegisterComponent(_volumeSlider);
builder.RegisterInstance(audioSource);
}
}
}
using VContainer.Unity;
namespace BGM.Volume
{
/// <summary>
/// 音量調整プレゼンター
/// </summary>
public class VolumeSceneTitlePresenter : IStartable
{
readonly VolumeService volumeService;
readonly VolumeSlider volumeSlider;
public VolumeSceneTitlePresenter(VolumeService volumeService, VolumeSlider volumeSlider)
{
this.volumeService = volumeService;
this.volumeSlider = volumeSlider;
}
void IStartable.Start()
{
// スライダーの値をVolumeServiceに渡す
volumeSlider.slider.onValueChanged.AddListener(volumeService.SetVolume);
}
}
}
using UnityEngine;
using VContainer;
namespace BGM.Volume
{
/// <summary>
/// 音量管理サービスクラス
/// </summary>
public class VolumeService
{
private VolumeData _volumeData;
private AudioSource _audioSource;
[Inject]
public void Construct(
VolumeData volumeData,
AudioSource audioSource
)
{
_volumeData = volumeData;
_audioSource = audioSource;
}
public float GetVolume()
{
return _volumeData.GetVolume();
}
public void SetVolume(float volume)
{
_volumeData.SetVolume(volume);
_audioSource.volume = volume;
}
}
}
namespace BGM.Volume
{
/// <summary>
/// 音量管理クラス
/// </summary>
public class VolumeData
{
private float Volume { get; set; } = 0.5f;
public float GetVolume()
{
return Volume;
}
public void SetVolume(float volume)
{
Volume = volume;
}
}
}