You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PlumberUltimateAds/Assets/Scripts/Sound.cs

110 lines
1.7 KiB
C#

2 months ago
/*
http://www.cgsoso.com/forum-211-1.html
CG Unity3d Unity3d VIP
CGSOSO CG
daily assets update for try.
U should buy the asset from home store if u use it in your project!
*/
using UnityEngine;
public class Sound : MonoBehaviour
{
public enum Button
{
Default,
Back
}
public enum Others
{
Other1,
Other2
}
public AudioSource audioSource;
public AudioSource loopAudioSource;
[HideInInspector]
public AudioClip[] buttonClips;
[HideInInspector]
public AudioClip[] otherClips;
public static Sound instance;
private void Awake()
{
instance = this;
}
private void Start()
{
UpdateSetting();
}
public bool IsMuted()
{
return !IsEnabled();
}
public bool IsEnabled()
{
return CUtils.GetBool("sound_enabled", defaultValue: true);
}
public void SetEnabled(bool enabled)
{
CUtils.SetBool("sound_enabled", enabled);
UpdateSetting();
}
public void Play(AudioClip clip)
{
audioSource.PlayOneShot(clip);
}
public void Play(AudioSource audioSource)
{
if (IsEnabled())
{
audioSource.Play();
}
}
public void PlayButton(Button type = Button.Default)
{
audioSource.PlayOneShot(buttonClips[(int)type]);
}
public void Play(Others type, float volume = 1f)
{
audioSource.volume = volume;
audioSource.PlayOneShot(otherClips[(int)type]);
}
public void PlayLooping(Others type, float volume = 1f)
{
loopAudioSource.volume = volume;
loopAudioSource.PlayOneShot(otherClips[(int)type]);
}
public void StopLooping()
{
loopAudioSource.Stop();
}
public void UpdateSetting()
{
audioSource.mute = IsMuted();
loopAudioSource.mute = IsMuted();
}
}