/// <summary> Pause all the SoundyControllers that are currently playing </summary> public static void PauseAllControllers() { if (Instance.DebugComponent) { DDebug.Log("Pause All Controllers", Instance); } SoundyController.PauseAll(); }
/// <summary> Gets a SoundyController from the Pool, or creates a new one if all the available controllers are in use </summary> public static SoundyController GetControllerFromPool() { RemoveNullControllersFromThePool(); if (Pool.Count <= 0) { return(SoundyController.GetController()); //the pool does not have any controllers in it -> create and return a new controller } SoundyController controller = Pool[0]; //assign the first found controller Pool.Remove(controller); //remove the assigned controller from the pool controller.gameObject.SetActive(true); return(controller); //return a reference to the controller }
/// <summary> Puts the SoundyController back in the Pool </summary> /// <param name="controller"> The controller </param> public static void PutControllerInPool(SoundyController controller) { if (!Pool.Contains(controller)) { Pool.Add(controller); } controller.gameObject.SetActive(false); #if UNITY_EDITOR controller.transform.parent = Instance.transform; #endif if (Instance.DebugComponent) { DDebug.Log("Put '" + controller.name + "' Controller back in the Pool - " + Pool.Count + " Controllers Available", Instance); } }
/// <summary> Creates a set number of SoundyController and adds them to the Pool </summary> /// <param name="numberOfControllers"> How many controllers should be created </param> public static void PopulatePool(int numberOfControllers) { RemoveNullControllersFromThePool(); if (numberOfControllers < 1) { return; //sanity check } for (int i = 0; i < numberOfControllers; i++) { PutControllerInPool(SoundyController.GetController()); } if (Instance.DebugComponent) { DDebug.Log("Populate Pool - Added " + numberOfControllers + " Controllers to the Pool - " + Pool.Count + " Controllers Available", Instance); } }
/// <summary> Plays one of the sounds from the AudioClip list, with the set randomized values, at the specified position </summary> /// <param name="position"> The world position where this sound will be played at </param> /// <param name="outputAudioMixerGroup"> The output AudioMixerGroup that the sound will get routed through </param> public SoundyController Play(Vector3 position, AudioMixerGroup outputAudioMixerGroup = null) { SoundyController controller = SoundyPooler.GetControllerFromPool(); m_lastPlayedAudioData = GetAudioData(Mode); controller.SetSourceProperties(m_lastPlayedAudioData.AudioClip, RandomVolume, RandomPitch, Loop, SpatialBlend); controller.SetOutputAudioMixerGroup(outputAudioMixerGroup); controller.SetPosition(position); if (m_lastPlayedAudioData == null) { return(controller); } controller.gameObject.name = "[" + SoundName + "]-(" + m_lastPlayedAudioData.AudioClip.name + ")"; controller.Play(); return(controller); }
/// <summary> Put a SoundyController back in the Pool </summary> /// <param name="controller"> The controller </param> public static void PutControllerInPool(SoundyController controller) { if (controller == null) { return; } if (!Pool.Contains(controller)) { Pool.Add(controller); } controller.gameObject.SetActive(false); controller.transform.SetParent(Instance.transform); if (Instance.DebugComponent) { DDebug.Log("Put '" + controller.name + "' Controller in the Pool - " + Pool.Count + " Controllers Available", Instance); } }
/// <summary> Stops all SoundyControllers from playing, destroys the GameObjects they are attached to and clears the Pool </summary> /// <param name="keepMinimumNumberOfControllers"> Should there be a minimum set number of controllers in the pool left after clearing? </param> public static void ClearPool(bool keepMinimumNumberOfControllers = false) { if (keepMinimumNumberOfControllers) { RemoveNullControllersFromThePool(); //remove any null controllers (sanity check) if (Pool.Count <= MinimumNumberOfControllers) //make sure the minimum number of controllers are in the pool before killing them { if (Instance.DebugComponent) { DDebug.Log("Clear Pool - " + Pool.Count + " Controllers Available", Instance); } return; } int killedControllersCount = 0; for (int i = Pool.Count - 1; i >= MinimumNumberOfControllers; i--) //go through the pool { SoundyController controller = Pool[i]; Pool.Remove(controller); //remove controller from the pool controller.Kill(); //kill the controller killedControllersCount++; } if (Instance.DebugComponent) { DDebug.Log("Clear Pool - Killed " + killedControllersCount + " Controllers - " + Pool.Count + "' Controllers Available", Instance); } return; } SoundyController.KillAll(); Pool.Clear(); if (Instance.DebugComponent) { DDebug.Log("Clear Pool - Killed All Controllers - " + Pool.Count + " Controllers Available", Instance); } }
/// <summary> /// Play the specified audio clip with the given parameters, at the set position. /// Returns a reference to the SoundyController that is playing the sound. /// Returns null if the AudioClip is null. /// </summary> /// <param name="audioClip"> The AudioClip to play </param> /// <param name="outputAudioMixerGroup"> The output audio mixer group that this sound will get routed through </param> /// <param name="position"> The position from where this sound will play from </param> /// <param name="volume"> The volume of the audio source (0.0 to 1.0) </param> /// <param name="pitch"> The pitch of the audio source </param> /// <param name="loop"> Is the audio clip looping? </param> /// <param name="spatialBlend"> /// Sets how much this AudioSource is affected by 3D space calculations (attenuation, /// doppler etc). 0.0 makes the sound full 2D, 1.0 makes it full 3D /// </param> public static SoundyController Play(AudioClip audioClip, AudioMixerGroup outputAudioMixerGroup, Vector3 position, float volume = 1, float pitch = 1, bool loop = false, float spatialBlend = 0) { if (!s_initialized) { s_instance = Instance; } if (audioClip == null) { return(null); } SoundyController controller = SoundyPooler.GetControllerFromPool(); controller.SetSourceProperties(audioClip, volume, pitch, loop, spatialBlend); controller.SetOutputAudioMixerGroup(outputAudioMixerGroup); controller.SetPosition(position); controller.Play(); if (Instance.DebugComponent) { DDebug.Log("Play '" + audioClip.name + "' AudioClip", Instance); } return(controller); }
/// <summary> Create a new SoundyController in the current scene and get a reference to it </summary> public static SoundyController GetController() { return(SoundyController.GetController()); }