Skip to content

Commit

Permalink
Merge pull request #2 from dimixar/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
dimixar authored Sep 8, 2017
2 parents ac7dd85 + 907c89c commit 99426d0
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace OSSC.Editor
public class SoundControllerEditor : UnityEditor.Editor
{
private const int NAME_ABV_LEN = 50;
private const float PITCH_RANGE_MAX = 3f;
private const float PITCH_RANGE_MIN = -3f;
private SoundController _ac;
private string categoryNameSearch = "";
private string _tagName = "";
Expand Down Expand Up @@ -218,7 +220,26 @@ private void DrawSoundItem(Model.SoundItem item, int index, Model.SoundItem[] it
{
item.clips[i] = (AudioClip)EditorGUILayout.ObjectField(item.clips[i], typeof(AudioClip), false);
}
item.volume = EditorGUILayout.Slider("Volume", item.volume, 0f, 1f);

item.isRandomVolume =
EditorGUILayout.ToggleLeft("Use Random Volume", item.isRandomVolume, EditorStyles.boldLabel);
if (!item.isRandomVolume)
item.volume = EditorGUILayout.Slider("Volume", item.volume, 0f, 1f);
else
{
EditorGUILayout.LabelField("Min Volume:", item.volumeRange.min.ToString(), EditorStyles.largeLabel);
EditorGUILayout.LabelField("Max Volume:", item.volumeRange.max.ToString(), EditorStyles.largeLabel);
EditorGUILayout.MinMaxSlider("Volume Range", ref item.volumeRange.min, ref item.volumeRange.max, 0f, 1f);
}

item.isRandomPitch =
EditorGUILayout.ToggleLeft("Use Random Pitch", item.isRandomPitch, EditorStyles.boldLabel);
if (item.isRandomPitch)
{
EditorGUILayout.LabelField("Min Pitch:", item.pitchRange.min.ToString(), EditorStyles.largeLabel);
EditorGUILayout.LabelField("Max Pitch:", item.pitchRange.max.ToString(), EditorStyles.largeLabel);
EditorGUILayout.MinMaxSlider("Pitch Range", ref item.pitchRange.min, ref item.pitchRange.max, PITCH_RANGE_MIN, PITCH_RANGE_MAX);
}
string nameAbv = "";
if (string.IsNullOrEmpty(item.name) == false)
nameAbv = item.name.Length > NAME_ABV_LEN ? item.name.Substring(0, NAME_ABV_LEN) : item.name;
Expand Down
16 changes: 16 additions & 0 deletions AudioController/Assets/OSSC/Source/Model/SoundItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,24 @@ public class SoundItem
public int tagID = -1;
public UnityEngine.Audio.AudioMixerGroup mixer;
public AudioClip[] clips;
public bool isRandomPitch;
public CustomRange pitchRange;
public bool isRandomVolume;
public CustomRange volumeRange;

[RangeAttribute(0f, 1f)]
public float volume = 1f;
}

[System.Serializable]
public class CustomRange
{
public float min = 1f;
public float max = 1f;

public float GetRandomRange()
{
return Random.Range(min, max);
}
}
}
28 changes: 25 additions & 3 deletions AudioController/Assets/OSSC/Source/SoundCue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,36 @@ private bool TryPlayNext()

private void PlayCurrentItem()
{
float realVolume = _data.sounds[_currentItem].volume * _data.categoryVolumes[_currentItem];
SoundItem item = _data.sounds[_currentItem];

float itemVolume = item.isRandomVolume
? item.volumeRange.GetRandomRange()
: item.volume;
float realVolume = itemVolume * _data.categoryVolumes[_currentItem];

float realPitch = item.isRandomPitch
? item.pitchRange.GetRandomRange()
: 1f;

if (_currentItem == _data.sounds.Length - 1)
{
AudioObject.Setup(_data.sounds[_currentItem].name, GetRandomClip( _data.sounds[_currentItem].clips ), realVolume, _data.fadeInTime, _data.fadeOutTime, _data.sounds[_currentItem].mixer);
AudioObject.Setup(
item.name,
GetRandomClip( item.clips ),
realVolume,
_data.fadeInTime,
_data.fadeOutTime,
item.mixer,
realPitch);
}
else
{
AudioObject.Setup(_data.sounds[_currentItem].name, GetRandomClip( _data.sounds[_currentItem].clips ), realVolume, mixer:_data.sounds[_currentItem].mixer);
AudioObject.Setup(
item.name,
GetRandomClip( item.clips ),
realVolume,
mixer: item.mixer,
pitch: realPitch);
}
AudioObject.Play();
}
Expand Down
6 changes: 5 additions & 1 deletion AudioController/Assets/OSSC/Source/SoundObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class SoundObject : MonoBehaviour, IPoolable
private float _fadeInTime;
private float _fadeOutTime;
private float _volume;
private float _pitch;
private bool _isDespawnOnFinishedPlaying = true;
#endregion

Expand All @@ -50,7 +51,7 @@ public string ID {
get { return _id; }
}

public void Setup(string id, AudioClip clip, float volume, float fadeInTime = 0f, float fadeOutTime = 0f, AudioMixerGroup mixer = null)
public void Setup(string id, AudioClip clip, float volume, float fadeInTime = 0f, float fadeOutTime = 0f, AudioMixerGroup mixer = null, float pitch = 1f)
{
_id = id;
_clip = clip;
Expand All @@ -62,6 +63,7 @@ public void Setup(string id, AudioClip clip, float volume, float fadeInTime = 0f
_source.time = 0f;
_source.outputAudioMixerGroup = mixer;
_volume = volume;
_pitch = pitch;
_fadeInTime = fadeInTime;
_fadeOutTime = fadeOutTime;
}
Expand All @@ -72,6 +74,7 @@ public void Play()
_source = GetComponent<AudioSource>();
_source.clip = _clip;
gameObject.SetActive(true);
_source.pitch = _pitch;
StartCoroutine(FadeRoutine(_fadeInTime, _volume));
_source.Play();
_isFree = false;
Expand Down Expand Up @@ -139,6 +142,7 @@ private IEnumerator StopRoutine()
_isFree = true;
_volume = 0f;
_source.time = 0f;
_source.pitch = 1f;

if (isDespawnOnFinishedPlaying)
_pool.Despawn(gameObject);
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Main Features:
- All data is saved on a scriptable object asset
- You can create multiple data assets and swap them easily
- Create whole cues of sounds with ease (very useful when you have dialog Voice Over)
- Add Random Pitch to sound items
- Add Random Volume to sound items
- It's not a singleton!!!

How to add it to your project and scene:
Expand Down

0 comments on commit 99426d0

Please sign in to comment.