diff --git a/Packages/AudioPlayer/Editor.meta b/Packages/AudioPlayer/Editor.meta new file mode 100644 index 0000000..fcbe4ca --- /dev/null +++ b/Packages/AudioPlayer/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a4c458fb64bf4ca6bbab334ba1cd22e1 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/AudioPlayer/Editor/BgmBundleEditor.cs b/Packages/AudioPlayer/Editor/BgmBundleEditor.cs new file mode 100644 index 0000000..b8e4967 --- /dev/null +++ b/Packages/AudioPlayer/Editor/BgmBundleEditor.cs @@ -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(); + } + } +} diff --git a/Packages/AudioPlayer/Editor/BgmBundleEditor.cs.meta b/Packages/AudioPlayer/Editor/BgmBundleEditor.cs.meta new file mode 100644 index 0000000..a176904 --- /dev/null +++ b/Packages/AudioPlayer/Editor/BgmBundleEditor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5314566721540475c9a37a6745185411 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/AudioPlayer/Editor/SeBundleEditor.cs b/Packages/AudioPlayer/Editor/SeBundleEditor.cs new file mode 100644 index 0000000..0210777 --- /dev/null +++ b/Packages/AudioPlayer/Editor/SeBundleEditor.cs @@ -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(); + } + } +} diff --git a/Packages/AudioPlayer/Editor/SeBundleEditor.cs.meta b/Packages/AudioPlayer/Editor/SeBundleEditor.cs.meta new file mode 100644 index 0000000..10494f6 --- /dev/null +++ b/Packages/AudioPlayer/Editor/SeBundleEditor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1224cd8cb5fb4d15848f65554301e23b +timeCreated: 1642004996 \ No newline at end of file diff --git a/Packages/AudioPlayer/Editor/com.kameffee.audio-player.Editor.asmdef b/Packages/AudioPlayer/Editor/com.kameffee.audio-player.Editor.asmdef new file mode 100644 index 0000000..7832a18 --- /dev/null +++ b/Packages/AudioPlayer/Editor/com.kameffee.audio-player.Editor.asmdef @@ -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 +} \ No newline at end of file diff --git a/Packages/AudioPlayer/Editor/com.kameffee.audio-player.Editor.asmdef.meta b/Packages/AudioPlayer/Editor/com.kameffee.audio-player.Editor.asmdef.meta new file mode 100644 index 0000000..e4cdb6d --- /dev/null +++ b/Packages/AudioPlayer/Editor/com.kameffee.audio-player.Editor.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 71085c4622d0d4dce8fb207376278a6a +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/AudioPlayer/Runtime/AudioPlayer.cs b/Packages/AudioPlayer/Runtime/AudioPlayer.cs index 5258d4e..fed809e 100644 --- a/Packages/AudioPlayer/Runtime/AudioPlayer.cs +++ b/Packages/AudioPlayer/Runtime/AudioPlayer.cs @@ -67,11 +67,16 @@ public void InitializeBgm(Func bgmDataLoader = null) _bgmManager.Initialize(bgmDataLoader.Invoke()); } - public void InitializeSe() + public void InitializeSe(Func seDataLoader = null) { + if (seDataLoader == null) + { + seDataLoader = () => Resources.Load("SeBundle"); + } + // Initialize SE _seManager = SeManager.Create(); - _seManager.Initialize(); + _seManager.Initialize(seDataLoader.Invoke()); } public void SetMasterVolume(float volume) diff --git a/Packages/AudioPlayer/Runtime/CoreBgmAudio.cs b/Packages/AudioPlayer/Runtime/CoreBgmAudio.cs index 8886ba3..d3d6950 100644 --- a/Packages/AudioPlayer/Runtime/CoreBgmAudio.cs +++ b/Packages/AudioPlayer/Runtime/CoreBgmAudio.cs @@ -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; diff --git a/Packages/AudioPlayer/Runtime/Data/BgmData.cs b/Packages/AudioPlayer/Runtime/Data/BgmData.cs new file mode 100644 index 0000000..6b28dd1 --- /dev/null +++ b/Packages/AudioPlayer/Runtime/Data/BgmData.cs @@ -0,0 +1,14 @@ +using System; +using UnityEngine; + +namespace Kameffee.AudioPlayer +{ + [Serializable] + public class BgmData + { + public AudioClip AudioClip => _audioClip; + + [SerializeField] + private AudioClip _audioClip; + } +} diff --git a/Packages/AudioPlayer/Runtime/Data/BgmData.cs.meta b/Packages/AudioPlayer/Runtime/Data/BgmData.cs.meta new file mode 100644 index 0000000..668ed7f --- /dev/null +++ b/Packages/AudioPlayer/Runtime/Data/BgmData.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: dd345e093b3f4c24b7a5cc4d7eb518a6 +timeCreated: 1641999387 \ No newline at end of file diff --git a/Packages/AudioPlayer/Runtime/Data/IBgmBundle.cs b/Packages/AudioPlayer/Runtime/Data/IBgmBundle.cs index 347d7bb..6358d3a 100644 --- a/Packages/AudioPlayer/Runtime/Data/IBgmBundle.cs +++ b/Packages/AudioPlayer/Runtime/Data/IBgmBundle.cs @@ -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); } -} \ No newline at end of file +} diff --git a/Packages/AudioPlayer/Runtime/Data/ISeBundle.cs b/Packages/AudioPlayer/Runtime/Data/ISeBundle.cs new file mode 100644 index 0000000..ba928a6 --- /dev/null +++ b/Packages/AudioPlayer/Runtime/Data/ISeBundle.cs @@ -0,0 +1,9 @@ +namespace Kameffee.AudioPlayer +{ + public interface ISeBundle + { + SeData GetData(int index); + + SeData GetData(string key); + } +} \ No newline at end of file diff --git a/Packages/AudioPlayer/Runtime/Data/ISeBundle.cs.meta b/Packages/AudioPlayer/Runtime/Data/ISeBundle.cs.meta new file mode 100644 index 0000000..6ea9cf6 --- /dev/null +++ b/Packages/AudioPlayer/Runtime/Data/ISeBundle.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e0b8bc6a091f4b99891ba1cc796c57ee +timeCreated: 1641999264 \ No newline at end of file diff --git a/Packages/AudioPlayer/Runtime/Data/SeBundle.cs b/Packages/AudioPlayer/Runtime/Data/SeBundle.cs new file mode 100644 index 0000000..c3622ad --- /dev/null +++ b/Packages/AudioPlayer/Runtime/Data/SeBundle.cs @@ -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 _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); + } + } +} diff --git a/Packages/AudioPlayer/Runtime/Data/SeBundle.cs.meta b/Packages/AudioPlayer/Runtime/Data/SeBundle.cs.meta new file mode 100644 index 0000000..41bd0be --- /dev/null +++ b/Packages/AudioPlayer/Runtime/Data/SeBundle.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6ab7411faa664d07a69f61b1f6912b2d +timeCreated: 1641998714 \ No newline at end of file diff --git a/Packages/AudioPlayer/Runtime/Data/SeData.cs b/Packages/AudioPlayer/Runtime/Data/SeData.cs new file mode 100644 index 0000000..84fa26f --- /dev/null +++ b/Packages/AudioPlayer/Runtime/Data/SeData.cs @@ -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; + } +} diff --git a/Packages/AudioPlayer/Runtime/Data/SeData.cs.meta b/Packages/AudioPlayer/Runtime/Data/SeData.cs.meta new file mode 100644 index 0000000..b7a0f50 --- /dev/null +++ b/Packages/AudioPlayer/Runtime/Data/SeData.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1fdbca140702490b8a3e27dec8802abb +timeCreated: 1641999256 \ No newline at end of file diff --git a/Packages/AudioPlayer/Runtime/ISePlayer.cs b/Packages/AudioPlayer/Runtime/ISePlayer.cs index 7cb0be5..40c68a1 100644 --- a/Packages/AudioPlayer/Runtime/ISePlayer.cs +++ b/Packages/AudioPlayer/Runtime/ISePlayer.cs @@ -10,9 +10,11 @@ public interface ISePlayer event Action OnChangeVolume; void Play(AudioClip audioClip, float pitch = 1f); + + void Play(string key, float pitch = 1f); void Stop(); void SetVolume(float volume); } -} \ No newline at end of file +} diff --git a/Packages/AudioPlayer/Runtime/SeManager.cs b/Packages/AudioPlayer/Runtime/SeManager.cs index bb4eb7f..dbd80df 100644 --- a/Packages/AudioPlayer/Runtime/SeManager.cs +++ b/Packages/AudioPlayer/Runtime/SeManager.cs @@ -18,6 +18,8 @@ public sealed class SeManager : MonoBehaviour, ISePlayer public event Action OnChangeVolume = null; + private ISeBundle _seBundle; + public static SeManager Create() { var se = new GameObject(SeManagerName); @@ -25,8 +27,9 @@ public static SeManager Create() return se.AddComponent(); } - 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++) @@ -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; diff --git a/Packages/AudioPlayer/Runtime/SePlayer.cs b/Packages/AudioPlayer/Runtime/SePlayer.cs index e961c60..095ee39 100644 --- a/Packages/AudioPlayer/Runtime/SePlayer.cs +++ b/Packages/AudioPlayer/Runtime/SePlayer.cs @@ -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; @@ -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; @@ -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) diff --git a/Packages/AudioPlayer/package.json b/Packages/AudioPlayer/package.json index adbea00..a30191f 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.3", + "version": "0.0.4", "unity": "2019.4", "description": "Simple Audio Player", "author": {