Last active
June 5, 2019 02:53
-
-
Save XakazukinX/9c10d6a99c48e03b332e0e84d3c7821e to your computer and use it in GitHub Desktop.
さうんどまねーじゃー模索
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if UNITY_EDITOR | |
using UnityEditor; | |
using UnityEngine; | |
namespace shigeno_EditorUtility | |
{ | |
public class NonEditableAttribute : PropertyAttribute | |
{ | |
} | |
[CustomPropertyDrawer(typeof(NonEditableAttribute))] | |
public class NonEditableDrawer : PropertyDrawer | |
{ | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
NonEditableAttribute NonEditable = (NonEditableAttribute) attribute; | |
EditorGUI.BeginDisabledGroup(true); | |
EditorGUI.PropertyField(position, property, label); | |
EditorGUI.EndDisabledGroup(); | |
} | |
} | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using shigeno_EditorUtility; | |
using UnityEditor; | |
using UnityEngine; | |
public class SoundManager : SingletonMonoBehaviour<SoundManager> | |
{ | |
[HideInInspector] public AudioSource[] _audioSources; | |
internal Dictionary<string, AudioSource> soundListDictionary; | |
//サウンドマネージャから呼び出せるサウンドの一覧 | |
[System.Serializable] | |
public class SoundListSetting | |
{ | |
[CustomLabel("サウンドの名前")] | |
public string soundName; | |
[CustomLabel("再生元になるAudioSource")] | |
public AudioSource targetAudioSource ; | |
[NonEditable] | |
public AudioClip targetAudioClip ; | |
} | |
public List<SoundListSetting> _soundListSettings = new List<SoundListSetting>(); | |
[Header("AutoSetSettings/Test")] [CustomLabel("サウンド名も自動設定するか")] [SerializeField] | |
private bool autoSetName = true; | |
//テストボタンで実行するサウンドの名前 | |
internal string testPlayAudioClipName = "Registered name"; | |
//テストボタンで実行するサウンドのインデックス | |
internal int testPlayAudioClipIdx = 0; | |
private void Awake() | |
{ | |
base.Awake(); | |
initDictionary(); | |
} | |
//サウンド名で指定する場合 | |
internal AudioSource playSound(string targetSoundName) | |
{ | |
return soundListDictionary[targetSoundName]; | |
} | |
//インデックスで指定する場合 | |
internal AudioSource playSound(int targetSoundIdx) | |
{ | |
return _soundListSettings[targetSoundIdx].targetAudioSource; | |
} | |
//サウンドが登録された辞書を初期化する関数 | |
internal void initDictionary() | |
{ | |
//辞書を初期化 | |
soundListDictionary = new Dictionary<string, AudioSource>(); | |
//辞書にSoundListSettingsの内容を格納 | |
for (int i = 0; i < _soundListSettings.Count; i++) | |
{ | |
soundListDictionary.Add(_soundListSettings[i].soundName,_soundListSettings[i].targetAudioSource); | |
} | |
} | |
//自動でSoundListSettingに追加するコンテキストメニュー | |
[ContextMenu("AutoSet")] | |
public void AutoSet() | |
{ | |
//AudioSourceが一つもなければ中断する | |
if (GetComponents<AudioSource>().Length == 0) | |
{ | |
Debug.LogError("SoundManagerにAudioSouceが一つも設定されていません。自動設定を中断します"); | |
return; | |
} | |
var sources = GetComponents<AudioSource>(); | |
//SoundListSettingを初期化する | |
_soundListSettings = new List<SoundListSetting>(); | |
for (int i = 0; i < sources.Length; i++) | |
{ | |
_soundListSettings.Add(new SoundListSetting()); | |
_soundListSettings[i].soundName = sources[i].clip.name; | |
_soundListSettings[i].targetAudioClip = sources[i].clip; | |
_soundListSettings[i].targetAudioSource = sources[i]; | |
} | |
//デバッグを非実行中にも行えるようにここでも辞書を初期化する | |
initDictionary(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OtoFuda.RythmSystem; | |
using UnityEditor; | |
using UnityEngine; | |
[CustomEditor (typeof(SoundManager))] | |
public class SoundManagerInspector : Editor | |
{ | |
SoundManager _soundManager = null; | |
void OnEnable () | |
{ | |
//Character コンポーネントを取得 | |
_soundManager = (SoundManager) target; | |
} | |
//拡張インスペクタ | |
public override void OnInspectorGUI () | |
{ | |
base.OnInspectorGUI(); | |
EditorGUILayout.BeginHorizontal(); | |
_soundManager.testPlayAudioClipName = EditorGUILayout.TextField("登録名からサウンドを再生するテスト",_soundManager.testPlayAudioClipName); | |
if (GUILayout.Button("SoundTest By Name")) | |
{ | |
soundTestByName(_soundManager.testPlayAudioClipName); | |
} | |
EditorGUILayout.EndHorizontal(); | |
EditorGUILayout.BeginHorizontal(); | |
_soundManager.testPlayAudioClipIdx = EditorGUILayout.IntField("インデックスからサウンドを再生するテスト", _soundManager.testPlayAudioClipIdx); | |
if (GUILayout.Button("SoundTest By Index")) | |
{ | |
soundTestByIndex(_soundManager.testPlayAudioClipIdx); | |
} | |
EditorGUILayout.EndHorizontal(); | |
} | |
//SoundListSettingの名前から再生するテスト | |
private void soundTestByName(string soundName) | |
{ | |
_soundManager.initDictionary(); | |
//辞書に含まれるかどうかを調べる | |
if (!_soundManager.soundListDictionary.ContainsKey(soundName)) | |
{ | |
Debug.LogError("辞書に登録されていない名前のAudioClipを再生しようとしました。処理を中止します。"); | |
return; | |
} | |
var playAudioSource = _soundManager.playSound(soundName); | |
playAudioSource.Play(); | |
Debug.Log(soundName +"を再生しました"); | |
} | |
//SoundListSettingのインデックスから再生するテスト | |
private void soundTestByIndex(int soundIndex) | |
{ | |
if (soundIndex < 0 ||_soundManager._soundListSettings.Count <= soundIndex) | |
{ | |
Debug.LogError("配列参照外エラーが起こりそうだったので処理を中断します"); | |
return; | |
} | |
var playAudioSource = _soundManager.playSound(soundIndex); | |
playAudioSource.Play(); | |
Debug.Log(_soundManager._soundListSettings[soundIndex].soundName +"を再生しました"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment