Big Game Project – Static Sound Player

I have continued making and implementing sound effects. Implementing them is tedious because every sound effect needs coding, and version control is annoying since changes to Unity prefabs can’t be merged and I have to change something in at least one C# file and one Unity prefab for every single sound I implement.

Anyway, all the structures for the audio are starting to come together and implementing a new sound doesn’t always create problems anymore. Today I’ve worked on another helpful class for managing audio. It contains sounds that don’t really belong anywhere and they can be accessed through static functions. Each sound has an enum value for easy access without having to compare strings every time. I have similar systems for entities (playable characters and enemies) in the game except the sounds can’t be accessed from static methods, since it makes sense that they belong to a specific instance. Because several entities can play the same sound simultaneously and the sounds should always play at the entity’s position. This script can be seen in this post.

 

So far the whole thing only contains two sounds, but I expect several more to be added later. Below is the full script:


using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Audio;

public enum MiscSounds
{
    UI_CLICK,
    CONSTRUCTION_FINISHED,
}

[System.Serializable]
public class MiscAudioSource
{
    public MiscSounds soundIdentifier;
    public List<AudioClip> audioClips;
    public AudioMixerGroup audioMixerGroup;
    [Range(0, 1)]
    public float spatialBlend = 1f;
    private AudioSource audioSource;
    private int currentIndex = 0;

    public void Initialize(AudioSource _audioSource)
    {
        audioSource = _audioSource;
        audioSource.playOnAwake = false;
        audioSource.clip = audioClips[0];
        audioSource.spatialBlend = spatialBlend;
        audioSource.outputAudioMixerGroup = audioMixerGroup;
        audioSource.volume = 1f;
    }

    public AudioSource RandomSound()
    {
        int random = Random.Range(0, (audioClips.Count - 1));
        currentIndex = random;
        audioSource.clip = audioClips[currentIndex];
        return audioSource;
    }

    public AudioSource NextSound()
    {
        audioSource.clip = audioClips[currentIndex];
        currentIndex++;
        if (currentIndex >= audioClips.Count)
            currentIndex = 0;
        return audioSource;
    }

    public AudioSource CurrentSound()
    {
        return audioSource;
    }
}

public class StaticAudioPlayer : MonoBehaviour
{
    static StaticAudioPlayer instance;
    public List<MiscAudioSource> audioSources;

    void Awake()
    {
        if (instance == null)
            instance = this;

        for (int i = 0; i < audioSources.Count; i++)
        {
            AudioSource audioSource = gameObject.AddComponent<AudioSource>();
            audioSources[i].Initialize(audioSource);
            audioSource.transform.SetParent(transform);
        }
    }

    public AudioSource GetAudioSource(MiscSounds identifier)
    {
        for (int i = 0; i < audioSources.Count; i++)
        {
            MiscAudioSource source = audioSources[i];
            if (source.soundIdentifier == identifier)
                return source.RandomSound();
        }

        return null;
    }

    public static void PlaySound(MiscSounds identifier, Vector3 pos, float minPitch = 1f, float maxPitch = 1f)
    {
        AudioSource audioSource = instance.GetAudioSource(identifier);
        AudioManager.PlaySourceAtPoint(audioSource, pos, minPitch, maxPitch);
    }

    public static void PlaySound(MiscSounds identifier, float minPitch = 1f, float maxPitch = 1f)
    {
        AudioSource audioSource = instance.GetAudioSource(identifier);
        AudioManager.PlayAudioSource(audioSource, minPitch, maxPitch);
    }

    public static void PlayClickSound()
    {
        AudioSource audioSource = instance.GetAudioSource(MiscSounds.UI_CLICK);
        AudioManager.PlayAudioSource(audioSource);
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s