void Play(DeAudioClipData data) { if (data.clip == null) { return; } _previewSource.clip = data.clip; _previewSource.volume = data.volume; _previewSource.Play(); }
static void PlayButton(Rect r, DeAudioClipData value) { if (GUI.Button(r, "►", Styles.btPlay)) { DeEditorSoundUtils.StopAll(); if (value.clip != null) { DeEditorSoundUtils.Play(value.clip); } } }
void OnEnable() { _src = target as DeAudioCollection; ConnectToAudioSource(); float lineH = EditorGUIUtility.singleLineHeight; float listVSpacer = 2f; _dataList = new ReorderableList(serializedObject, serializedObject.FindProperty("data"), true, true, true, true); _dataList.elementHeight = lineH * 2 + listVSpacer + 10; _dataList.drawHeaderCallback = rect => { GUI.Label(rect, "Data", DeGUI.styles.label.bold); }; _dataList.onAddCallback = list => { // Force volume to 1 (otherwise set to 0 by list default) and audioClip to null ReorderableList.defaultBehaviours.DoAddButton(list); int addedIndex = list.serializedProperty.arraySize - 1; list.serializedProperty.GetArrayElementAtIndex(addedIndex).FindPropertyRelative("clip").objectReferenceValue = null; list.serializedProperty.GetArrayElementAtIndex(addedIndex).FindPropertyRelative("volume").floatValue = 1; }; _dataList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => { DeAudioClipData item = _src.data[index]; EditorGUIUtility.labelWidth = 50; float ry = rect.y + 2; item.clip = EditorGUI.ObjectField(new Rect(rect.x, ry, rect.width, lineH), "Clip", item.clip, typeof(AudioClip), false) as AudioClip; ry += lineH + listVSpacer; float btW = 25; float prevVolume = item.volume; item.volume = EditorGUI.Slider(new Rect(rect.x, ry, rect.width - btW * 2 - 5, lineH), "Volume", item.volume, 0, 1); if (Math.Abs(item.volume - prevVolume) > float.Epsilon && _src.previewSource.isPlaying && _src.previewSource.clip == item.clip) { _src.previewSource.volume = item.volume; } if (GUI.Button(new Rect(rect.x + (rect.width - btW * 2), ry - 1, btW, lineH), "►", DeGUI.styles.button.tool)) { Play(item); } if (GUI.Button(new Rect(rect.x + (rect.width - btW), ry - 1, btW, lineH), "■", DeGUI.styles.button.tool)) { Stop(item); } }; }
void Stop(DeAudioClipData data) { _src.previewSource.Stop(); _src.previewSource.clip = null; }
public static DeAudioClipData DeAudioClip(Rect rect, GUIContent label, DeAudioClipData value, bool allowGroupChange = true, DeAudioClipGUIMode mode = DeAudioClipGUIMode.Full) { Styles.Init(); bool hasGroup = false; bool hasPlayStopButtonsInFirstRow = false; bool hasPlayStopButtonsInSecondRow = false; bool hasVolume = false; bool hasLoopInSecondRow = false; bool hasLoopInThirdRow = false; bool hasPitch = false; if (mode != DeAudioClipGUIMode.ClipOnly) { if (mode != DeAudioClipGUIMode.CompactPreviewOnly && mode != DeAudioClipGUIMode.FullNoGroup) { hasGroup = true; } switch (mode) { case DeAudioClipGUIMode.CompactPreviewOnly: case DeAudioClipGUIMode.CompactWithGroupAndPreview: hasPlayStopButtonsInFirstRow = true; break; case DeAudioClipGUIMode.VolumeWithPreview: case DeAudioClipGUIMode.VolumeAndLoopsWithPreview: case DeAudioClipGUIMode.FullNoGroup: case DeAudioClipGUIMode.Full: hasVolume = true; hasPlayStopButtonsInSecondRow = true; if (mode == DeAudioClipGUIMode.VolumeAndLoopsWithPreview) { hasLoopInSecondRow = true; } if (mode == DeAudioClipGUIMode.FullNoGroup || mode == DeAudioClipGUIMode.Full) { hasPitch = true; hasLoopInThirdRow = true; } break; } } Rect lineR = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight); // First line if (hasPlayStopButtonsInFirstRow) { Rect btStopR = new Rect(lineR.xMax - _ControlButtonW, lineR.y, _ControlButtonW, lineR.height); Rect btPlayR = new Rect(btStopR.x - _ControlButtonW, btStopR.y, btStopR.width, btStopR.height); lineR.width -= _ControlButtonW * 2; PlayButton(btPlayR, value); StopButton(btStopR); } if (hasGroup) { Rect groupR = new Rect(lineR.xMax - _GroupW, lineR.y, _GroupW, lineR.height); lineR.width -= groupR.width; using (new EditorGUI.DisabledGroupScope(!allowGroupChange)) { DeAudioGroupId newGroupId = (DeAudioGroupId)EditorGUI.EnumPopup(groupR, value.groupId); if (allowGroupChange) { value.groupId = newGroupId; } } } Rect clipR = lineR; value.clip = label.text == "" ? EditorGUI.ObjectField(clipR, value.clip, typeof(AudioClip), false) as AudioClip : EditorGUI.ObjectField(clipR, label, value.clip, typeof(AudioClip), false) as AudioClip; // Second line lineR.y = lineR.yMax + VSpace; lineR.width = rect.width; if (hasPlayStopButtonsInSecondRow) { Rect btStopR = new Rect(lineR.xMax - _ControlButtonW, lineR.y, _ControlButtonW, lineR.height); Rect btPlayR = new Rect(btStopR.x - _ControlButtonW, btStopR.y, btStopR.width, btStopR.height); lineR.width -= _ControlButtonW * 2; PlayButton(btPlayR, value); StopButton(btStopR); } if (hasLoopInSecondRow) { Rect loopR = new Rect(lineR.xMax - _LoopToggleW, lineR.y, _LoopToggleW, lineR.height); lineR.width -= loopR.width; LoopToggle(loopR, value); } if (hasVolume) { Rect volumeR = lineR; value.volume = EditorGUI.Slider(volumeR, "└ Volume", value.volume, 0, 1); } // Third line lineR.y = lineR.yMax + VSpace; lineR.width = rect.width; if (hasLoopInThirdRow) { Rect loopR = new Rect(lineR.xMax - _LoopToggleW, lineR.y, _LoopToggleW, lineR.height); lineR.width -= loopR.width; LoopToggle(loopR, value); } if (hasPitch) { Rect pitchR = lineR; value.pitch = EditorGUI.Slider(pitchR, "└ Pitch", value.pitch, 0, 3); } return(value); }
public static DeAudioClipData DeAudioClip(Rect rect, string label, DeAudioClipData value, bool allowGroupChange = true, DeAudioClipGUIMode mode = DeAudioClipGUIMode.Full) { return(DeAudioClip(rect, new GUIContent(label, ""), value, allowGroupChange, mode)); }
static void LoopToggle(Rect r, DeAudioClipData value) { value.loop = DeGUI.ToggleButton(r, value.loop, "Loop"); }
public static DeAudioClipData DeAudioClip(GUIContent label, DeAudioClipData value, bool allowGroupChange = true, DeAudioClipGUIMode mode = DeAudioClipGUIMode.Full) { Styles.Init(); GUIStyle style; switch (mode) { case DeAudioClipGUIMode.ClipOnly: case DeAudioClipGUIMode.Compact: case DeAudioClipGUIMode.CompactPreviewOnly: case DeAudioClipGUIMode.CompactWithGroupAndPreview: style = Styles.oneRow; break; case DeAudioClipGUIMode.VolumeWithPreview: case DeAudioClipGUIMode.VolumeAndLoopsWithPreview: style = Styles.twoRows; break; default: style = Styles.threeRows; break; } Rect r = GUILayoutUtility.GetRect(GUIContent.none, style); return(DeAudioGUI.DeAudioClip(r, label, value, allowGroupChange, mode)); // using (new GUILayout.HorizontalScope()) { // value.clip = EditorGUILayout.ObjectField(label, value.clip, typeof(AudioClip), false) as AudioClip; // if (mode != DeAudioClipGUIMode.ClipOnly) { // if (mode != DeAudioClipGUIMode.CompactPreviewOnly) { // using (new EditorGUI.DisabledGroupScope(!allowGroupChange)) { // DeAudioGroupId newGroupId = (DeAudioGroupId)EditorGUILayout.EnumPopup(value.groupId, GUILayout.Width(78)); // if (allowGroupChange) value.groupId = newGroupId; // } // } // if (mode == DeAudioClipGUIMode.CompactPreviewOnly || mode == DeAudioClipGUIMode.CompactWithGroupAndPreview) { // DeAudioGUI.PlayButton(value); // DeAudioGUI.StopButton(); // } // } // } // if (mode == DeAudioClipGUIMode.CompactPreviewOnly) GUILayout.Space(4); // if (mode == DeAudioClipGUIMode.VolumeWithPreview || mode == DeAudioClipGUIMode.VolumeAndLoopsWithPreview || mode == DeAudioClipGUIMode.Full) { // // Volume, pitch, play/pause/loop buttons // GUILayout.Space(-1); // using (new GUILayout.HorizontalScope()) { // value.volume = EditorGUILayout.Slider("└ Volume", value.volume, 0, 1); // if (mode == DeAudioClipGUIMode.VolumeAndLoopsWithPreview) DeAudioGUI.LoopToggle(value); // DeAudioGUI.PlayButton(value); // DeAudioGUI.StopButton(); // } // if (mode == DeAudioClipGUIMode.Full) { // GUILayout.Space(-1); // using (new GUILayout.HorizontalScope()) { // value.pitch = EditorGUILayout.Slider("└ Pitch", value.pitch, 0, 3); // DeAudioGUI.LoopToggle(value); // } // } else GUILayout.Space(4); // } else if (mode == DeAudioClipGUIMode.CompactWithGroupAndPreview) GUILayout.Space(4); // return value; }