Skip to content

Commit

Permalink
Merge pull request #2 from kameffee/develop
Browse files Browse the repository at this point in the history
v0.0.3
  • Loading branch information
kameffee authored Jan 10, 2022
2 parents a9a4887 + 65a8502 commit fe1f40e
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 38 deletions.
54 changes: 54 additions & 0 deletions Packages/AudioPlayer/README.md
Original file line number Diff line number Diff line change
@@ -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)
37 changes: 17 additions & 20 deletions Packages/AudioPlayer/Runtime/AudioPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace Kameffee.AudioPlayer
{
public class AudioPlayer
public sealed class AudioPlayer : IAudioPlayer
{
public static AudioPlayer Instance
public static IAudioPlayer Instance
{
get
{
Expand All @@ -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<float> OnChangeMasterVolume = null;
Expand Down Expand Up @@ -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);
}
}
}
}
4 changes: 3 additions & 1 deletion Packages/AudioPlayer/Runtime/BgmManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions Packages/AudioPlayer/Runtime/BgmPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public enum PlayType
}

[AddComponentMenu("Audio/Bgm Player")]
public class BgmPlayer : MonoBehaviour
public sealed class BgmPlayer : MonoBehaviour
{
[SerializeField]
private PlayType _playType;
Expand Down Expand Up @@ -121,4 +121,4 @@ public void Stop(float fadeOutTime)
AudioPlayer.Instance.Bgm.Stop(fadeOutTime, _ignoreTimeScale);
}
}
}
}
4 changes: 2 additions & 2 deletions Packages/AudioPlayer/Runtime/BgmSlider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,4 +25,4 @@ private void SetVolume(float volume)
AudioPlayer.Instance.Bgm.SetVolume(volume);
}
}
}
}
4 changes: 2 additions & 2 deletions Packages/AudioPlayer/Runtime/CoreBgmAudio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Kameffee.AudioPlayer
{
[AddComponentMenu("")]
public class CoreBgmAudio : MonoBehaviour
public sealed class CoreBgmAudio : MonoBehaviour
{
public AudioSource AudioSource
{
Expand Down Expand Up @@ -156,4 +156,4 @@ public void SetVolume(float volume)
}
}
}
}
}
4 changes: 2 additions & 2 deletions Packages/AudioPlayer/Runtime/CoreSeAudio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Kameffee.AudioPlayer
{
[AddComponentMenu("")]
public class CoreSeAudio : MonoBehaviour
public sealed class CoreSeAudio : MonoBehaviour
{
public AudioSource AudioSource
{
Expand Down Expand Up @@ -48,4 +48,4 @@ public void SetVolume(float volume)
AudioSource.volume = volume;
}
}
}
}
21 changes: 21 additions & 0 deletions Packages/AudioPlayer/Runtime/IAudioPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace Kameffee.AudioPlayer
{
public interface IAudioPlayer
{
float MasterVolume { get; }

bool Initialized { get; }

event Action<float> OnChangeMasterVolume;

IBgmPlayer Bgm { get; }

ISePlayer Se { get; }

void Initialize();

void SetMasterVolume(float volume);
}
}
3 changes: 3 additions & 0 deletions Packages/AudioPlayer/Runtime/IAudioPlayer.cs.meta

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

30 changes: 30 additions & 0 deletions Packages/AudioPlayer/Runtime/IBgmPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using UnityEngine;

namespace Kameffee.AudioPlayer
{
public interface IBgmPlayer
{
bool IsPlaying { get; }

float Volume { get; }

event Action<float> 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);
}
}
3 changes: 3 additions & 0 deletions Packages/AudioPlayer/Runtime/IBgmPlayer.cs.meta

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

18 changes: 18 additions & 0 deletions Packages/AudioPlayer/Runtime/ISePlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using UnityEngine;

namespace Kameffee.AudioPlayer
{
public interface ISePlayer
{
float Volume { get; }

event Action<float> OnChangeVolume;

void Play(AudioClip audioClip, float pitch = 1f);

void Stop();

void SetVolume(float volume);
}
}
3 changes: 3 additions & 0 deletions Packages/AudioPlayer/Runtime/ISePlayer.cs.meta

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

2 changes: 1 addition & 1 deletion Packages/AudioPlayer/Runtime/SeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
4 changes: 2 additions & 2 deletions Packages/AudioPlayer/Runtime/SePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Kameffee.AudioPlayer
{
[AddComponentMenu("Audio/Se Player")]
public class SePlayer : MonoBehaviour
public sealed class SePlayer : MonoBehaviour
{
[SerializeField]
private AudioClip _clip;
Expand Down Expand Up @@ -34,4 +34,4 @@ public void Play(AudioClip audioClip)
AudioPlayer.Instance.Se.Play(audioClip, _pitch);
}
}
}
}
2 changes: 1 addition & 1 deletion Packages/AudioPlayer/Runtime/SeSlider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Kameffee.AudioPlayer
{
[AddComponentMenu("UI/Audio/SeSlider")]
public class SeSlider : MonoBehaviour
public sealed class SeSlider : MonoBehaviour
{
[SerializeField]
private Slider seSlider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "AudioManager",
"name": "AudioPlayer",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
6 changes: 2 additions & 4 deletions Packages/AudioPlayer/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -14,13 +14,11 @@
"bgm"
],
"license": "MIT",
"dependencies": {
},
"samples": [
{
"displayName": "AudioPlayer Demo",
"description": "",
"path": "Samples~/AudioPlayerDemo"
}
]
}
}

0 comments on commit fe1f40e

Please sign in to comment.