Last active
December 19, 2020 15:58
-
-
Save AbrahamArmasCordero/f4b43862826ffca5ad78bf29d09dcc3b to your computer and use it in GitHub Desktop.
Unite 2016 SimpleAudioEvent
This file contains hidden or 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
///////// AudioEvent.cs ///////// | |
//Public use code: https://bitbucket.org/richardfine/scriptableobjectdemo/commits/03a730f1b0581c0d424268bc03e33dac21f34248?w=0#chg-Assets/ScriptableObject/Audio/MinMaxRangeAttribute.cs | |
//Created by: Richard Fine https://bitbucket.org/richardfine/ | |
using UnityEngine; | |
public abstract class AudioEvent : ScriptableObject | |
{ | |
public abstract void Play(AudioSource source); | |
} |
This file contains hidden or 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
///////// MinMaxRangeAttribute.cs ///////// | |
//Public use code: https://bitbucket.org/richardfine/scriptableobjectdemo/commits/03a730f1b0581c0d424268bc03e33dac21f34248?w=0#chg-Assets/ScriptableObject/Audio/MinMaxRangeAttribute.cs | |
//Created by: Richard Fine https://bitbucket.org/richardfine/ | |
using System; | |
public class MinMaxRangeAttribute : Attribute { | |
public MinMaxRangeAttribute(float min, float max) | |
{ | |
Min = min; | |
Max = max; | |
} | |
public float Min { get; private set; } | |
public float Max { get; private set; } | |
} | |
/*Ejemplo de Implementación: | |
[MinMaxRange(0, 2)] | |
public RangedFloat pitch; | |
*/ |
This file contains hidden or 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
///////// RangedFloat.cs ///////// | |
//Public use code: https://bitbucket.org/richardfine/scriptableobjectdemo/commits/03a730f1b0581c0d424268bc03e33dac21f34248?w=0#chg-Assets/ScriptableObject/Audio/MinMaxRangeAttribute.cs | |
//Created by: Richard Fine https://bitbucket.org/richardfine/ | |
using System; | |
[Serializable] | |
public struct RangedFloat | |
{ | |
public RangedFloat(float min, float max) | |
{ | |
minValue = min; | |
maxValue = max; | |
} | |
public float minValue; | |
public float maxValue; | |
} |
This file contains hidden or 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
///////// RangedFloatDrawer.cs ///////// | |
//Public use code: https://bitbucket.org/richardfine/scriptableobjectdemo/commits/03a730f1b0581c0d424268bc03e33dac21f34248?w=0#chg-Assets/ScriptableObject/Audio/MinMaxRangeAttribute.cs | |
//Created by: Richard Fine https://bitbucket.org/richardfine/ | |
using UnityEngine; | |
using UnityEditor; | |
[CustomPropertyDrawer(typeof(RangedFloat), true)] | |
public class RangedFloatDrawer : PropertyDrawer | |
{ | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
label = EditorGUI.BeginProperty(position, label, property); | |
position = EditorGUI.PrefixLabel(position, label); | |
SerializedProperty minProp = property.FindPropertyRelative("minValue"); | |
SerializedProperty maxProp = property.FindPropertyRelative("maxValue"); | |
float minValue = minProp.floatValue; | |
float maxValue = maxProp.floatValue; | |
float rangeMin = 0; | |
float rangeMax = 1; | |
var ranges = (MinMaxRangeAttribute[])fieldInfo.GetCustomAttributes(typeof(MinMaxRangeAttribute), true); | |
if (ranges.Length > 0) | |
{ | |
rangeMin = ranges[0].Min; | |
rangeMax = ranges[0].Max; | |
} | |
const float rangeBoundsLabelWidth = 40f; | |
var rangeBoundsLabel1Rect = new Rect(position); | |
rangeBoundsLabel1Rect.width = rangeBoundsLabelWidth; | |
GUI.Label(rangeBoundsLabel1Rect, new GUIContent(minValue.ToString("F2"))); | |
position.xMin += rangeBoundsLabelWidth; | |
var rangeBoundsLabel2Rect = new Rect(position); | |
rangeBoundsLabel2Rect.xMin = rangeBoundsLabel2Rect.xMax - rangeBoundsLabelWidth; | |
GUI.Label(rangeBoundsLabel2Rect, new GUIContent(maxValue.ToString("F2"))); | |
position.xMax -= rangeBoundsLabelWidth; | |
EditorGUI.BeginChangeCheck(); | |
EditorGUI.MinMaxSlider(position, ref minValue, ref maxValue, rangeMin, rangeMax); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
minProp.floatValue = minValue; | |
maxProp.floatValue = maxValue; | |
} | |
EditorGUI.EndProperty(); | |
} | |
} |
This file contains hidden or 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
///////// SimpleAudioEvent.cs ///////// | |
//Public use code: https://bitbucket.org/richardfine/scriptableobjectdemo/commits/03a730f1b0581c0d424268bc03e33dac21f34248?w=0#chg-Assets/ScriptableObject/Audio/MinMaxRangeAttribute.cs | |
//Created by: Richard Fine https://bitbucket.org/richardfine/ | |
using UnityEngine; | |
[CreateAssetMenu(menuName="Audio Events/Simple")] | |
public class SimpleAudioEvent : AudioEvent | |
{ | |
public AudioClip[] clips; | |
public RangedFloat volume; | |
[MinMaxRange(0, 2)] | |
public RangedFloat pitch; | |
public override void Play(AudioSource source) | |
{ | |
if (clips.Length == 0) return; | |
source.clip = clips[Random.Range(0, clips.Length)]; | |
source.volume = Random.Range(volume.minValue, volume.maxValue); | |
source.pitch = Random.Range(pitch.minValue, pitch.maxValue); | |
source.PlayOneShot(source.clip); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment