Skip to content

Commit

Permalink
Merge pull request #5 from baobao/feature/dev
Browse files Browse the repository at this point in the history
Audio settings are now saved locally
  • Loading branch information
baobao authored Jul 5, 2020
2 parents f7917fc + c034af4 commit e1ff3ab
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 10 deletions.
20 changes: 13 additions & 7 deletions Assets/Sample/Sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,22 @@

public class Sample : MonoBehaviour
{
// Start is called before the first frame update
[OnValueChanged("OnValueChangeBgmVolume")] [Range(0, 1f)]public float bgmVolume;
[OnValueChanged("OnValueChangeSeVolume")] [Range(0, 1f)]public float seVolume;
private bool _isSeMute;
private bool _isBgmMute;

void Start()
{
Shibuya24UnityAudioManager.InitializeIfNeed();

// Apply LocalSave
#if ENABLE_LOCALSAVE_SHIBUYA24_AUDIO
bgmVolume = AudioLocalSave.GetVolume(AudioChannel.BGM);
seVolume = AudioLocalSave.GetVolume(AudioChannel.SE);
_isBgmMute = AudioLocalSave.GetMute(AudioChannel.BGM);
_isSeMute = AudioLocalSave.GetMute(AudioChannel.SE);
#endif
}

// Update is called once per frame
Expand Down Expand Up @@ -53,16 +65,13 @@ void StopBgm()
Shibuya24UnityAudioManager.StopBgm();
}

private bool _isSeMute;

[Button("Mute SE")]
void ToggleSEMute()
{
Shibuya24UnityAudioManager.SetMute(AudioChannel.SE, _isSeMute);
_isSeMute = _isSeMute == false;
}

private bool _isBgmMute;

[Button("Mute BGM")]
void ToggleBGMMute()
Expand All @@ -71,9 +80,6 @@ void ToggleBGMMute()
_isBgmMute = _isBgmMute == false;
}

[OnValueChanged("OnValueChangeBgmVolume")] [Range(0, 1f)]public float bgmVolume;
[OnValueChanged("OnValueChangeSeVolume")] [Range(0, 1f)]public float seVolume;

private void OnValueChangeBgmVolume()
{
Debug.Log($"OnValueChangeBgmVolume : {bgmVolume}");
Expand Down
121 changes: 121 additions & 0 deletions Assets/Shibuya24UnityAudioManager/Runtime/AudioLocalSave.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#if ENABLE_LOCALSAVE_SHIBUYA24_AUDIO
using System;
using UnityEngine;

namespace info.shibuya24.Audio
{
/// <summary>
/// Shibuya24AudioManager Local Save
/// Use PlayerPrefs
/// </summary>
public static class AudioLocalSave
{
private const string BgmVolumeKey = "shibuya24.bgm.volume";
private const string SeVolumeKey = "shibuya24.se.volume";

private const string BgmMuteKey = "shibuya24.bgm.mute";
private const string SeMuteKey = "shibuya24.se.mute";

/// <summary>
/// Save Volume
/// </summary>
public static void SetVolume(AudioChannel ch, float value)
{
switch (ch)
{
case AudioChannel.BGM:
SetFloat(BgmVolumeKey, value);
break;
case AudioChannel.SE:
SetFloat(SeVolumeKey, value);
break;
}
}

/// <summary>
/// Get Volume.
/// </summary>
public static float GetVolume(AudioChannel ch, float defaultValue = 1f)
{
switch (ch)
{
case AudioChannel.BGM:
return GetFloat(BgmVolumeKey, defaultValue);
case AudioChannel.SE:
return GetFloat(SeVolumeKey, defaultValue);
}

Debug.LogError($"Invalid key : {ch}");
return 0;
}

/// <summary>
/// Set Mute State.
/// </summary>
public static void SetMute(AudioChannel ch, bool value)
{
switch (ch)
{
case AudioChannel.BGM:
SetBool(BgmMuteKey, value);
break;
case AudioChannel.SE:
SetBool(SeMuteKey, value);
break;
}
}


/// <summary>
/// Get Mute State.
/// </summary>
public static bool GetMute(AudioChannel ch, bool defaultValue = false)
{
switch (ch)
{
case AudioChannel.BGM:
return GetBool(BgmMuteKey, defaultValue);
case AudioChannel.SE:
return GetBool(SeMuteKey, defaultValue);
}

Debug.LogError($"Invalid key : {ch}");
return false;
}

#region PlayerPrefs Wrapper

private static void SetFloat(string key, float value)
{
PlayerPrefs.SetFloat(key, value);
}

private static float GetFloat(string key, float defaultValue = 0)
{
return PlayerPrefs.GetFloat(key, defaultValue);
}

private static void SetInt(string key, int value)
{
PlayerPrefs.SetInt(key, value);
}

private static int GetInt(string key, int defaultValue = 0)
{
return PlayerPrefs.GetInt(key, defaultValue);
}

private static void SetBool(string key, bool value)
{
SetInt(key, value ? 1 : 0);
}

private static bool GetBool(string key, bool defaultValue = false)
{
return GetInt(key, defaultValue ? 1 : 0) != 0;
}

#endregion
}
}
#endif
11 changes: 11 additions & 0 deletions Assets/Shibuya24UnityAudioManager/Runtime/AudioLocalSave.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public enum AudioChannel
/// </summary>
public sealed class Shibuya24UnityAudioManager : MonoBehaviourSingleton<Shibuya24UnityAudioManager>
{
public const string Version = "0.0.2";

/// <summary>
/// BGMの同時再生数は2固定
/// </summary>
Expand Down Expand Up @@ -113,6 +115,20 @@ public static void InitializeIfNeed(AudioSetting setting = null, IAudioResourceL

Loader = loader;

#if ENABLE_LOCALSAVE_SHIBUYA24_AUDIO
#if ENABLE_DEBUG_SHIBUYA24_AUDIO
Debug.Log("Enable AudioLocalSave");
#endif
// Apply Initialize LocalSetting
SetVolume(AudioChannel.BGM, AudioLocalSave.GetVolume(AudioChannel.BGM));
SetVolume(AudioChannel.SE, AudioLocalSave.GetVolume(AudioChannel.SE));
SetMute(AudioChannel.BGM, AudioLocalSave.GetMute(AudioChannel.BGM));
SetMute(AudioChannel.SE, AudioLocalSave.GetMute(AudioChannel.SE));
#else
#if ENABLE_DEBUG_SHIBUYA24_AUDIO
Debug.Log("Disable AudioLocalSave");
#endif
#endif
_isInitialized = true;
}

Expand Down Expand Up @@ -175,6 +191,9 @@ public static async UniTask StopBgm(float duration = 1f)
public static void SetVolume(AudioChannel ch, float volume)
{
if (ch == AudioChannel.None) return;
#if ENABLE_LOCALSAVE_SHIBUYA24_AUDIO
AudioLocalSave.SetVolume(ch, volume);
#endif
// Clamp
volume = Mathf.Clamp01(volume);
var list = PlayingChannelMap[ch];
Expand All @@ -190,6 +209,9 @@ public static void SetVolume(AudioChannel ch, float volume)
public static void SetMute(AudioChannel ch, bool isMute)
{
if (ch == AudioChannel.None) return;
#if ENABLE_LOCALSAVE_SHIBUYA24_AUDIO
AudioLocalSave.SetMute(ch, isMute);
#endif
var list = PlayingChannelMap[ch];
for (int i = 0; i < list.Count; i++)
{
Expand Down Expand Up @@ -269,7 +291,8 @@ private static async UniTask<int> PlayBgm(string bgmPath)

CurrentBgmPath = bgmPath;
#if ENABLE_DEBUG_SHIBUYA24_AUDIO
Debug.Log($"playingId : {playingId}");
var msg = playingId < 0 ? "Not Playing" : $"playingId : {playingId.ToString()}";
Debug.Log($"current Bgm => {msg}");
#endif
if (playingId >= 0)
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/Shibuya24UnityAudioManager/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "info.shibuya24.unity.audiomanager",
"displayName": "Shibuya24UnityAudioManager",
"version": "0.0.1",
"version": "0.0.2",
"unity": "2018.4",
"description": "My Unity Audio Manager.",
"keywords": [ "Unity", "C#", "Audio" ],
Expand Down
3 changes: 2 additions & 1 deletion ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,8 @@ PlayerSettings:
webGLLinkerTarget: 1
webGLThreadsSupport: 0
webGLWasmStreaming: 0
scriptingDefineSymbols: {}
scriptingDefineSymbols:
7: ENABLE_DEBUG_SHIBUYA24_AUDIO;ENABLE_LOCALSAVE_SHIBUYA24_AUDIO
platformArchitecture: {}
scriptingBackend: {}
il2cppCompilerConfiguration: {}
Expand Down

0 comments on commit e1ff3ab

Please sign in to comment.