Sounds
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class BackgroundManager : MonoBehaviour
|
||||
{
|
||||
private static AudioSource audioSource;
|
||||
public AudioClip BG;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
audioSource = GetComponent<AudioSource>();
|
||||
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (BG != null)
|
||||
{
|
||||
PlayBG(false,BG);
|
||||
}
|
||||
}
|
||||
|
||||
public static void PlayBG(bool resetSong, AudioClip clip = null)
|
||||
{
|
||||
if (clip != null)
|
||||
{
|
||||
audioSource.clip = clip;
|
||||
}
|
||||
if (audioSource.clip != null)
|
||||
{
|
||||
if (resetSong)
|
||||
{
|
||||
audioSource.Stop();
|
||||
}
|
||||
audioSource.Play();
|
||||
}
|
||||
}
|
||||
|
||||
public static void StopBG()
|
||||
{
|
||||
audioSource.Pause();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: af6819c62fb95894485dace7bf3f0c4b
|
||||
@@ -24,7 +24,7 @@ public class SoundEffectLibrary : MonoBehaviour
|
||||
|
||||
public AudioClip GetRandomClip(string name)
|
||||
{
|
||||
if (!soundDictionary.ContainsKey(name))
|
||||
if (soundDictionary.ContainsKey(name))
|
||||
{
|
||||
List<AudioClip> audioClips = soundDictionary[name];
|
||||
if(audioClips.Count > 0)
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class SoundEffectManager : MonoBehaviour
|
||||
{
|
||||
private static AudioSource audioSource;
|
||||
private static SoundEffectLibrary soundEffectLibrary;
|
||||
[SerializeField] private Slider sfxSlider;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
audioSource = GetComponent<AudioSource>();
|
||||
soundEffectLibrary = GetComponent<SoundEffectLibrary>();
|
||||
}
|
||||
|
||||
public static void Play(string soundName)
|
||||
{
|
||||
AudioClip audioClip = soundEffectLibrary.GetRandomClip(soundName);
|
||||
if (audioClip != null)
|
||||
{
|
||||
audioSource.PlayOneShot(audioClip);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
sfxSlider.onValueChanged.AddListener(delegate {OnValueChanged();});
|
||||
}
|
||||
|
||||
public static void SetVolume(float volume)
|
||||
{
|
||||
audioSource.volume = volume;
|
||||
}
|
||||
|
||||
public void OnValueChanged()
|
||||
{
|
||||
SetVolume(sfxSlider.value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 184172b3257bd764283336d08477bacd
|
||||
Reference in New Issue
Block a user