public static bool Prefix(ref DictionaryExt <GlobalAudioManager.Sounds, GlobalAudioManager.MusicSource> ___s_musicSources, ref GlobalAudioManager.Sounds ___s_currentMusic) { string name = SceneManager.GetActiveScene().name; for (int i = 0; i < ___s_musicSources.Values.Count; i++) { var key = ___s_musicSources.Keys[i]; var value = ___s_musicSources.Values[i]; if (key != ___s_currentMusic && value.SceneName != name) { if (CustomAudio.ReplacedClips.Contains(key)) { //SL.Log("Game tried to clean up " + key + ", but we skipped it!"); continue; } UnityEngine.Object.Destroy(value.Source.gameObject); ___s_musicSources.Remove(key); i--; } } return(false); }
public static bool Prefix(GlobalAudioManager.Sounds _sound) { if (CustomAudio.ReplacedClips.Contains(_sound)) { SL.Log("Game tried to replace " + _sound + ", but it is already replaced with a custom sound! Skipping..."); return(false); } return(true); }
private IEnumerator FadeMusic(GlobalAudioManager _manager, GlobalAudioManager.Sounds _music, GlobalAudioManager.MusicSource musSource, float _time, bool _in = true) { float vol; float targetVol; if (!_in) { vol = musSource.Source.volume; targetVol = 0f; } else { vol = 0f; targetVol = musSource.OrigVolume; } if (!musSource.Source.gameObject.activeSelf) { musSource.Source.gameObject.SetActive(true); } if (_in) { musSource.Source.Play(); } float lerp = 0f; while (lerp < 1f) { if (!musSource.Source) { break; } lerp = Mathf.Clamp01(lerp + Time.deltaTime / _time); musSource.Source.volume = Mathf.Lerp(vol, targetVol, lerp); yield return(null); } if (!_in && musSource.Source) { if (At.GetValue(typeof(GlobalAudioManager), _manager, "m_eventMusic") is GlobalAudioManager.Sounds _eventMusic && _eventMusic != GlobalAudioManager.Sounds.NONE && _music == _eventMusic) { musSource.Source.Stop(); } else { musSource.Source.Pause(); } }
/// <summary>Replace a global sound with the provided AudioClip.</summary> public static void ReplaceAudio(GlobalAudioManager.Sounds sound, AudioClip clip) { if (!GAMInstance) { SL.LogWarning("Cannot find GlobalAudioManager Instance!"); return; } if (ReplacedClips.Contains(sound)) { SL.Log($"The Sound clip '{sound}' has already been replaced, replacing again..."); } try { DoReplaceClip(sound, clip); } catch (Exception e) { SL.LogError($"Exception replacing clip '{sound}'.\r\nMessage: {e.Message}\r\nStack: {e.StackTrace}"); } }
private static void DoReplaceClip(GlobalAudioManager.Sounds _sound, AudioClip _newClip) { if (!_newClip) { SL.LogWarning($"The replacement clip for '{_sound}' is null"); return; } //var path = GAMInstance.GetPrefabPath(_sound); var path = (string)At.Invoke(GAMInstance, "GetPrefabPath", _sound); var resource = Resources.Load <GameObject>("_Sounds/" + path); var component = resource.GetComponent <AudioSource>(); component.clip = _newClip; resource.hideFlags |= HideFlags.DontUnloadUnusedAsset; if (!ReplacedClips.Contains(_sound)) { ReplacedClips.Add(_sound); } SL.Log("Replaced " + _sound + " AudioSource with custom clip!"); }
private AudioSource PlayMusicHook(On.GlobalAudioManager.orig_PlayMusic orig, GlobalAudioManager self, GlobalAudioManager.Sounds _sound, float _fade) { string songName = _sound.ToString(); if (SL.Instance.AudioClips.ContainsKey(songName) && At.GetValue(typeof(GlobalAudioManager), self, "s_musicSources") is DictionaryExt <GlobalAudioManager.Sounds, GlobalAudioManager.MusicSource> dict) { // set our custom clip to the actual GlobalAudioManager dictionary, so it works with the game systems as expected if (!dict.ContainsKey(_sound) && At.Call(self, "GetPrefabPath", new object[] { _sound }) is string prefabPath) { GameObject gameObject = Resources.Load("_Sounds/" + prefabPath) as GameObject; gameObject = Instantiate(gameObject); AudioSource component = gameObject.GetComponent <AudioSource>(); DontDestroyOnLoad(gameObject); dict.Add(_sound, new GlobalAudioManager.MusicSource(component)); } dict[_sound].Source.clip = SL.Instance.AudioClips[_sound.ToString()]; At.SetValue(dict, typeof(GlobalAudioManager), self, "s_musicSources"); At.Call(self, "CleanUpMusic", null); At.SetValue(_sound, typeof(GlobalAudioManager), self, "s_currentMusic"); StartCoroutine(FadeMusic(self, _sound, dict[_sound], _fade)); return(dict[_sound].Source); } else { return(orig(self, _sound, _fade)); } }