Files
JourneyToNowhere_Unity/Assets/Scripts/SingleUtils/HomeWorld_Initialize.cs
T
2024-03-08 15:49:30 +08:00

214 lines
7.3 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
using UnityEngine.UI;
public class HomeWorld_Initialize : MonoBehaviour
{
public AudioSource audioSource;
public AudioClip OpenMenuClip1;
public float PlayVolume = 0.8f;
public AK.Wwise.Event StartingEvent;
private readonly System.Random random = new();
//[ShowOnly] public float AudioElapsedTimePlaying = 0;
[ShowOnly] public float AudioChangesOn = 0f;
[ShowOnly] bool SoundIsPlaying = false;
[ShowOnly] string previousEventName = "";
/*public List<string> AudioClipNames = new() {
"Gion3.ogg",
"OriginStation.ogg",
"TrainToTheFuture.ogg",
"AsBefore.ogg",
"OriginStation_Cue_127.ogg",
"H3P2-Default_231.ogg"
//"Finality-HOYO-MiX.ogg"
};*/
/*public List<string> AudioEventNames = new()
{
"Play_BGM_Df_1",
"Play_BGM_Df_2",
"Play_BGM_Df_3",
"Play_BGM_Df_4",
"Play_BGM_Df_5"
};*/
public List<AK.Wwise.Event> AudioEvents = new() { };
public AK.Wwise.Event CurrentEvent = null;
public bool useAssetBundle = false;
public AudioBundleLoader BundleLoader;
public AssetBundle AudioBundle;
public RectTransform SettingsPanel;
private Resolution GameResolutionPre;
public Toggle PostProcessingToggle;
public SettingsPanel SettingsPanelScript;
//static uint[] currentlyPlayingIDs = new uint[50];
private async void Start()
{
UnityEngine.SceneManagement.SceneManager.activeSceneChanged += delegate
{
CurrentEvent.Stop(gameObject);
};
/*if (useAssetBundle)
{
/*BundleLoader.InitializeAudioAssetBundle();
new WaitForSeconds(2)
AudioBundle = BundleLoader.GetAudioAssetBundle();
OpenMenuClip1 = BundleLoader.GetAudioClipFromAssetBundle(StartingClipName);
}
else if (!OpenMenuClip1)
OpenMenuClip1 = await JTN.Utils.LoadClip(Path.Combine(Application.streamingAssetsPath, StartingClipName));
if (audioSource && OpenMenuClip1)
audioSource.PlayOneShot(OpenMenuClip1, PlayVolume);*/
/* if (SettingsPanel)
{
if (Screen.currentResolution.height != GameResolutionPre.height)
{
SettingsPanel.sizeDelta = new Vector2(SettingsPanel.sizeDelta.x, Screen.currentResolution.height);
GameResolutionPre = Screen.currentResolution;
}
if (Screen.currentResolution.width != GameResolutionPre.width)
{
SettingsPanel.sizeDelta = new Vector2(Screen.currentResolution.width, SettingsPanel.sizeDelta.y);
GameResolutionPre = Screen.currentResolution;
}
}*/
if (!SoundIsPlaying)
PostNormal(StartingEvent);
if (PlayerPrefs.GetInt("PostProcessingEnabled") == 1)
{
foreach (PostProcessVolume volume in SettingsPanelScript.PostProcessingVolumes)
volume.enabled = true;
PostProcessingToggle.isOn = true;
}
else
{
foreach (PostProcessVolume volume in SettingsPanelScript.PostProcessingVolumes)
volume.enabled = false;
PostProcessingToggle.isOn = false;
}
}
private void FixedUpdate()
{
/*if (BundleLoader.AudioAssetBundle)
{
if (!audioSource.isPlaying)
PlayBGM();
else
{
AudioChangesOn -= Time.fixedDeltaTime;
}
if (AudioChangesOn <= 0f && AudioIsPlaying)
{
Debug.LogError("Clip has finished playing. Choosing a random clip...");
PlayBGM();
}
}*/
if (!SoundIsPlaying)
{
Debug.LogError("An event finished playing.");
PlayBGM();
}
/*else
AudioChangesOn -= Time.fixedDeltaTime;
if (AudioChangesOn <= 0f && SoundIsPlaying)
{
Debug.LogError("Event has finished playing. Choosing a random clip...");
PlayBGM();
}*/
}
/*private void Update()
{
if (SettingsPanel)
{
if (Screen.currentResolution.height != GameResolutionPre.height &&
Screen.currentResolution.width != GameResolutionPre.width)
{
SettingsPanel.sizeDelta = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);
}
GameResolutionPre = Screen.currentResolution;
}
}*/
/*public void PlayBGM()
{
string AudioClipName = AudioClipNames[random.Next(AudioClipNames.Count)];
Debug.Log("Attempting to load audiobundle...");
if (audioSource)
{
AudioClip clip = BundleLoader.GetAudioClipFromAssetBundle(AudioClipName);
if (!clip)
Debug.LogError($"AudioClip {AudioClipName} could not be loaded.");
else
{
audioSource.clip = clip;
audioSource.Play();
audioSource.clip = clip;
AudioChangesOn = clip.length;
Debug.LogError($"Tried playing ambience: {clip.name} with a length of {clip.length}");
AudioIsPlaying = true;
//TimesAmbientSet++;
}
}
}*/
// March 08, 2024 -- Move to Wwise
public void PlayBGM()
{
foreach (var @event in AudioEvents)
{
if (previousEventName.Length > 1 && CurrentEvent != null)
{
if (previousEventName == CurrentEvent.Name)
CurrentEvent = AudioEvents[random.Next(AudioEvents.Count)];
}
CurrentEvent = AudioEvents[random.Next(AudioEvents.Count)];
}
CurrentEvent.Post(gameObject, (uint)AkCallbackType.AK_EndOfEvent, BGMCallback);
Debug.LogError($"Posted event \"{CurrentEvent.Name}\"");
if (!SoundIsPlaying)
{
CurrentEvent.Post(gameObject);
SoundIsPlaying = true;
}
else
Debug.LogError($"The event \"{CurrentEvent.Name}\" is currently playing...");
}
public void PostNormal(AK.Wwise.Event @event)
{
CurrentEvent = @event;
CurrentEvent.Post(gameObject, (uint)AkCallbackType.AK_EndOfEvent, CallbackFunction);
Debug.LogError($"Posted event \"{CurrentEvent.Name}\"");
if (!SoundIsPlaying)
{
CurrentEvent.Post(gameObject);
SoundIsPlaying = true;
}
else
Debug.LogError($"The event \"{CurrentEvent.Name}\" is currently playing...");
}
void CallbackFunction(object in_cookie, AkCallbackType callType, object in_info)
{
if (callType == AkCallbackType.AK_EndOfEvent)
{
SoundIsPlaying = false;
}
}
void BGMCallback(object in_cookie, AkCallbackType callType, object in_info)
{
if (callType == AkCallbackType.AK_EndOfEvent)
{
SoundIsPlaying = false;
PlayBGM();
}
}
}