Exemple #1
0
    private SoundManager()
    {
        channels         = new Dictionary <SoundChannels, SoundChannel>();
        readOnlyChannels = new ReadOnlyDictionary <SoundChannels, SoundChannel>(channels);

        for (SoundChannels i = 0; i < SoundChannels.Count; i++)
        {
            channels.Add(i, new SoundChannel(this));
        }

        audioSourcesPool  = new List <SJAudioSource>();
        audioSourcePrefab = SJResources.Instance.LoadGameObjectAndGetComponent <SJAudioSource>("SJAudioSourcePrefab");

        for (int i = 0; i < initialPoolSize; i++)
        {
            audioSourcesPool.Add(GameObject.Instantiate <SJAudioSource>(audioSourcePrefab));
        }
    }
Exemple #2
0
    public static SoundManager Setup(SoundSettings settings, SoundChannels channels)
    {
        instance = new GameObject("SoundManager").AddComponent <SoundManager>();

        instance.settings = settings;
        instance.channels = channels;

        instance.idToSound     = new Dictionary <int, SoundInstance>();
        instance.playingSounds = new List <SoundInstance>();

        instance.idCounter = 0;

        instance.soundPool = new DynamicPool <SoundInstance>(() =>
        {
            var sound = new GameObject("SFX").AddComponent <SoundInstance>();

            sound.source = sound.gameObject.AddComponent <AudioSource>();

            sound.transform.parent = instance.transform;
            sound.gameObject.SetActive(false);

            return(sound);
        });

        instance.soundPool.onFree += (SoundInstance sound) =>
        {
            sound.source.Stop();
            sound.source.clip = null;
            sound.id          = 0;

            sound.transform.parent = instance.transform;
            sound.attachedToUnit   = null;

            sound.gameObject.SetActive(false);
        };

        return(instance);
    }