You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
2.9 KiB
118 lines
2.9 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
namespace UltraCombos
|
|
{
|
|
public class SoundLoader : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
string folder;
|
|
|
|
[SerializeField]
|
|
List<string> filenames = new List<string>();
|
|
|
|
[SerializeField]
|
|
bool loop = false;
|
|
|
|
List<AudioClip> clips = new List<AudioClip>();
|
|
|
|
AudioSource source = null;
|
|
|
|
private void Start()
|
|
{
|
|
if (filenames.Count == 0)
|
|
{
|
|
string filename = gameObject.name;
|
|
LoadFile(filename);
|
|
}
|
|
else
|
|
{
|
|
foreach (var filename in filenames)
|
|
{
|
|
LoadFile(filename);
|
|
}
|
|
}
|
|
|
|
source = gameObject.AddComponent<AudioSource>();
|
|
source.loop = loop;
|
|
}
|
|
|
|
void LoadFile(string filename)
|
|
{
|
|
string path = $"{Application.dataPath}/../../{folder}/{filename}";
|
|
if (System.IO.File.Exists(path))
|
|
{
|
|
var ext = System.IO.Path.GetExtension(path);
|
|
AudioType type = AudioType.UNKNOWN;
|
|
if (ext.Equals(".AIF", System.StringComparison.CurrentCultureIgnoreCase))
|
|
{
|
|
type = AudioType.AIFF;
|
|
}
|
|
else if (ext.Equals(".WAV", System.StringComparison.CurrentCultureIgnoreCase))
|
|
{
|
|
type = AudioType.WAV;
|
|
}
|
|
|
|
if (type != AudioType.UNKNOWN)
|
|
{
|
|
StartCoroutine(LoadAudioFile($"file://{path}", type));
|
|
}
|
|
else
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
|
|
}
|
|
|
|
public void PlayOneShot()
|
|
{
|
|
if (clips.Count > 0)
|
|
source.PlayOneShot(clips[Random.Range(0, clips.Count)]);
|
|
}
|
|
|
|
private void FadeIn()
|
|
{
|
|
|
|
}
|
|
|
|
private void FadeOut()
|
|
{
|
|
|
|
}
|
|
|
|
IEnumerator LoadAudioFile(string uri, AudioType type)
|
|
{
|
|
using (var www = UnityWebRequestMultimedia.GetAudioClip(uri, type))
|
|
{
|
|
yield return www.SendWebRequest();
|
|
|
|
if (www.isNetworkError || www.isHttpError)
|
|
{
|
|
Debug.Log(www.error);
|
|
}
|
|
else
|
|
{
|
|
using (var hanlder = www.downloadHandler as DownloadHandlerAudioClip)
|
|
{
|
|
if (hanlder != null)
|
|
{
|
|
clips.Add(hanlder.audioClip);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
yield return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|