This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
192 changed files
with
3,785 additions
and
604 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
namespace UDBase.Common { | ||
public interface IScheme { | ||
|
||
void Init(); | ||
|
||
void PostInit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using UnityEngine.Audio; | ||
|
||
namespace UDBase.Controllers.AudioSystem { | ||
public class Audio : ControllerHelper<IAudio> { | ||
|
||
public const string DefaultSoundChannelVolume = "SoundVolume"; | ||
public const string DefaultMusicChannelVolume = "MusicVolume"; | ||
public const string DefaultSoundChannelName = "Sound"; | ||
public const string DefaultMusicChannelName = "Music"; | ||
|
||
public static void MuteChannel(string parameter) { | ||
if ( Instance != null ) { | ||
Instance.MuteChannel(parameter); | ||
} | ||
} | ||
|
||
public static void UnMuteChannel(string parameter) { | ||
if ( Instance != null ) { | ||
Instance.UnMuteChannel(parameter); | ||
} | ||
} | ||
|
||
public static float GetChannelVolume(string parameter) { | ||
if ( Instance != null ) { | ||
return Instance.GetChannelVolume(parameter); | ||
} | ||
return 0.0f; | ||
} | ||
|
||
public static bool IsChannelMuted(string parameter) { | ||
if ( Instance != null ) { | ||
return Instance.IsChannelMuted(parameter); | ||
} | ||
return false; | ||
} | ||
|
||
public static void ToggleChannel(string parameter) { | ||
if ( Instance != null ) { | ||
Instance.ToggleChannel(parameter); | ||
} | ||
} | ||
|
||
public static void SetChannelVolume(string parameter, float normalizedVolume) { | ||
if ( Instance != null ) { | ||
Instance.SetChannelVolume(parameter, normalizedVolume); | ||
} | ||
} | ||
|
||
public static void MuteSound() { | ||
MuteChannel(DefaultSoundChannelVolume); | ||
} | ||
|
||
public static void UnMuteSound() { | ||
UnMuteChannel(DefaultSoundChannelVolume); | ||
} | ||
|
||
public static void ToggleSound() { | ||
ToggleChannel(DefaultSoundChannelVolume); | ||
} | ||
|
||
public static float GetSoundVolume() { | ||
return GetChannelVolume(DefaultSoundChannelVolume); | ||
} | ||
|
||
public static bool IsSoundMuted() { | ||
return IsChannelMuted(DefaultSoundChannelVolume); | ||
} | ||
|
||
public static void SetSoundVolume(float normalizedVolume) { | ||
SetChannelVolume(DefaultSoundChannelVolume, normalizedVolume); | ||
} | ||
|
||
public static void MuteMusic() { | ||
MuteChannel(DefaultMusicChannelVolume); | ||
} | ||
|
||
public static void UnMuteMusic() { | ||
UnMuteChannel(DefaultMusicChannelVolume); | ||
} | ||
|
||
public static void ToggleMusic() { | ||
ToggleChannel(DefaultMusicChannelVolume); | ||
} | ||
|
||
public static float GetMusicVolume() { | ||
return GetChannelVolume(DefaultMusicChannelVolume); | ||
} | ||
|
||
public static bool IsMusicMuted() { | ||
return IsChannelMuted(DefaultMusicChannelVolume); | ||
} | ||
|
||
public static void SetMusicVolume(float normalizedVolume) { | ||
SetChannelVolume(DefaultMusicChannelVolume, normalizedVolume); | ||
} | ||
|
||
public static AudioMixerGroup GetMixerGroup(string channelName) { | ||
if ( Instance != null ) { | ||
return Instance.GetMixerGroup(channelName); | ||
} | ||
return null; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
using UnityEngine; | ||
using UnityEngine.Audio; | ||
using UDBase.Controllers.LogSystem; | ||
using System.Collections.Generic; | ||
using UDBase.Controllers.EventSystem; | ||
using UDBase.Utils; | ||
|
||
namespace UDBase.Controllers.AudioSystem { | ||
public class AudioController : IAudio { | ||
const float MinVolume = -80.0f; | ||
const float MaxVolume = 0.0f; | ||
|
||
readonly string _mixerPath; | ||
readonly float _initialVolume; | ||
readonly string[] _channels; | ||
readonly Dictionary<string, float> _volumes = new Dictionary<string, float>(); | ||
readonly Dictionary<string, bool> _mutes = new Dictionary<string, bool>(); | ||
|
||
readonly Dictionary<string, AudioMixerGroup> _groups = new Dictionary<string, AudioMixerGroup>(); | ||
|
||
AudioMixer _mixer; | ||
|
||
public AudioController(string mixerPath, string[] channels = null, float initialVolume = 0.5f) { | ||
_mixerPath = mixerPath; | ||
_channels = channels; | ||
_initialVolume = initialVolume; | ||
} | ||
|
||
public void Init() { | ||
_mixer = Resources.Load(_mixerPath) as AudioMixer; | ||
if ( _mixer ) { | ||
Log.MessageFormat("AudioMixer loaded from '{0}'", LogTags.Audio, _mixerPath); | ||
} else { | ||
Log.ErrorFormat("AudioMixer not found at '{0}'", LogTags.Audio, _mixerPath); | ||
} | ||
} | ||
|
||
public void PostInit() { | ||
UnityHelper.AddPersistantStartCallback(() => InitializeChannels()); | ||
} | ||
|
||
void InitializeChannels() { | ||
if ( _channels != null ) { | ||
for ( int i = 0; i < _channels.Length; i++ ) { | ||
SetChannelVolume(_channels[i], _initialVolume); | ||
} | ||
} | ||
} | ||
|
||
public void Reset() {} | ||
|
||
public void MuteChannel(string channelParam) { | ||
UpdateMute(channelParam, true); | ||
UpdateChannel(channelParam); | ||
} | ||
|
||
public void UnMuteChannel(string channelParam) { | ||
UpdateMute(channelParam, false); | ||
if ( Mathf.Approximately(FindVolume(channelParam), 0.0f) ) { | ||
ResetVolume(channelParam); | ||
} | ||
UpdateChannel(channelParam); | ||
} | ||
|
||
public float GetChannelVolume(string channelParam) { | ||
return FindMute(channelParam) ? 0.0f : FindVolume(channelParam); | ||
} | ||
|
||
public bool IsChannelMuted(string channelParam) { | ||
return FindMute(channelParam) || Mathf.Approximately(FindVolume(channelParam), 0.0f); | ||
} | ||
|
||
public void ToggleChannel(string channelParam) { | ||
if ( IsChannelMuted(channelParam) ) { | ||
UnMuteChannel(channelParam); | ||
} else { | ||
MuteChannel(channelParam); | ||
} | ||
} | ||
|
||
public void SetChannelVolume(string channelParam, float normalizedVolume) { | ||
UpdateVolume(channelParam, normalizedVolume); | ||
UpdateChannel(channelParam); | ||
} | ||
|
||
void EnsureVolume(string channelParam) { | ||
if ( !_volumes.ContainsKey(channelParam) ) { | ||
var realVolume = 0.0f; | ||
if ( _mixer ) { | ||
_mixer.GetFloat(channelParam, out realVolume); | ||
} | ||
var realVolumeNormalized = Mathf.InverseLerp(MinVolume, MaxVolume, realVolume); | ||
_volumes.Add(channelParam, realVolumeNormalized); | ||
} | ||
} | ||
|
||
void UpdateVolume(string channelParam, float normalizedVolume) { | ||
EnsureVolume(channelParam); | ||
_volumes[channelParam] = normalizedVolume; | ||
} | ||
|
||
float FindVolume(string channelParam) { | ||
EnsureVolume(channelParam); | ||
return _volumes[channelParam]; | ||
} | ||
|
||
void EnsureMute(string channelParam) { | ||
if ( !_mutes.ContainsKey(channelParam) ) { | ||
_mutes.Add(channelParam, false); | ||
} | ||
} | ||
|
||
void UpdateMute(string channelParam, bool mute) { | ||
EnsureMute(channelParam); | ||
_mutes[channelParam] = mute; | ||
} | ||
|
||
bool FindMute(string channelParam) { | ||
EnsureMute(channelParam); | ||
return _mutes[channelParam]; | ||
} | ||
|
||
void ResetVolume(string channelParam) { | ||
_volumes[channelParam] = _initialVolume; | ||
} | ||
|
||
float VolumeDecorator(float normalizedVolume, bool muted) { | ||
return muted ? MinVolume : Mathf.Lerp(MinVolume, MaxVolume, normalizedVolume); | ||
} | ||
|
||
void UpdateChannel(string channelParam) { | ||
var volume = FindVolume(channelParam); | ||
var mute = FindMute(channelParam); | ||
if ( _mixer ) { | ||
_mixer.SetFloat(channelParam, VolumeDecorator(volume, mute)); | ||
} | ||
OnVolumeChanged(channelParam); | ||
} | ||
|
||
void OnVolumeChanged(string channelParam) { | ||
Events.Fire(new VolumeChangeEvent(channelParam, GetChannelVolume(channelParam))); | ||
} | ||
|
||
bool GetMixerGroupFast(string channelName, out AudioMixerGroup result) { | ||
return _groups.TryGetValue(channelName, out result); | ||
} | ||
|
||
public AudioMixerGroup GetMixerGroup(string channelName) { | ||
AudioMixerGroup group; | ||
if ( !GetMixerGroupFast(channelName, out group) ) { | ||
if ( _mixer != null ) { | ||
var results = _mixer.FindMatchingGroups(channelName); | ||
if ( results.Length > 0 ) { | ||
group = results[0]; | ||
} | ||
} | ||
_groups.Add(channelName, group); | ||
} | ||
if ( !group ) { | ||
Log.ErrorFormat("Cannot find channel with name '{0}'", LogTags.Audio, channelName); | ||
} | ||
return group; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Collections.Generic; | ||
using FullSerializer; | ||
|
||
namespace UDBase.Controllers.AudioSystem { | ||
public class AudioSaveNode { | ||
[fsProperty("channels")] | ||
public Dictionary<string, ChannelNode> Channels { get; set; } | ||
} | ||
|
||
public class ChannelNode { | ||
[fsProperty("vol")] | ||
public float Volume { get; set; } | ||
[fsProperty("mute")] | ||
public bool IsMuted { get; set; } | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.