Untitled
saddam
plain_text
2 years ago
2.6 kB
6
Indexable
using System.Collections; using System.Collections.Generic; using Sirenix.OdinInspector; using UnityEngine; public enum SoundType { OnClick, BG, PickGoodObj, PickBadObj, PickGem, Fall, Complete, Fail, CoinAddition, Kiss, cheering, FinishVoiceOver, GlassBreak, GiftOpen, FOllowPick, KeyPick, cashPick, TaxPick, poof, specialPick, AfterFInish, DiamondCrush, FoodPickGood, FoodPickBad, DiamondDie, CakeDie, Slap } [System.Serializable] public class SoundClass { public SoundType Type; public AudioClip[] clip; [HideInInspector] public bool canSound = true; } public class SoundManager : MonoBehaviour { public static SoundManager Instance; void getInstance() { if (!Instance) { Instance = this; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); } } //public Dictionary<SoundType, AudioClip> particlesData = new Dictionary<SoundType, AudioClip>(); [TableList(ShowIndexLabels = true)] public SoundClass[] soundData; public AudioSource OneShotSound, BgSound; // Start is called before the first frame update void Awake() { getInstance(); } [HideInEditorMode] [Button("Test")] public void PlayOnShot(SoundType type, float vol = 1) { for (int i = 0; i < soundData.Length; i++) { if (soundData[i].Type == type && soundData[i].canSound) { OneShotSound.PlayOneShot(soundData[i].clip[Random.Range(0, soundData[i].clip.Length)], vol * GameData.SoundVolume); soundData[i].canSound = false; StartCoroutine(Delay(() => soundData[i].canSound = true, 0.3f)); return; } } } public void PlayBG(SoundType type, float vol = 1) { for (int i = 0; i < soundData.Length; i++) { if (soundData[i].Type == type) { int index = Random.Range(0, soundData[i].clip.Length); if (BgSound.clip != soundData[i].clip[index]) BgSound.clip = soundData[i].clip[index]; BgSound.volume = vol * GameData.SoundVolume * 0.5f; BgSound.Play(); return; } } } public IEnumerator Delay(System.Action Callback, float t) { yield return new WaitForSeconds(t); Callback(); } }
Editor is loading...