/// <summary> /// Get a free SoundCue. /// </summary> /// <returns>Returns a SoundCue instance.</returns> public SoundCue GetSoundCue() { SoundCue cue = FindFreeCue(); cue.OnPlayKilled += OnPlayKilled_handler; return(cue); }
private void OnPlayKilled_handler(SoundCue cue, SoundCueProxy proxy) { //NOTE: Clear up any references to events. cue.OnPlayKilled = null; cue.OnPlayCueEnded = null; cue.OnPlayEnded = null; proxy.SoundCue = null; }
/// <summary> /// Finds the first non-playing SoundCue. /// </summary> /// <returns>cached SoundCue item.</returns> private SoundCue FindFreeCue() { SoundCue cue = null; for (int i = 0; i < _soundCues.Count; i++) { if (_soundCues[i].IsPlaying == false) { cue = _soundCues[i]; break; } } if (cue == null) { cue = new SoundCue(_soundCues.Count); _soundCues.Add(cue); } return(cue); }
/// <summary> /// Creates a SoundCue and plays it. /// </summary> /// <param name="settings">A struct which contains all data for SoundController to work</param> /// <returns>A soundCue interface which can be subscribed to it's events.</returns> public ISoundCue Play(PlaySoundSettings settings) { if (settings.soundCueProxy != null) { return(PlaySoundCue(settings)); } if (settings.names == null && string.IsNullOrEmpty(settings.name)) { return(null); } string[] names = null; string categoryName = settings.categoryName; float fadeInTime = settings.fadeInTime; float fadeOutTime = settings.fadeOutTime; bool isLooped = settings.isLooped; int tagID = _database.soundTags.GetTagIDByName(settings.tagName); Transform parent = settings.parent; if (settings.names != null) { names = settings.names; } else { names = new[] { settings.name }; } UnityEngine.Assertions.Assert.IsNotNull(names, "[AudioController] names cannot be null"); if (names != null) { UnityEngine.Assertions.Assert.IsFalse(names.Length == 0, "[AudioController] names cannot have 0 strings"); } CategoryItem category = null; GameObject prefab = null; List <SoundItem> items = new List <SoundItem>(); List <float> catVolumes = new List <float>(); List <CategoryItem> categories = new List <CategoryItem>(); if (string.IsNullOrEmpty(categoryName) == false) { category = System.Array.Find(_database.items, (item) => { return(item.name == categoryName); }); // Debug.Log(category); if (category == null) { return(null); } prefab = category.usingDefaultPrefab ? _defaultPrefab : category.audioObjectPrefab; for (int i = 0; i < names.Length; i++) { SoundItem item = System.Array.Find(category.soundItems, (x) => { return(x.name == names[i]); }); if (item != null && category.isMute == false) { bool canAddItem = tagID == -1 || tagID == item.tagID; if (canAddItem) { catVolumes.Add(category.categoryVolume); items.Add(item); categories.Add(category); } } } } else { prefab = _defaultPrefab; CategoryItem[] categoryItems = _database.items; for (int i = 0; i < names.Length; i++) { SoundItem item = null; item = items.Find((x) => names[i] == x.name); if (item != null) { bool canAddItem = tagID == -1 || tagID == item.tagID; if (canAddItem == false) { continue; } catVolumes.Add(catVolumes[items.IndexOf(item)]); categories.Add(categories[items.IndexOf(item)]); items.Add(item); continue; } for (int j = 0; j < categoryItems.Length; j++) { item = System.Array.Find(categoryItems[j].soundItems, (x) => x.name == names[i]); if (item != null && categoryItems[j].isMute == false) { bool canAddItem = tagID == -1 || tagID == item.tagID; if (canAddItem == false) { continue; } catVolumes.Add(categoryItems[j].categoryVolume); categories.Add(categoryItems[j]); items.Add(item); break; } } } } if (items.Count == 0) { return(null); } SoundCue cue = _cueManager.GetSoundCue(); SoundCueData data; data.audioPrefab = prefab; data.sounds = items.ToArray(); data.categoryVolumes = catVolumes.ToArray(); data.categoriesForSounds = categories.ToArray(); data.fadeInTime = fadeInTime; data.fadeOutTime = fadeOutTime; data.isFadeIn = data.fadeInTime >= 0.1f; data.isFadeOut = data.fadeOutTime >= 0.1f; data.isLooped = isLooped; cue.AudioObject = _pool.GetFreeObject(prefab).GetComponent <SoundObject>(); if (parent != null) { cue.AudioObject.transform.SetParent(parent, false); } SoundCueProxy proxy = new SoundCueProxy(); proxy.SoundCue = cue; proxy.Play(data); return(proxy); }