/* * ----------------------- * StopSound() * used in the editor * ----------------------- */ static public void StopSound(string soundFxName) { if (theAudioManager == null) { if (!FindAudioManager()) { return; } } SoundFX soundFX = FindSoundFX(soundFxName, true); if (soundFX == null) { return; } AudioClip clip = soundFX.GetClip(); if (clip != null) { Assembly unityEditorAssembly = typeof(AudioImporter).Assembly; Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil"); MethodInfo method = audioUtilClass.GetMethod( "StopClip", BindingFlags.Static | BindingFlags.Public, null, new System.Type[] { typeof(AudioClip) }, null); method.Invoke(null, new object[] { clip }); } }
/* * ----------------------- * IsSoundPlaying() * used in the editor * ----------------------- */ static public bool IsSoundPlaying(string soundFxName) { if (theAudioManager == null) { if (!FindAudioManager()) { return(false); } } SoundFX soundFX = FindSoundFX(soundFxName, true); if (soundFX == null) { return(false); } AudioClip clip = soundFX.GetClip(); if (clip != null) { Assembly unityEditorAssembly = typeof(AudioImporter).Assembly; Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil"); MethodInfo method = audioUtilClass.GetMethod( "IsClipPlaying", BindingFlags.Static | BindingFlags.Public, null, new System.Type[] { typeof(AudioClip) }, null); return(Convert.ToBoolean(method.Invoke(null, new object[] { clip }))); } return(false); }
/* * ----------------------- * PlaySound() * ----------------------- */ static public int PlaySound(SoundFX soundFX, EmitterChannel src = EmitterChannel.Any, float delay = 0.0f) { if (!SoundEnabled) { return(-1); } return(PlaySoundAt((staticListenerPosition != null) ? staticListenerPosition.position : Vector3.zero, soundFX, src, delay)); }
/* * ----------------------- * Init() * ----------------------- */ void Init() { // look up the actual SoundFX object soundFXCached = AudioManager.FindSoundFX(soundFXName); if (soundFXCached == null) { soundFXCached = AudioManager.FindSoundFX(string.Empty); } initialized = true; }
/* * ----------------------- * PlaySoundAt() * ----------------------- */ static public int PlaySoundAt(Vector3 position, SoundFX soundFX, EmitterChannel src = EmitterChannel.Any, float delay = 0.0f, float volumeOverride = 1.0f, float pitchMultiplier = 1.0f) { if (!SoundEnabled) { return(-1); } AudioClip clip = soundFX.GetClip(); if (clip == null) { return(-1); } // check the distance from the local player and ignore sounds out of range if (staticListenerPosition != null) { float distFromListener = (staticListenerPosition.position - position).sqrMagnitude; if (distFromListener > theAudioManager.audioMaxFallOffDistanceSqr) { return(-1); } if (distFromListener > soundFX.MaxFalloffDistSquared) { return(-1); } } // check max playing sounds if (soundFX.ReachedGroupPlayLimit()) { if (theAudioManager.verboseLogging) { Debug.Log("[AudioManager] PlaySoundAt() with " + soundFX.name + " skipped due to group play limit"); } return(-1); } int idx = FindFreeEmitter(src, soundFX.priority); if (idx == -1) { // no free emitters - should only happen on very low priority sounds return(-1); } SoundEmitter emitter = theAudioManager.soundEmitters[idx]; // make sure to detach the emitter from a previous parent emitter.ResetParent(soundEmitterParent.transform); emitter.gameObject.SetActive(true); // set up the sound emitter AudioSource audioSource = emitter.audioSource; ONSPAudioSource osp = emitter.osp; audioSource.enabled = true; audioSource.volume = Mathf.Clamp01(Mathf.Clamp01(theAudioManager.volumeSoundFX * soundFX.volume) * volumeOverride * soundFX.GroupVolumeOverride); audioSource.pitch = soundFX.GetPitch() * pitchMultiplier; audioSource.time = 0.0f; audioSource.spatialBlend = 1.0f; audioSource.rolloffMode = soundFX.falloffCurve; if (soundFX.falloffCurve == AudioRolloffMode.Custom) { audioSource.SetCustomCurve(AudioSourceCurveType.CustomRolloff, soundFX.volumeFalloffCurve); } audioSource.SetCustomCurve(AudioSourceCurveType.ReverbZoneMix, soundFX.reverbZoneMix); audioSource.dopplerLevel = 0; audioSource.clip = clip; audioSource.spread = soundFX.spread; audioSource.loop = soundFX.looping; audioSource.mute = false; audioSource.minDistance = soundFX.falloffDistance.x; audioSource.maxDistance = soundFX.falloffDistance.y; audioSource.outputAudioMixerGroup = soundFX.GetMixerGroup(AudioManager.EmitterGroup); // set the play time so we can check when sounds are done emitter.endPlayTime = Time.time + clip.length + delay; // cache the default volume for fading emitter.defaultVolume = audioSource.volume; // sound priority emitter.priority = soundFX.priority; // reset this emitter.onFinished = null; // update the sound group limits emitter.SetPlayingSoundGroup(soundFX.Group); // add to the playing list if (src == EmitterChannel.Any) { theAudioManager.playingEmitters.AddUnique(emitter); } // OSP properties if (osp != null) { osp.EnableSpatialization = soundFX.ospProps.enableSpatialization; osp.EnableRfl = theAudioManager.enableSpatializedFastOverride || soundFX.ospProps.useFastOverride ? true : false; osp.Gain = soundFX.ospProps.gain; osp.UseInvSqr = soundFX.ospProps.enableInvSquare; osp.Near = soundFX.ospProps.invSquareFalloff.x; osp.Far = soundFX.ospProps.invSquareFalloff.y; audioSource.spatialBlend = (soundFX.ospProps.enableSpatialization) ? 1.0f : 0.8f; // make sure to set the properties in the audio source before playing osp.SetParameters(ref audioSource); } audioSource.transform.position = position; if (theAudioManager.verboseLogging) { Debug.Log("[AudioManager] PlaySoundAt() channel = " + idx + " soundFX = " + soundFX.name + " volume = " + emitter.volume + " Delay = " + delay + " time = " + Time.time + "\n"); } // play the sound if (delay > 0f) { audioSource.PlayDelayed(delay); } else { audioSource.Play(); } return(idx); }
/* * ----------------------- * DrawCategories() * ----------------------- */ void DrawCategories(Event e) { // do any housework before we start drawing if (moveQueued) { // make a temp copy List <SoundFX> origSoundList = new List <SoundFX>(audioManager.soundGroupings[origGroup].soundList); SoundFX temp = origSoundList[origIndex]; List <SoundFX> moveToSoundList = new List <SoundFX>(audioManager.soundGroupings[moveToGroup].soundList); // add it to the move to group moveToSoundList.Add(temp); audioManager.soundGroupings[moveToGroup].soundList = moveToSoundList.ToArray(); // and finally, remove it from the original group origSoundList.RemoveAt(origIndex); audioManager.soundGroupings[origGroup].soundList = origSoundList.ToArray(); Debug.Log("> Moved '" + temp.name + "' from " + "'" + audioManager.soundGroupings[origGroup].name + "' to '" + audioManager.soundGroupings[moveToGroup].name); MarkDirty(); moveQueued = false; } // switch to the next group if (nextGroup > -1) { selectedGroup = nextGroup; nextGroup = -1; } // add a sound if (addSound) { List <SoundFX> soundList = new List <SoundFX>(audioManager.soundGroupings[selectedGroup].soundList); SoundFX soundFX = new SoundFX(); soundFX.name = audioManager.soundGroupings[selectedGroup].name.ToLower() + "_new_unnamed_sound_fx"; soundList.Add(soundFX); audioManager.soundGroupings[selectedGroup].soundList = soundList.ToArray(); MarkDirty(); addSound = false; } // sort the sounds if (sortSounds) { List <SoundFX> soundList = new List <SoundFX>(audioManager.soundGroupings[selectedGroup].soundList); soundList.Sort(delegate(SoundFX sfx1, SoundFX sfx2) { return(string.Compare(sfx1.name, sfx2.name)); }); audioManager.soundGroupings[selectedGroup].soundList = soundList.ToArray(); MarkDirty(); sortSounds = false; } // delete a sound if (deleteSoundIdx > -1) { List <SoundFX> soundList = new List <SoundFX>(audioManager.soundGroupings[selectedGroup].soundList); soundList.RemoveAt(deleteSoundIdx); audioManager.soundGroupings[selectedGroup].soundList = soundList.ToArray(); MarkDirty(); deleteSoundIdx = -1; } // duplicate a sound if (dupeSoundIdx > -1) { List <SoundFX> soundList = new List <SoundFX>(audioManager.soundGroupings[selectedGroup].soundList); SoundFX origSoundFX = soundList[dupeSoundIdx]; // clone this soundFX string json = JsonUtility.ToJson(origSoundFX); SoundFX soundFX = JsonUtility.FromJson <SoundFX>(json); soundFX.name += "_duplicated"; soundList.Insert(dupeSoundIdx + 1, soundFX); audioManager.soundGroupings[selectedGroup].soundList = soundList.ToArray(); MarkDirty(); dupeSoundIdx = -1; } if (e.type == EventType.Repaint) { groups.Clear(); } GUILayout.Space(6f); Color defaultColor = GUI.contentColor; BeginContents(); if (DrawHeader("Sound FX Groups", true)) { EditorGUILayout.BeginVertical(GUI.skin.box); soundGroups.Clear(); for (int i = 0; i < audioManager.soundGroupings.Length; i++) { soundGroups.Add(audioManager.soundGroupings[i]); } for (int i = 0; i < soundGroups.size; i++) { EditorGUILayout.BeginHorizontal(); { if (i == selectedGroup) { GUI.contentColor = (i == editGroup) ? Color.white : Color.yellow; } else { GUI.contentColor = defaultColor; } if ((e.type == EventType.KeyDown) && ((e.keyCode == KeyCode.Return) || (e.keyCode == KeyCode.KeypadEnter))) { // toggle editing if (editGroup >= 0) { editGroup = -1; } Event.current.Use(); } if (i == editGroup) { soundGroups[i].name = GUILayout.TextField(soundGroups[i].name, GUILayout.MinWidth(Screen.width - 80f)); } else { GUILayout.Label(soundGroups[i].name, (i == selectedGroup) ? EditorStyles.whiteLabel : EditorStyles.label, GUILayout.ExpandWidth(true)); } GUILayout.FlexibleSpace(); if (GUILayout.Button(GUIContent.none, "OL Minus", GUILayout.Width(17f))) // minus button { if (EditorUtility.DisplayDialog("Delete '" + soundGroups[i].name + "'", "Are you sure you want to delete the selected sound group?", "Continue", "Cancel")) { soundGroups.RemoveAt(i); MarkDirty(); } } } EditorGUILayout.EndHorizontal(); // build a list of items Rect lastRect = GUILayoutUtility.GetLastRect(); if (e.type == EventType.Repaint) { groups.Add(new ItemRect(i, lastRect, null)); } if ((e.type == EventType.MouseDown) && lastRect.Contains(e.mousePosition)) { if ((i != selectedGroup) || (e.clickCount == 2)) { nextGroup = i; if (e.clickCount == 2) { editGroup = i; } else if (editGroup != nextGroup) { editGroup = -1; } Repaint(); } } } // add the final plus button EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button(GUIContent.none, "OL Plus", GUILayout.Width(17f))) // plus button { soundGroups.Add(new SoundGroup("unnamed sound group")); selectedGroup = editGroup = soundGroups.size - 1; MarkDirty(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); // reset the color GUI.contentColor = defaultColor; // the sort and import buttons EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("Sort", GUILayout.Width(70f))) { soundGroups.Sort(delegate(SoundGroup sg1, SoundGroup sg2) { return(string.Compare(sg1.name, sg2.name)); }); MarkDirty(); } EditorGUILayout.EndHorizontal(); // draw a rect around the selected item if ((selectedGroup >= 0) && (selectedGroup < groups.size)) { EditorGUI.DrawRect(groups[selectedGroup].rect, new Color(1f, 1f, 1f, 0.06f)); } // finally move the sound groups back into the audio manager if (soundGroups.size > 0) { audioManager.soundGroupings = soundGroups.ToArray(); } // calculate the drop area rect if ((e.type == EventType.Repaint) && (groups.size > 0)) { dropArea.x = groups[0].rect.x; dropArea.y = groups[0].rect.y; dropArea.width = groups[0].rect.width; dropArea.height = (groups[groups.size - 1].rect.y - groups[0].rect.y) + groups[groups.size - 1].rect.height; } } // draw the sound group properties now DrawSoundGroupProperties(); EndContents(); EditorGUILayout.HelpBox("Create and delete sound groups by clicking + and - respectively. Double click to rename sound groups. Drag and drop sounds from below to the groups above to move them.", MessageType.Info); }