/// <summary> /// Stops any current music and play new clip. /// </summary> public void PlayMusic(AudioClip clip, float volumeScale = 1) { musicSourceA.Stop(); musicSourceB.Stop(); musicSourceA.clip = clip; musicSourceA.Play(); activeMusicChannel = MusicChannel.A; }
/// <summary> /// Crossfades between current music and new clip /// </summary> public void CrossFadeMusic(AudioClip clip, float fadeDuration, float volumeScale = 1) { if (activeMusicChannel == MusicChannel.A) { activeMusicChannel = MusicChannel.B; musicSourceB.clip = clip; musicSourceB.Play(); } else if (activeMusicChannel == MusicChannel.B) { activeMusicChannel = MusicChannel.A; musicSourceA.clip = clip; musicSourceA.Play(); } StartCoroutine(AnimateCrossFadeMusic(volumeScale, fadeDuration)); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // Acquire target channel if necessary if (targetChannel == null) { targetChannel = property.objectReferenceValue as MusicChannel; } // If channel is still null, channel was deleted if (targetChannel == null) { return; } // Get serialized reference to target channel if necessary if (serializedChannel == null) { serializedChannel = new SerializedObject(property.objectReferenceValue); } serializedChannel.Update(); // Get serialized properties volumeProp = serializedChannel.FindProperty("volume"); // Clips box GUILayout.BeginVertical(EditorStyles.helpBox); if (expanded = EditorGUILayout.Foldout(expanded, "Channel: " + targetChannel.name, true)) { EditorGUILayout.Space(); EditorGUILayout.LabelField("Clips"); EditorGUI.indentLevel++; clipsProp = serializedChannel.FindProperty("clips"); for (int i = 0; i < clipsProp.arraySize; i++) { var clip = clipsProp.GetArrayElementAtIndex(i); var clipProp = clip.FindPropertyRelative("_clip"); var barsProp = clip.FindPropertyRelative("_bars"); EditorGUILayout.BeginHorizontal(); //var changedClip = EditorGUILayout.ObjectField(clip.Clip, typeof(AudioClip), false) as AudioClip; EditorGUILayout.PropertyField(clipProp); if (clipProp.objectReferenceValue == null) { targetChannel.Clips.RemoveAt(i); } //else _targetChannel.Clips[i].Clip = changedClip; //clip.Bars = EditorGUILayout.IntField (clip.Bars); EditorGUILayout.PropertyField(barsProp); EditorGUILayout.EndHorizontal(); } var newClip = EditorGUILayout.ObjectField(null, typeof(AudioClip), false) as AudioClip; if (newClip != null) { targetChannel.AddClip(newClip); } EditorGUI.indentLevel--; EditorGUILayout.Space(); EditorGUILayout.PropertyField(volumeProp); } EditorGUILayout.EndVertical(); serializedChannel.ApplyModifiedProperties(); }