diff --git a/Assets/_Content/Scenes/Theatre.unity b/Assets/_Content/Scenes/Theatre.unity index 73234eb5..d546479e 100644 --- a/Assets/_Content/Scenes/Theatre.unity +++ b/Assets/_Content/Scenes/Theatre.unity @@ -1226352,6 +1226352,7 @@ Transform: m_Children: - {fileID: 1387960518} - {fileID: 57213872} + - {fileID: 667581932} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &622883669 @@ -1228690,6 +1228691,55 @@ Mesh: - serializedVersion: 1 m_IndexStart: 0 m_IndexCount: 0 +--- !u!1 &667581931 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 667581932} + - component: {fileID: 667581933} + m_Layer: 0 + m_Name: SceneFades + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &667581932 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 667581931} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 622343290} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &667581933 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 667581931} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 96d97cc71ddb7944a8a00c214933401b, type: 3} + m_Name: + m_EditorClassIdentifier: Assembly-CSharp::PersistentWwiseFadeInPlayer + _fadeInEvent: + idInternal: 0 + valueGuidInternal: + WwiseObjectReference: {fileID: 11400000, guid: cb7005361d4283844bb30f03e0a63a83, type: 2} + _bindRetryFrames: 120 --- !u!1001 &669525208 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/Assets/_Content/Scripts/Dev/Audio.meta b/Assets/_Content/Scripts/Dev/Audio.meta new file mode 100644 index 00000000..b3168772 --- /dev/null +++ b/Assets/_Content/Scripts/Dev/Audio.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 422dd821fe4660649b9069203be96904 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/_Content/Scripts/Dev/Audio/PersistentWwiseFadeInPlayer.cs b/Assets/_Content/Scripts/Dev/Audio/PersistentWwiseFadeInPlayer.cs new file mode 100644 index 00000000..3803ab63 --- /dev/null +++ b/Assets/_Content/Scripts/Dev/Audio/PersistentWwiseFadeInPlayer.cs @@ -0,0 +1,162 @@ +using System.Collections; +using UnityEngine; +using UnityEngine.SceneManagement; +using OctoCore.Navigation; + +[DisallowMultipleComponent] +public sealed class PersistentWwiseFadeInPlayer : MonoBehaviour +{ + private static PersistentWwiseFadeInPlayer _instance; + + [SerializeField] private AK.Wwise.Event _fadeInEvent; + + private const int BindRetryFrames = 120; + + private OctoSceneFade _boundFade; + private Coroutine _bindRoutine; + + [RuntimeInitializeOnLoadMethod( + RuntimeInitializeLoadType.SubsystemRegistration)] + private static void ResetInstance() + { + _instance = null; + } + + private void Awake() + { + if (_instance != null && _instance != this) + { + Destroy(gameObject); + return; + } + + _instance = this; + + // Required because DontDestroyOnLoad should preserve a root object. + if (transform.parent != null) + transform.SetParent(null, true); + + DontDestroyOnLoad(gameObject); + } + + private void OnEnable() + { + SceneManager.sceneLoaded += OnSceneLoaded; + } + + private void Start() + { + StartBinding(SceneManager.GetActiveScene()); + } + + private void OnDisable() + { + SceneManager.sceneLoaded -= OnSceneLoaded; + + if (_bindRoutine != null) + { + StopCoroutine(_bindRoutine); + _bindRoutine = null; + } + + Unbind(); + } + + private void OnDestroy() + { + if (_instance == this) + _instance = null; + } + + private void OnSceneLoaded(Scene scene, LoadSceneMode mode) + { + StartBinding(scene); + } + + private void StartBinding(Scene scene) + { + if (_bindRoutine != null) + StopCoroutine(_bindRoutine); + + _bindRoutine = StartCoroutine(BindWhenAvailable(scene)); + } + + private IEnumerator BindWhenAvailable(Scene scene) + { + for (int frame = 0; frame < BindRetryFrames; frame++) + { + OctoSceneFade fade = FindFade(scene); + + if (fade != null) + { + Bind(fade); + _bindRoutine = null; + yield break; + } + + yield return null; + } + + Debug.LogWarning( + $"[{nameof(PersistentWwiseFadeInPlayer)}] " + + $"Could not find OctoSceneFade for scene '{scene.name}'.", + this + ); + + _bindRoutine = null; + } + + private static OctoSceneFade FindFade(Scene scene) + { + if (scene.IsValid() && scene.isLoaded) + { + foreach (GameObject root in scene.GetRootGameObjects()) + { + OctoSceneFade fade = + root.GetComponentInChildren(true); + + if (fade != null) + return fade; + } + } + + // Essential when OctoSceneFade is itself persistent + // or not contained in the newly loaded scene. + return OctoSceneFade.Instance; + } + + private void Bind(OctoSceneFade fade) + { + if (_boundFade == fade) + return; + + Unbind(); + + _boundFade = fade; + _boundFade.OnFadeIn += OnFadeIn; + } + + private void Unbind() + { + if (_boundFade != null) + _boundFade.OnFadeIn -= OnFadeIn; + + _boundFade = null; + } + + private void OnFadeIn() + { + if (_fadeInEvent == null) + { + Debug.LogWarning( + $"[{nameof(PersistentWwiseFadeInPlayer)}] " + + "No Wwise Event assigned.", + this + ); + + return; + } + + _fadeInEvent.Post(gameObject); + } +} \ No newline at end of file diff --git a/Assets/_Content/Scripts/Dev/Audio/PersistentWwiseFadeInPlayer.cs.meta b/Assets/_Content/Scripts/Dev/Audio/PersistentWwiseFadeInPlayer.cs.meta new file mode 100644 index 00000000..650f8d58 --- /dev/null +++ b/Assets/_Content/Scripts/Dev/Audio/PersistentWwiseFadeInPlayer.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 96d97cc71ddb7944a8a00c214933401b \ No newline at end of file