/// <summary> /// This method is called only when PlaySoundSettings has a SoundCue reference in it. /// Same as Play(), but much faster. /// </summary> /// <param name="settings">PlaySoundSettings instance with SoundCue reference in it.</param> /// <returns>Same SoundCue from PlaySoundSettings</returns> private SoundCueProxy PlaySoundCue(PlaySoundSettings settings) { SoundCueProxy cue = settings.soundCueProxy as SoundCueProxy; Transform parent = settings.parent; float fadeInTime = settings.fadeInTime; float fadeOutTime = settings.fadeOutTime; bool isLooped = settings.isLooped; var ncue = _cueManager.GetSoundCue(); ncue.AudioObject = _pool.GetFreeObject(cue.Data.audioPrefab).GetComponent <SoundObject>(); if (parent != null) { ncue.AudioObject.transform.SetParent(parent, false); } SoundCueData data = cue.Data; data.fadeInTime = fadeInTime; data.fadeOutTime = fadeOutTime; data.isFadeIn = data.fadeInTime >= 0.1f; data.isFadeOut = data.fadeOutTime >= 0.1f; data.isLooped = isLooped; cue.SoundCue = ncue; cue.Play(data); return(cue); }
public void ToggleMute(string categoryName) { var muted = IsMute(categoryName); SetMute(categoryName, !muted); if (categoryName == "Effects") { return; } if (categoryName == "Music" && !muted) { StopAll(true); } else { PlaySoundSettings settings = new PlaySoundSettings(); settings.Init(); settings.name = "Music"; settings.isLooped = true; // FADE IN / FADE OUT TIME BREAKS IF GAME PAUSED //settings.fadeInTime = 3f; //settings.fadeOutTime = 3f; Play(settings); } }
void TestSequence() { PlaySoundSettings settings = new PlaySoundSettings(); settings.Init(); settings.names = new[] { "Test", "Test1", "Test2" }; var proxyCue = Play(settings); Debug.Log(proxyCue.ID); }
void Test() { PlaySoundSettings settings = new PlaySoundSettings(); settings.Init(); settings.name = "Test"; var proxyCue = Play(settings); Debug.Log(proxyCue.ID); }
void TestSequence2TimesPlay() { PlaySoundSettings settings = new PlaySoundSettings(); settings.Init(); settings.names = new[] { "Test", "Test1", "Test2" }; var proxyCue = Play(settings); proxyCue.OnPlayCueEnded += cue => { var sett = new PlaySoundSettings(); sett.soundCueProxy = proxyCue; proxyCue = Play(sett); }; Debug.Log(proxyCue.ID); }
/// <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); }