diff --git a/Packages/AudioPlayer/README.md b/Packages/AudioPlayer/README.md index 67ac8ee..5162c46 100644 --- a/Packages/AudioPlayer/README.md +++ b/Packages/AudioPlayer/README.md @@ -1,3 +1,57 @@ # AudioPlayer ![Unity 2019.4+](https://img.shields.io/badge/unity-unity%202019.4%2B-blue) ![License: MIT](https://img.shields.io/badge/License-MIT-brightgreen.svg) + +## Introduction + +### Initialize + +```c# +// One time Initialize +AudioPlayer.Instance.Initialize(); +``` + +### Play Bgm + +#### Simple play +```c# +AudioPlayer.Instance.Bgm.Play(audioClip); +``` + +#### Cross Fade +```c# +AudioPlayer.Instance.Bgm.CrossFade(audioClip, crossFadeTime: 3f); +``` + +### Play Sound effects +```c# +AudioPlayer.Instance.Se.Play(audioClip); +``` + +### Volume settings + +#### Change the volumes +```c# +// Master volume +AudioPlayer.Instance.SetMasterVolume(0.5f); + +// Bgm volume +AudioPlayer.Instance.Bgm.SetVolume(1f); // ActualVolume 0.5f + +// Se volume +AudioPlayer.Instance.Se.SetVolume(0.5f); // ActualVolume 0.25f +``` + +The actual volume of the BGM will be `0.5 x 1 = 0.5f`. +The actual volume of the SE will be `0.5 x 0.5 = 0.25f`. + +## Installation + +### Installing from a Git URL +1. Copy for `https://github.com/kameffee/AudioPlayer.git` +2. Open a PackageManager. `Window/PackageManager` +3. Click the add **[+]** button in the status bar. +4. Select Add package from git URL from the add menu. +5. Enter a valid Git URL in the text box and click Add. + +see for [Installing from a Git URL](https://docs.unity3d.com/2019.4/Documentation/Manual/upm-ui-giturl.html) diff --git a/Packages/AudioPlayer/Runtime/AudioPlayer.cs b/Packages/AudioPlayer/Runtime/AudioPlayer.cs index ce2ee03..5258d4e 100644 --- a/Packages/AudioPlayer/Runtime/AudioPlayer.cs +++ b/Packages/AudioPlayer/Runtime/AudioPlayer.cs @@ -3,9 +3,9 @@ namespace Kameffee.AudioPlayer { - public class AudioPlayer + public sealed class AudioPlayer : IAudioPlayer { - public static AudioPlayer Instance + public static IAudioPlayer Instance { get { @@ -21,28 +21,13 @@ public static AudioPlayer Instance private static AudioPlayer _instance; - public BgmManager Bgm => _bgmManager; + public IBgmPlayer Bgm => _bgmManager; private BgmManager _bgmManager; - public SeManager Se => _seManager; + public ISePlayer Se => _seManager; private SeManager _seManager; - public float MasterVolume - { - get => _masterVolume; - set - { - var toVolume = Mathf.Clamp01(value); - bool isChange = Math.Abs(toVolume - _masterVolume) > 0.000001f; - _masterVolume = toVolume; - - if (isChange) - { - OnChangeMasterVolume?.Invoke(toVolume); - } - } - } - + public float MasterVolume => _masterVolume; private float _masterVolume; public event Action OnChangeMasterVolume = null; @@ -88,5 +73,17 @@ public void InitializeSe() _seManager = SeManager.Create(); _seManager.Initialize(); } + + public void SetMasterVolume(float volume) + { + var toVolume = Mathf.Clamp01(volume); + bool isChange = Math.Abs(toVolume - _masterVolume) > 0.000001f; + _masterVolume = toVolume; + + if (isChange) + { + OnChangeMasterVolume?.Invoke(toVolume); + } + } } } diff --git a/Packages/AudioPlayer/Runtime/BgmManager.cs b/Packages/AudioPlayer/Runtime/BgmManager.cs index b667b62..cc20c35 100644 --- a/Packages/AudioPlayer/Runtime/BgmManager.cs +++ b/Packages/AudioPlayer/Runtime/BgmManager.cs @@ -6,7 +6,7 @@ namespace Kameffee.AudioPlayer { - public class BgmManager : MonoBehaviour + public sealed class BgmManager : MonoBehaviour, IBgmPlayer { private static readonly string ManagerName = "BgmManager"; private static readonly string PlayerName = "BgmPlayer"; @@ -15,6 +15,8 @@ public class BgmManager : MonoBehaviour private int _preInstanceCount = 2; + public bool IsPlaying => _bgmPlayers.Any(bgmAudio => bgmAudio.IsPlaying); + public float Volume => _volume; private float _volume; diff --git a/Packages/AudioPlayer/Runtime/BgmPlayer.cs b/Packages/AudioPlayer/Runtime/BgmPlayer.cs index e6f0c26..1fc54c8 100644 --- a/Packages/AudioPlayer/Runtime/BgmPlayer.cs +++ b/Packages/AudioPlayer/Runtime/BgmPlayer.cs @@ -10,7 +10,7 @@ public enum PlayType } [AddComponentMenu("Audio/Bgm Player")] - public class BgmPlayer : MonoBehaviour + public sealed class BgmPlayer : MonoBehaviour { [SerializeField] private PlayType _playType; @@ -121,4 +121,4 @@ public void Stop(float fadeOutTime) AudioPlayer.Instance.Bgm.Stop(fadeOutTime, _ignoreTimeScale); } } -} \ No newline at end of file +} diff --git a/Packages/AudioPlayer/Runtime/BgmSlider.cs b/Packages/AudioPlayer/Runtime/BgmSlider.cs index e799baf..27e98cc 100644 --- a/Packages/AudioPlayer/Runtime/BgmSlider.cs +++ b/Packages/AudioPlayer/Runtime/BgmSlider.cs @@ -4,7 +4,7 @@ namespace Kameffee.AudioPlayer { [AddComponentMenu("UI/Audio/Bgm Slider")] - public class BgmSlider : MonoBehaviour + public sealed class BgmSlider : MonoBehaviour { [SerializeField] private Slider bgmSlider; @@ -25,4 +25,4 @@ private void SetVolume(float volume) AudioPlayer.Instance.Bgm.SetVolume(volume); } } -} \ No newline at end of file +} diff --git a/Packages/AudioPlayer/Runtime/CoreBgmAudio.cs b/Packages/AudioPlayer/Runtime/CoreBgmAudio.cs index caf7354..8886ba3 100644 --- a/Packages/AudioPlayer/Runtime/CoreBgmAudio.cs +++ b/Packages/AudioPlayer/Runtime/CoreBgmAudio.cs @@ -5,7 +5,7 @@ namespace Kameffee.AudioPlayer { [AddComponentMenu("")] - public class CoreBgmAudio : MonoBehaviour + public sealed class CoreBgmAudio : MonoBehaviour { public AudioSource AudioSource { @@ -156,4 +156,4 @@ public void SetVolume(float volume) } } } -} \ No newline at end of file +} diff --git a/Packages/AudioPlayer/Runtime/CoreSeAudio.cs b/Packages/AudioPlayer/Runtime/CoreSeAudio.cs index 675771f..bde5695 100644 --- a/Packages/AudioPlayer/Runtime/CoreSeAudio.cs +++ b/Packages/AudioPlayer/Runtime/CoreSeAudio.cs @@ -3,7 +3,7 @@ namespace Kameffee.AudioPlayer { [AddComponentMenu("")] - public class CoreSeAudio : MonoBehaviour + public sealed class CoreSeAudio : MonoBehaviour { public AudioSource AudioSource { @@ -48,4 +48,4 @@ public void SetVolume(float volume) AudioSource.volume = volume; } } -} \ No newline at end of file +} diff --git a/Packages/AudioPlayer/Runtime/IAudioPlayer.cs b/Packages/AudioPlayer/Runtime/IAudioPlayer.cs new file mode 100644 index 0000000..58b5246 --- /dev/null +++ b/Packages/AudioPlayer/Runtime/IAudioPlayer.cs @@ -0,0 +1,21 @@ +using System; + +namespace Kameffee.AudioPlayer +{ + public interface IAudioPlayer + { + float MasterVolume { get; } + + bool Initialized { get; } + + event Action OnChangeMasterVolume; + + IBgmPlayer Bgm { get; } + + ISePlayer Se { get; } + + void Initialize(); + + void SetMasterVolume(float volume); + } +} diff --git a/Packages/AudioPlayer/Runtime/IAudioPlayer.cs.meta b/Packages/AudioPlayer/Runtime/IAudioPlayer.cs.meta new file mode 100644 index 0000000..0020492 --- /dev/null +++ b/Packages/AudioPlayer/Runtime/IAudioPlayer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d1ae51d63a6a470d96300af3665bfa93 +timeCreated: 1641773829 \ No newline at end of file diff --git a/Packages/AudioPlayer/Runtime/IBgmPlayer.cs b/Packages/AudioPlayer/Runtime/IBgmPlayer.cs new file mode 100644 index 0000000..70e5f39 --- /dev/null +++ b/Packages/AudioPlayer/Runtime/IBgmPlayer.cs @@ -0,0 +1,30 @@ +using System; +using UnityEngine; + +namespace Kameffee.AudioPlayer +{ + public interface IBgmPlayer + { + bool IsPlaying { get; } + + float Volume { get; } + + event Action OnChangeVolume; + + void Play(int id, float pitch = 1, float fadeTime = 0f, bool ignoreTimeScale = false); + + void Play(AudioClip audioClip, float pitch = 1, float fadeTime = 0f, bool ignoreTimeScale = false); + + void CrossFade(int id, float crossFadeTime, float pitch = 1, bool ignoreTimeScale = false); + + void CrossFade(AudioClip audioClip, float crossFadeTime, float pitch = 1, bool ignoreTimeScale = false); + + void Stop(float fadeTime = 0f, bool ignoreTimeScale = false); + + void Pause(); + + void UnPause(); + + void SetVolume(float volume); + } +} diff --git a/Packages/AudioPlayer/Runtime/IBgmPlayer.cs.meta b/Packages/AudioPlayer/Runtime/IBgmPlayer.cs.meta new file mode 100644 index 0000000..768727d --- /dev/null +++ b/Packages/AudioPlayer/Runtime/IBgmPlayer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 0cb357055f684b29b18d2d073f6a91ed +timeCreated: 1641774877 \ No newline at end of file diff --git a/Packages/AudioPlayer/Runtime/ISePlayer.cs b/Packages/AudioPlayer/Runtime/ISePlayer.cs new file mode 100644 index 0000000..7cb0be5 --- /dev/null +++ b/Packages/AudioPlayer/Runtime/ISePlayer.cs @@ -0,0 +1,18 @@ +using System; +using UnityEngine; + +namespace Kameffee.AudioPlayer +{ + public interface ISePlayer + { + float Volume { get; } + + event Action OnChangeVolume; + + void Play(AudioClip audioClip, float pitch = 1f); + + void Stop(); + + void SetVolume(float volume); + } +} \ No newline at end of file diff --git a/Packages/AudioPlayer/Runtime/ISePlayer.cs.meta b/Packages/AudioPlayer/Runtime/ISePlayer.cs.meta new file mode 100644 index 0000000..2fbbaa8 --- /dev/null +++ b/Packages/AudioPlayer/Runtime/ISePlayer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 7f0c52147bd84f9b9a44ba51b3341f9b +timeCreated: 1641774775 \ No newline at end of file diff --git a/Packages/AudioPlayer/Runtime/SeManager.cs b/Packages/AudioPlayer/Runtime/SeManager.cs index df8fb84..bb4eb7f 100644 --- a/Packages/AudioPlayer/Runtime/SeManager.cs +++ b/Packages/AudioPlayer/Runtime/SeManager.cs @@ -4,7 +4,7 @@ namespace Kameffee.AudioPlayer { - public class SeManager : MonoBehaviour + public sealed class SeManager : MonoBehaviour, ISePlayer { private static readonly string SeManagerName = "SeManager"; private static readonly string SePlayerName = "SePlayer"; diff --git a/Packages/AudioPlayer/Runtime/SePlayer.cs b/Packages/AudioPlayer/Runtime/SePlayer.cs index 14612c8..e961c60 100644 --- a/Packages/AudioPlayer/Runtime/SePlayer.cs +++ b/Packages/AudioPlayer/Runtime/SePlayer.cs @@ -3,7 +3,7 @@ namespace Kameffee.AudioPlayer { [AddComponentMenu("Audio/Se Player")] - public class SePlayer : MonoBehaviour + public sealed class SePlayer : MonoBehaviour { [SerializeField] private AudioClip _clip; @@ -34,4 +34,4 @@ public void Play(AudioClip audioClip) AudioPlayer.Instance.Se.Play(audioClip, _pitch); } } -} \ No newline at end of file +} diff --git a/Packages/AudioPlayer/Runtime/SeSlider.cs b/Packages/AudioPlayer/Runtime/SeSlider.cs index 5460a41..0e464cf 100644 --- a/Packages/AudioPlayer/Runtime/SeSlider.cs +++ b/Packages/AudioPlayer/Runtime/SeSlider.cs @@ -4,7 +4,7 @@ namespace Kameffee.AudioPlayer { [AddComponentMenu("UI/Audio/SeSlider")] - public class SeSlider : MonoBehaviour + public sealed class SeSlider : MonoBehaviour { [SerializeField] private Slider seSlider; diff --git a/Packages/AudioPlayer/Runtime/com.kameffee.audio-player.asmdef b/Packages/AudioPlayer/Runtime/com.kameffee.audio-player.asmdef index d54f474..6d2c350 100644 --- a/Packages/AudioPlayer/Runtime/com.kameffee.audio-player.asmdef +++ b/Packages/AudioPlayer/Runtime/com.kameffee.audio-player.asmdef @@ -1,5 +1,5 @@ { - "name": "AudioManager", + "name": "AudioPlayer", "references": [], "includePlatforms": [], "excludePlatforms": [], diff --git a/Packages/AudioPlayer/package.json b/Packages/AudioPlayer/package.json index 055fc22..adbea00 100644 --- a/Packages/AudioPlayer/package.json +++ b/Packages/AudioPlayer/package.json @@ -1,7 +1,7 @@ { "name": "com.kameffee.audio-player", "displayName": "AudioPlayer", - "version": "0.0.2", + "version": "0.0.3", "unity": "2019.4", "description": "Simple Audio Player", "author": { @@ -14,8 +14,6 @@ "bgm" ], "license": "MIT", - "dependencies": { - }, "samples": [ { "displayName": "AudioPlayer Demo", @@ -23,4 +21,4 @@ "path": "Samples~/AudioPlayerDemo" } ] -} +} \ No newline at end of file