Files
2024-01-31 22:53:00 +08:00

76 lines
2.3 KiB
C#

using UnityEngine;
using System.Collections.Generic;
public class BigWorld : MonoBehaviour
{
public bool useAudioBundle = false;
public AudioBundleLoader BundleLoader;
public List<string> AudioClipNames = new() {
"Gion3.ogg",
"OriginStation.ogg",
"TrainToTheFuture.ogg",
"Finality.ogg"
};
public AssetBundle AudioBundle;
public AudioSource ambientAudioSource;
public AudioClip CurrentAmbient;
public bool AutoPlayAmbient = true;
private readonly System.Random random = new();
[ShowOnly] public float AudioChangesOn = 0f;
[ShowOnly] public bool AudioIsPlaying = false;
void Start()
{
if (BundleLoader)
{
AudioBundle = BundleLoader.AudioAssetBundle;
}
if (AutoPlayAmbient)
{
Debug.Log("Automatically playing ambient...");
PlayAmbient();
}
}
public void PlayAmbient()
{
if (useAudioBundle)
{
string AudioClipName = AudioClipNames[random.Next(AudioClipNames.Count)];
Debug.Log("Attempting to load audiobundle...");
if (ambientAudioSource)
{
AudioClip clip = BundleLoader.GetAudioClipFromAssetBundle(/*"Default/" + */AudioClipName);
if (!clip)
Debug.LogError($"AudioClip {AudioClipName} could not be loaded.");
else
{
ambientAudioSource.clip = clip;
ambientAudioSource.Play();
ambientAudioSource.clip = clip;
AudioChangesOn = clip.length;
Debug.LogError($"Tried playing ambience: {clip.name} with a length of {clip.length}");
AudioIsPlaying = true;
//TimesAmbientSet++;
}
}
}
}
private void FixedUpdate()
{
if (AutoPlayAmbient)
{
if (ambientAudioSource.isPlaying)
{
AudioChangesOn -= Time.fixedDeltaTime;
}
if (AudioChangesOn <= 0f && AudioIsPlaying)
{
Debug.LogError("Clip has finished playing. Choosing a random clip...");
PlayAmbient();
}
}
}
}