Add files via upload

This commit is contained in:
Geri 2021-01-08 16:05:34 +01:00 committed by GitHub
parent 82aab308f0
commit 310f5c8838
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
54 changed files with 2357 additions and 0 deletions

View file

@ -0,0 +1,206 @@
using System.Collections;
using UnityEngine;
using System;
public class AudioManager : MonoBehaviour{
public static AudioManager instance;
[SerializeField] Sounds soundList = default;
void Awake(){
if(instance == null){
instance = this;
}else{
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
foreach(Sounds.List s in soundList.sounds){
if(string.IsNullOrEmpty(s.name) || string.IsNullOrWhiteSpace(s.name)){
Debug.LogWarning("The name one sound is empty");
continue;
}
GameObject sound = new GameObject(s.name);
sound.transform.parent = transform;
s.source = sound.AddComponent<AudioSource>();
if(soundList.mainMixer && soundList.sfxMixer){
if (s.type == Sounds.List.Type.Music)
s.source.outputAudioMixerGroup = soundList.mainMixer;
else
s.source.outputAudioMixerGroup = soundList.sfxMixer;
}
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.loop = s.loop;
}
}
#region Play
public void Play(string name){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if(s == null){
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.Play();
}
public void Play(string name, float delay){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.PlayDelayed(delay);
}
public void PlayOneShot(string name){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.PlayOneShot(s.clip);
}
#endregion
#region Pause
public void Pause(string name){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.Pause();
}
public void UnPause(string name){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.UnPause();
}
#endregion
#region Stop
public void Stop(string name){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.Stop();
}
public void StopAll(){
foreach (Sounds.List s in soundList.sounds){
if (s.source){
s.source.Stop();
}
}
}
#endregion
public AudioSource GetSource(string name){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
return null;
}
return s.source;
}
#region Fades
public void FadeIn(string name, float duration){
StartCoroutine(FadeInCoroutine(name, duration));
}
IEnumerator FadeInCoroutine(string name, float fadeTime){
AudioSource audioSource = GetSource(name);
if (audioSource != null && !audioSource.isPlaying){
float volume = audioSource.volume;
audioSource.volume = 0;
audioSource.Play();
while (audioSource.volume < volume){
audioSource.volume += Time.deltaTime / fadeTime;
yield return null;
}
audioSource.volume = volume;
}
}
public void FadeOut(string name, float duration){
StartCoroutine(FadeOutCoroutine(name, duration));
}
IEnumerator FadeOutCoroutine(string name, float fadeTime){
AudioSource audioSource = GetSource(name);
if (audioSource && audioSource.isPlaying){
float startVolume = audioSource.volume;
while (audioSource.volume > 0){
audioSource.volume -= startVolume * Time.deltaTime / fadeTime;
yield return null;
}
audioSource.Stop();
audioSource.volume = startVolume;
}
}
public void PlayMuted(string name){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = 0f;
s.source.Play();
}
public void FadeMutedIn(string name, float duration){
StartCoroutine(FadeMutedInCoroutine(name, duration));
}
IEnumerator FadeMutedInCoroutine(string name, float fadeTime){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
yield break;
}
while (s.source.volume < s.volume){
s.source.volume += Time.deltaTime / fadeTime;
yield return null;
}
s.source.volume = s.volume;
}
public void FadeMutedOut(string name, float duration){
StartCoroutine(FadeMutedOutCoroutine(name, duration));
}
IEnumerator FadeMutedOutCoroutine(string name, float fadeTime){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
yield break;
}
while (s.source.volume > 0){
s.source.volume -= Time.deltaTime / fadeTime;
yield return null;
}
s.source.volume = 0;
}
#endregion
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7d2879f3876727040b4f0cc799ec7ada
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

View file

@ -0,0 +1,96 @@
fileFormatVersion: 2
guid: af0857324620d1d4b8b6bf41b6cdecfc
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 11
mipmaps:
mipMapMode: 0
enableMipMap: 1
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
vTOnly: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: -1
aniso: -1
mipBias: -100
wrapU: -1
wrapV: -1
wrapW: -1
nPOTScale: 1
lightmap: 0
compressionQuality: 50
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 0
textureShape: 1
singleChannelComponent: 0
flipbookRows: 1
flipbookColumns: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
ignorePngGamma: 0
applyGammaDecoding: 0
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID:
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,50 @@
using UnityEngine;
using UnityEngine.Audio;
[CreateAssetMenu(fileName = "Sounds", menuName = "Tools/Sounds", order = 0)]
public class Sounds : ScriptableObject{
[Tooltip("The music mixer.")]
public AudioMixerGroup mainMixer = default;
[Tooltip("The SFX mixer.")]
public AudioMixerGroup sfxMixer = default;
public List[] sounds;
[System.Serializable] public class List{
[Tooltip("Name of the sound. Each name has to be different between each other.")]
public string name;
public AudioClip clip;
[System.Serializable] public enum Type { Music, SFX }
[Space]
[Tooltip("Is it part of the music or the SFX?")] public Type type;
[Space]
[Tooltip("Default volume of the sound."), Range(0f, 1f)] public float volume;
[Tooltip("Variance percentage of the volume"), Range(0f, 1f)] public float volumeVariance;
[Tooltip("Default pitch of the sound."), Range(.1f, 3f)] public float pitch;
[Tooltip("Variance percentage of the pitch"), Range(0f, 1f)] public float pitchVariance;
public bool loop;
[HideInInspector] public AudioSource source;
float randomVolume;
public float RandomVolume{
get{
randomVolume = volume * (1f + Random.Range(-volumeVariance / 2f, volumeVariance / 2f));
return randomVolume;
}
}
float randomPitch;
public float RandomPitch{
get{
randomPitch = pitch * (1f + Random.Range(-pitchVariance / 2f, pitchVariance / 2f));
return randomPitch;
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6ded47588579f3047b1c3cd72cd87044
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: af0857324620d1d4b8b6bf41b6cdecfc, type: 3}
userData:
assetBundleName:
assetBundleVariant: