Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
Merge branch 'v.0.9.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
KonH committed Sep 30, 2017
2 parents 64632a1 + d93eed9 commit f5c516a
Show file tree
Hide file tree
Showing 192 changed files with 3,785 additions and 604 deletions.
1 change: 0 additions & 1 deletion Common/DefaultScheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

namespace UDBase.Common {
public class ProjectScheme : Scheme {

public ProjectScheme() {
AddController(new Log(), new UnityLog());
}
Expand Down
2 changes: 0 additions & 2 deletions Common/IScheme.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
namespace UDBase.Common {
public interface IScheme {

void Init();

void PostInit();
}
}
8 changes: 3 additions & 5 deletions Common/Scheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace UDBase.Common {
/// Note: This class used native Debug messages (Log controller is unreachable yet)
/// </summary>
public abstract class Scheme : IScheme {
Dictionary<IController, ControllerHelperBase> _controllers = new Dictionary<IController, ControllerHelperBase>();
readonly Dictionary<IController, ControllerHelperBase> _controllers =
new Dictionary<IController, ControllerHelperBase>();

public void AddController(ControllerHelperBase helper, params IController[] controllers) {
if( helper == null ) {
Expand All @@ -31,10 +32,7 @@ public void AddController(ControllerHelperBase helper, params IController[] cont
}

public bool HasController(IController controller) {
if( controller != null ) {
return _controllers.ContainsKey(controller);
}
return false;
return (controller != null) && _controllers.ContainsKey(controller);
}

public ControllerHelperBase GetControllerHelper(IController controller) {
Expand Down
1 change: 0 additions & 1 deletion Common/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace UDBase.Common {
public sealed class Startup {

/* Using this method template logics can be loaded on first scene load
* And before any Awake/OnEnable/Start methods are invoked
*/
Expand Down
1 change: 0 additions & 1 deletion Common/UDBaseConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace UDBase.Common {
public static class UDBaseConfig {

// Scheme
public const string SchemeSymbolPrefix = "Scheme_";
public const string SchemeDefaultName = "Default";
Expand Down
9 changes: 9 additions & 0 deletions Controllers/Audio.meta

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

104 changes: 104 additions & 0 deletions Controllers/Audio/Audio.cs
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;
}
}
}
12 changes: 12 additions & 0 deletions Controllers/Audio/Audio.cs.meta

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

165 changes: 165 additions & 0 deletions Controllers/Audio/AudioController.cs
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;
}
}
}
12 changes: 12 additions & 0 deletions Controllers/Audio/AudioController.cs.meta

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

16 changes: 16 additions & 0 deletions Controllers/Audio/AudioSaveNode.cs
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; }
}
}
12 changes: 12 additions & 0 deletions Controllers/Audio/AudioSaveNode.cs.meta

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

Loading

0 comments on commit f5c516a

Please sign in to comment.