Skip to content

Commit

Permalink
Merge pull request #3 from kameffee/develop
Browse files Browse the repository at this point in the history
v0.0.4
  • Loading branch information
kameffee authored Jan 14, 2022
2 parents fe1f40e + 9ee347e commit cd8307b
Show file tree
Hide file tree
Showing 22 changed files with 262 additions and 24 deletions.
8 changes: 8 additions & 0 deletions Packages/AudioPlayer/Editor.meta

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

36 changes: 36 additions & 0 deletions Packages/AudioPlayer/Editor/BgmBundleEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections;
using System.Collections.Generic;
using Kameffee.AudioPlayer;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace Kameffee.AudioPlayer
{
[CustomEditor(typeof(BgmBundle))]
public class BgmBundleEditor : Editor
{
private ReorderableList _reorderableList;

private void OnEnable()
{
var _list = serializedObject.FindProperty("_bgmDataList");
_reorderableList = new ReorderableList(serializedObject, _list)
{
drawElementCallback = (rect, index, active, focused) =>
{
rect.xMin += 10;
EditorGUI.PropertyField(rect, _list.GetArrayElementAtIndex(index), true);
},
elementHeightCallback = index => EditorGUI.GetPropertyHeight(_list.GetArrayElementAtIndex(index)),
};
}

public override void OnInspectorGUI()
{
serializedObject.Update();
_reorderableList.DoLayoutList();
serializedObject.ApplyModifiedProperties();
}
}
}
11 changes: 11 additions & 0 deletions Packages/AudioPlayer/Editor/BgmBundleEditor.cs.meta

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

32 changes: 32 additions & 0 deletions Packages/AudioPlayer/Editor/SeBundleEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using UnityEditor;
using UnityEditorInternal;

namespace Kameffee.AudioPlayer
{
[CustomEditor(typeof(SeBundle))]
public class SeBundleEditor : Editor
{
private ReorderableList _reorderableList;

private void OnEnable()
{
var _list = serializedObject.FindProperty("_seDataList");
_reorderableList = new ReorderableList(serializedObject, _list)
{
drawElementCallback = (rect, index, active, focused) =>
{
rect.xMin += 10;
EditorGUI.PropertyField(rect, _list.GetArrayElementAtIndex(index), true);
},
elementHeightCallback = index => EditorGUI.GetPropertyHeight(_list.GetArrayElementAtIndex(index)),
};
}

public override void OnInspectorGUI()
{
serializedObject.Update();
_reorderableList.DoLayoutList();
serializedObject.ApplyModifiedProperties();
}
}
}
3 changes: 3 additions & 0 deletions Packages/AudioPlayer/Editor/SeBundleEditor.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
@@ -0,0 +1,18 @@
{
"name": "com.kameffee.audio-player.Editor",
"references": [
"GUID:343deaaf83e0cee4ca978e7df0b80d21",
"GUID:117c7fb0080b4ca41bc18bf080e4cf82"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

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

9 changes: 7 additions & 2 deletions Packages/AudioPlayer/Runtime/AudioPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,16 @@ public void InitializeBgm(Func<IBgmBundle> bgmDataLoader = null)
_bgmManager.Initialize(bgmDataLoader.Invoke());
}

public void InitializeSe()
public void InitializeSe(Func<ISeBundle> seDataLoader = null)
{
if (seDataLoader == null)
{
seDataLoader = () => Resources.Load<SeBundle>("SeBundle");
}

// Initialize SE
_seManager = SeManager.Create();
_seManager.Initialize();
_seManager.Initialize(seDataLoader.Invoke());
}

public void SetMasterVolume(float volume)
Expand Down
1 change: 1 addition & 0 deletions Packages/AudioPlayer/Runtime/CoreBgmAudio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public AudioSource AudioSource

public void Play(AudioClip audioClip, float pitch = 1, float fadeTime = 0, bool ignoreTimeScale = false)
{
AudioSource.loop = true;
AudioSource.clip = audioClip;
AudioSource.pitch = pitch;

Expand Down
14 changes: 14 additions & 0 deletions Packages/AudioPlayer/Runtime/Data/BgmData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using UnityEngine;

namespace Kameffee.AudioPlayer
{
[Serializable]
public class BgmData
{
public AudioClip AudioClip => _audioClip;

[SerializeField]
private AudioClip _audioClip;
}
}
3 changes: 3 additions & 0 deletions Packages/AudioPlayer/Runtime/Data/BgmData.cs.meta

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

19 changes: 2 additions & 17 deletions Packages/AudioPlayer/Runtime/Data/IBgmBundle.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
using System;
using UnityEngine;


namespace Kameffee.AudioPlayer
{
[Serializable]
public class BgmData
{
public AudioClip AudioClip => _audioClip;
public float Pitch => _pitch;

[SerializeField]
private AudioClip _audioClip;

[SerializeField]
private float _pitch = 1f;
}

public interface IBgmBundle
{
BgmData GetData(int index);

BgmData GetData(string name);
}
}
}
9 changes: 9 additions & 0 deletions Packages/AudioPlayer/Runtime/Data/ISeBundle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Kameffee.AudioPlayer
{
public interface ISeBundle
{
SeData GetData(int index);

SeData GetData(string key);
}
}
3 changes: 3 additions & 0 deletions Packages/AudioPlayer/Runtime/Data/ISeBundle.cs.meta

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

29 changes: 29 additions & 0 deletions Packages/AudioPlayer/Runtime/Data/SeBundle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Kameffee.AudioPlayer
{
[CreateAssetMenu(fileName = "SeBundle", menuName = "AudioSettings/SeBundle")]
public class SeBundle : ScriptableObject, ISeBundle
{
[SerializeField]
private List<SeData> _seDataList;

public SeData GetData(int index)
{
if (index < 0 || index >= _seDataList.Count)
{
throw new ArgumentOutOfRangeException();
}

return _seDataList[index];
}

public SeData GetData(string key)
{
return _seDataList.First(data => data.Key == key);
}
}
}
3 changes: 3 additions & 0 deletions Packages/AudioPlayer/Runtime/Data/SeBundle.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/Data/SeData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using UnityEngine;

namespace Kameffee.AudioPlayer
{
[Serializable]
public class SeData
{
public string Key => _key;
public AudioClip AudioClip => _audioClip;

[SerializeField]
private string _key = "SeKey_0";

[SerializeField]
private AudioClip _audioClip;
}
}
3 changes: 3 additions & 0 deletions Packages/AudioPlayer/Runtime/Data/SeData.cs.meta

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

4 changes: 3 additions & 1 deletion Packages/AudioPlayer/Runtime/ISePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ public interface ISePlayer
event Action<float> OnChangeVolume;

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

void Play(string key, float pitch = 1f);

void Stop();

void SetVolume(float volume);
}
}
}
17 changes: 16 additions & 1 deletion Packages/AudioPlayer/Runtime/SeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ public sealed class SeManager : MonoBehaviour, ISePlayer

public event Action<float> OnChangeVolume = null;

private ISeBundle _seBundle;

public static SeManager Create()
{
var se = new GameObject(SeManagerName);
DontDestroyOnLoad(se);
return se.AddComponent<SeManager>();
}

public void Initialize(float initialVolume = 1)
public void Initialize(ISeBundle seBundle, float initialVolume = 1)
{
_seBundle = seBundle;
_volume = initialVolume;

for (int i = 0; i < _preInstanceCount; i++)
Expand All @@ -44,6 +47,18 @@ private CoreSeAudio CreatePlayer()
return player;
}

public void Play(string key, float pitch = 1)
{
if (_seBundle == null)
{
Debug.LogError("SeBundle is not loaded.");
return;
}

var seData = _seBundle.GetData(key);
Play(seData.AudioClip, pitch);
}

public void Play(AudioClip audioClip, float pitch = 1f)
{
CoreSeAudio seAudio;
Expand Down
37 changes: 35 additions & 2 deletions Packages/AudioPlayer/Runtime/SePlayer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using UnityEngine;
using System;
using UnityEngine;

namespace Kameffee.AudioPlayer
{
[AddComponentMenu("Audio/Se Player")]
public sealed class SePlayer : MonoBehaviour
{
[SerializeField]
private PlayType _playType;

[SerializeField]
private AudioClip _clip;

Expand All @@ -14,6 +18,15 @@ public AudioClip Clip
set => _clip = value;
}

[SerializeField]
private string _key;

public string Key
{
get => _key;
set => _key = value;
}

[SerializeField]
[Range(-3, 3f)]
private float _pitch = 1f;
Expand All @@ -26,7 +39,27 @@ public float Pitch

public void Play()
{
AudioPlayer.Instance.Se.Play(_clip);
switch (_playType)
{
case PlayType.AudioClip:
Play(_clip);
break;
case PlayType.Id:
Play(_key);
break;
default:
throw new ArgumentOutOfRangeException();
}
}

public void Play(string key)
{
Play(key, _pitch);
}

public void Play(string key, float pitch)
{
AudioPlayer.Instance.Se.Play(key, pitch);
}

public void Play(AudioClip audioClip)
Expand Down
2 changes: 1 addition & 1 deletion 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.3",
"version": "0.0.4",
"unity": "2019.4",
"description": "Simple Audio Player",
"author": {
Expand Down

0 comments on commit cd8307b

Please sign in to comment.