Unity 简单的音效管理器

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

public enum _eSoundLayer
{
    Background,
    Effect,
    EffectUI
}

public class SoundManager
{
    SoundServer mSoundServer = new SoundServer();
    Dictionary<_eSoundLayer, ISoundLayer> mSoundLayers = new Dictionary<_eSoundLayer, ISoundLayer>();


    public void Create()
    {
        mSoundServer.Create("Sound");
        mSoundLayers.Add(_eSoundLayer.Background, new SoundLayerBackground(mSoundServer));
        mSoundLayers.Add(_eSoundLayer.Effect, new SoundLayerEffect(mSoundServer, 5));
        mSoundLayers.Add(_eSoundLayer.EffectUI, new SoundLayerUI(mSoundServer, 3));
    }

    public void SetVolume(_eSoundLayer layer, float volume)
    {
        mSoundLayers[layer].SetVolume(volume);
    }

    public void Play(_eSoundLayer layer, string sound_name)
    {
        if (string.IsNullOrEmpty(sound_name))
        {
            return;
        }
        mSoundLayers[layer].Play(sound_name);
    }

    public void Stop(_eSoundLayer layer)
    {
        mSoundLayers[layer].Stop();
    }

    public void Destroy()
    {
        foreach (var sound_layer in mSoundLayers)
        {
            sound_layer.Value.Destroy();
        }
        mSoundLayers.Clear();
    }
}

public class SoundServer
{
    GameObject mPlayer;
    string mSoundFileRootPath;

    public void Create(string sound_file_root_path)
    {
        Debug.Log(" SoundPlayer +++++++++++++++++++++++++++");
        mSoundFileRootPath = sound_file_root_path;
        mPlayer = new GameObject("SoundPlayer");
    }

    AudioClip LoadSound(string sound_name)
    {
        return Resources.Load<AudioClip>(mSoundFileRootPath + "/" + sound_name);
    }

    public AudioSource CreateSoundPlayer()
    {
        return mPlayer.AddComponent<AudioSource>();
    }

    public void Play(AudioSource sound_player, string sound_name, bool is_loop)
    {
        if (sound_player == null) return;
        //sound_player.Stop();
        sound_player.loop = is_loop;
        sound_player.clip = LoadSound(sound_name);
        sound_player.Play(); 
    }

    public void Pause(AudioSource sound_player)
    {
        if (sound_player == null) return;
        sound_player.Pause();
    }

    public void Stop(AudioSource sound_player)
    {
        if (sound_player == null) return;
        sound_player.Stop();
    }

    public void DestroySoundPlayer(AudioSource sound_player)
    {
        if (sound_player == null) return;
        sound_player.Stop();
        UnityEngine.Object.Destroy(sound_player);
    }
}

public interface ISoundLayer
{
    void Play(string sound_name);
    void Stop();
    void SetVolume(float volume);

    void Destroy();
}

public class SoundLayerBackground : ISoundLayer
{
    SoundServer mSoundServer;
    AudioSource mSoundPlayer;

    public SoundLayerBackground(SoundServer sound_server)
    {
        mSoundServer = sound_server;
        mSoundPlayer = mSoundServer.CreateSoundPlayer();
    }

    public void Play(string sound_name)
    {
        if (IsSameSound(sound_name)) return;
        mSoundServer.Play(mSoundPlayer, sound_name, true);
    }

    public void Stop()
    {
        mSoundServer.Stop(mSoundPlayer);
    }

    public void SetVolume(float volume)
    {
        mSoundPlayer.volume = volume;
    }

    public void Destroy()
    {
        mSoundServer.DestroySoundPlayer(mSoundPlayer);
    }

    bool IsSameSound(string sound_name)
    {
        return mSoundPlayer.clip != null && mSoundPlayer.clip.name == sound_name;
    }
}

public class SoundLayerEffect : ISoundLayer
{
    SoundServer mSoundServer;
    Queue<AudioSource> mSoundPlayers = new Queue<AudioSource>();

    public SoundLayerEffect(SoundServer sound_server, int track_number)
    {
        mSoundServer = sound_server;
        mSoundPlayers = new Queue<AudioSource>(track_number);
        while (track_number > 0)
        {
            mSoundPlayers.Enqueue(mSoundServer.CreateSoundPlayer());
            track_number--;
        }
    }

    public void Play(string sound_name)
    {
        AudioSource current_player = mSoundPlayers.Dequeue();
        mSoundServer.Play(current_player, sound_name, false);
        mSoundPlayers.Enqueue(current_player);
    }

    public void Stop()
    {
        foreach (var sound_player in mSoundPlayers)
        {
            mSoundServer.Stop(sound_player);
        } 
    }

    public void SetVolume(float volume)
    {
        foreach (var sound_player in mSoundPlayers)
        {
            sound_player.volume = volume;
        }
    }

    public void Destroy()
    {
        foreach (var sound_player in mSoundPlayers)
        {
            mSoundServer.DestroySoundPlayer(sound_player);
        }
    }
}
public class SoundLayerUI : ISoundLayer
{
    SoundServer mSoundServer;
    Queue<AudioSource> mSoundPlayers = new Queue<AudioSource>();

    public SoundLayerUI(SoundServer sound_server, int track_number)
    {
        mSoundServer = sound_server;
        mSoundPlayers = new Queue<AudioSource>(track_number);
        while (track_number > 0)
        {
            mSoundPlayers.Enqueue(mSoundServer.CreateSoundPlayer());
            track_number--;
        }
    }

    public void Play(string sound_name)
    {
        AudioSource current_player = mSoundPlayers.Dequeue();
        mSoundServer.Play(current_player, sound_name, false);
        mSoundPlayers.Enqueue(current_player);
    }

    public void Stop()
    {
        foreach (var sound_player in mSoundPlayers)
        {
            mSoundServer.Stop(sound_player);
        }
    }

    public void SetVolume(float volume)
    {
        foreach (var sound_player in mSoundPlayers)
        {
            sound_player.volume = volume;
        }
    }

    public void Destroy()
    {
        foreach (var sound_player in mSoundPlayers)
        {
            mSoundServer.DestroySoundPlayer(sound_player);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值