Last active
June 11, 2020 18:53
-
-
Save beardordie/a57c57d6cd1f50d870cc7ed8c926ba2f to your computer and use it in GitHub Desktop.
Simple Audio Event - supports multiple clips per event, pitch and volume variance - with Custom Editor
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; | |
using System.Collections; | |
using UnityEngine; | |
using UnityEngine.Audio; | |
/// <summary> | |
/// This is a template to create a new Simple Audio Event. | |
/// It supports an array of audio clips, pitch and volume variance, | |
/// and a custom editor that shows a button to preview a random sound | |
/// from the array with the selected pitch and volume variance. | |
/// Examples: PlayerHurtGrunt, EnemyHurtGrunt, CoinCollectedSound, | |
/// Weapon1Shot, Weapon2Shot, Misfires, etc. | |
/// | |
/// How to Utilize: | |
/// Once you've created a Scriptable Object from this asset menu and set it up, | |
/// in script all you have to do is serialize a SimpleAudioEvent and then | |
/// call its Play method and pass in an AudioSource as its only parameter | |
/// | |
/// Example: | |
/// [SerializeField] SimpleAudioEvent audioEvent; | |
/// audioEvent.Play(audioSource); | |
/// | |
/// It's that easy. | |
/// | |
/// </summary> | |
[CreateAssetMenu(menuName = "Audio Events/Simple")] | |
public class SimpleAudioEvent : ScriptableObject | |
{ | |
[SerializeField] AudioClip[] clips = new AudioClip[0]; | |
[SerializeField] RangedFloat volume = new RangedFloat(1, 1); | |
[MinMaxRange(0f,2f)] [SerializeField] RangedFloat pitch = new RangedFloat(1, 1); | |
[MinMaxRange(0f, 1000f)] [SerializeField] RangedFloat distance = new RangedFloat(1, 1000); | |
[Tooltip("Optional")] [SerializeField] AudioMixerGroup mixer; | |
public void Play(AudioSource source, float delay=0f) | |
{ | |
source.outputAudioMixerGroup = mixer; | |
int clipIndex = UnityEngine.Random.Range(0, clips.Length); | |
source.clip = clips[clipIndex]; | |
source.pitch = UnityEngine.Random.Range(pitch.minValue, pitch.maxValue); | |
source.volume = UnityEngine.Random.Range(volume.minValue, volume.maxValue); | |
source.minDistance = distance.minValue; | |
source.maxDistance = distance.maxValue; | |
if(delay>0f) | |
{ | |
source.PlayDelayed(delay); | |
} | |
else | |
{ | |
source.Play(); | |
} | |
} | |
public int NumClips() | |
{ | |
return clips.Length; | |
} | |
} | |
// place the following in SimpleAudioEventDrawer.cs in an Editor folder | |
using UnityEngine; | |
using UnityEditor; | |
[CustomEditor(typeof(SimpleAudioEvent))] | |
public class SimpleAudioEventEditor : Editor | |
{ | |
private AudioSource previewSource; | |
public void OnEnable() | |
{ | |
var audioObject = EditorUtility.CreateGameObjectWithHideFlags( | |
"Audio Preview", | |
HideFlags.HideAndDontSave, | |
typeof(AudioSource)); | |
previewSource = audioObject.GetComponent<AudioSource>(); | |
} | |
private void OnDisable() | |
{ | |
DestroyImmediate(previewSource.gameObject); | |
} | |
public override void OnInspectorGUI() | |
{ | |
DrawDefaultInspector(); | |
EditorGUI.BeginDisabledGroup(serializedObject.isEditingMultipleObjects); | |
if(GUILayout.Button("Preview")) | |
{ | |
SimpleAudioEvent simpleTarget = (SimpleAudioEvent)target; | |
simpleTarget.Play(previewSource); | |
} | |
EditorGUI.EndDisabledGroup(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment