码迷,mamicode.com
首页 > 其他好文 > 详细

游戏内音效的管理类,可通用

时间:2017-12-16 14:41:46      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:ips   没有   volume   场景   vol   empty   rest   returns   ipa   

/// <summary>
/// 音频管理器
/// </summary>
public class AudioManager : MonoSingleTonDontDestroyOnLoad<AudioManager>
{
    /// <summary>
    /// 用于播放背景音乐的音频
    /// </summary>
    private AudioSource m_bgAudio;

    /// <summary>
    /// 用于播放音效的音频
    /// </summary>
    private AudioSource m_EffectAudio;

    /// <summary>
    /// 控制背景音量的大小
    /// </summary>
    public float BgVolume
    {
        get
        {
            return m_bgAudio.volume;
        }
        set
        {
            m_bgAudio.volume = value;
        }
    }
    /// <summary>
    /// 背景音乐的开关
    /// </summary>
    public bool BgMute
    {
        get
        {
            return m_bgAudio.mute;
        }
        set
        {
            m_bgAudio.mute = value;
        }
    }

    /// <summary>
    /// 控制音效的大小
    /// </summary>
    public float EffectVolume
    {
        get
        {
            return m_EffectAudio.volume;
        }
        set
        {
            m_EffectAudio.volume = value;
        }
    }
    /// <summary>
    /// 音效的开关
    /// </summary>
    public bool EffectMute
    {
        get
        {
            return m_EffectAudio.mute;
        }
        set
        {
            m_EffectAudio.mute = value;
        }
    }

    protected override void Awake()
    {
        base.Awake();

        //实例化音乐源
        m_bgAudio = gameObject.AddComponent<AudioSource>();
        m_bgAudio.loop = true;  //开启循环
        m_bgAudio.playOnAwake = false;  //开始播放

        //实例化音乐源
        m_EffectAudio = gameObject.AddComponent<AudioSource>();
        m_EffectAudio.loop = false;
        m_EffectAudio.playOnAwake = false;

    }

    private void PlayBgBase(object bgName, bool restart = false)
    {
        BgVolume = 0.5f;
        //定义一个空的字符串
        string curBgName = string.Empty;
        if (m_bgAudio.clip != null)
        {
            // 得到这个音频剪辑的name
            curBgName = m_bgAudio.clip.name;
        }

        // 根据用户的音频片段名称,找到AudioClip,然后播放
        // AudioResourcesManager是提前定义好的查找音频剪辑对应路径的单例脚本,并动态加载出来
        AudioClip clip = AudioResourceManager.Instance.Load<AudioClip>(bgName);

        // 如果找到了,不为空
        if (clip != null)
        {
            // 如果这个音频剪辑已经赋值给了音频源,且正在播放,name直接跳出
            if (clip.name == curBgName && !restart)
            {
                return;
            }
            // 否则,把音频剪辑资源赋值给音频源,然后播放
            m_bgAudio.clip = clip;
            m_bgAudio.Play();
        }
        else
        {
            //没有找到直接报错
            //异常,调用写日志的工具类
            UnityEngine.Debug.Log("没有找到音频片段");
        }
    }

    //枚举里有多少个就要写多少个方法
    /// <summary>
    /// 这个为背景音乐测试方法
    /// </summary>
    /// <param name="bgName"></param>
    /// <param name="restart"></param>
    public void PlayBgTest(AudioType.TestAudio bgName, bool restart = false)
    {
        PlayBgBase(bgName,restart);
    }
    /// <summary>
    /// BGM
    /// </summary>
    /// <param name="bgName"></param>
    /// <param name="restart"></param>
    public void PlayBGM(AudioType.BGM bgName, bool restart = false)
    {
        PlayBgBase(bgName, restart);
    }

    /// <summary>
    /// 游戏场景背景音乐
    /// </summary>
    /// <param name="restart"></param>
    public void PlayGameSceneBGM(bool restart = false)
    {
        int x = Random.Range(1, 3);
        switch (x)
        {
            case 1:
                PlayBgBase(AudioType.BGM.GameSound_1, restart);
                break;
            case 2:
                PlayBgBase(AudioType.BGM.GameSound_2, restart);
                break;
            default:
                break;
        }
    }


    /// <summary>
    /// 音效播放基类
    /// </summary>
    /// <param name="clip">音频片段</param>
    /// <param name="defAudio">如果defAudio=true,直接播放,否在相机出播放</param>
    /// <param name="volume">音量</param>
    private void PlayEffectBase(AudioClip clip, bool defAudio = true, float volume = 1f)
    {
        
        
        if (clip == null)
        {
            UnityEngine.Debug.LogError("没有找到音效片段");
            return;
        }
        //否则,就是clip不为空的话,如果defAudio=true,直接播放  
        if (defAudio)
        {
            m_EffectAudio.PlayOneShot(clip, volume);
        }
        else
        {
            //指定点播放  
            AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position, volume);
        }
    }


    //枚举里有多少个就要写多少个方法
    /// <summary>
    /// 这个为测试播放音乐特效方法
    /// </summary>
    /// <param name="effectName"></param>
    /// <param name="defAudio"></param>
    /// <param name="volume"></param>
    public void PlayEffectTest(AudioType.TestAudio effectName, bool defAudio = true, float volume = 1f)
    {
        AudioClip clip = AudioResourceManager.Instance.Load<AudioClip>(effectName);
        PlayEffectBase(clip, defAudio, volume);
    }
    /// <summary>
    /// 播放音效的方法
    /// </summary>
    /// <param name="effectName">音效名</param>
    /// <param name="defAudio"></param>
    /// <param name="volume"></param>
    public void PlayEffectSound(AudioType.Sound effectName, bool defAudio = true, float volume = 1f)
    {
        AudioClip clip = AudioResourceManager.Instance.Load<AudioClip>(effectName);
        PlayEffectBase(clip, defAudio, volume);
    }

    /// <summary>
    /// 游戏内播放固定短语的音效
    /// </summary>
    /// <param name="message">音效的名字</param>
    /// <param name="isMan">是否为男性</param>
    /// <param name="defAudio"></param>
    /// <param name="volume"></param>
    public void PlayEffectChatSound(string message, bool isMan = true, bool defAudio = true, float volume = 1)
    {
        // TODO传入消息内容的参数不确定
        string filePath = AudioPath(isMan) + "/Talk/";
        AudioClip clip = Resources.Load<AudioClip>(filePath + message);
        PlayEffectBase(clip, defAudio, volume);
        Debug.Log("播放短语音效:" + ShortWords.shortWords[int.Parse(message)]);
    }

    /// <summary>
    /// 游戏内音效(吃碰杠和)
    /// </summary>
    /// <param name="effectName"></param>
    /// <param name="isMan"></param>
    /// <param name="defAudio"></param>
    /// <param name="volume"></param>
    public void PlayEffectGameSound(AudioType.GameSound effectName, bool isMan = true, bool defAudio = true, float volume = 1f)
    {
        //地址
        string filePath = AudioPath(isMan) + "/Operate";
        AudioClip[] clips = Resources.LoadAll<AudioClip>(filePath);
        List<AudioClip> list = new List<AudioClip>();
        string startWith = string.Empty;
        switch (effectName.ToString())
        {
            case "Chi":
                startWith = "chi_";
                break;
            case "Pong":
                startWith = "peng_";
                break;
            case "Kong":
                startWith = "gang_";
                break;
            case "Hu":
                startWith = "hu_";
                break;
            default:
                break;
        }
        //查找指定资源
        for (int i = 0; i < clips.Length; i++)
        {
            if (clips[i].name.StartsWith(startWith))
            {
                list.Add(clips[i]);
            }
        }
        //随机资源
        AudioClip clip = list[Random.Range(0, list.Count)];
        // 播放音效
        PlayEffectBase(clip, defAudio, volume);
    }

    /// <summary>
    /// 打牌音效的外部调用方法
    /// </summary>
    /// <param name="CardID">牌的ID</param>
    /// <param name="isMan"></param>
    /// <param name="defAudio"></param>
    /// <param name="volume"></param>
    public void PlayEffectCard(int CardID, bool isMan = true, bool defAudio = true, float volume = 1f)
    {
        //地址
        string filePath= AudioPath(isMan) + "/Chip/";
        //加载资源
        AudioClip[] clips = Resources.LoadAll<AudioClip>(filePath);
        List<AudioClip> list = new List<AudioClip>();
        string startWith = CardID.ToString() + "_";
        //查找指定资源
        for (int i = 0; i < clips.Length; i++)
        {
            if (clips[i].name.StartsWith(startWith))
            {
                list.Add(clips[i]);
            }
        }
        //随机资源
        AudioClip clip = list[Random.Range(0, list.Count)];
        // 播放音效
        PlayEffectBase(clip, defAudio, volume);
    }

    /// <summary>
    /// 设置声音地址
    /// 根据性别
    /// 语言:普通话/杭州方言
    /// </summary>
    /// <param name="isMan"></param>
    /// <returns></returns>
    private string AudioPath(bool isMan)
    {
        // 地址准备
        string languages = string.Empty;
        switch (language)
        {
            case Language.Mandarin:
                languages = "m_";
                break;
            case Language.HangZhouLocalism:
                languages = "d_";
                break;
            default:
                break;
        }
        string sex = string.Empty;
        if (isMan)
        {
            sex = "men";
        }
        else
        {
            sex = "women";
        }
        //地址
        string path = "AudioClip/Voice/Mah/" + languages + sex;
        return path;
    }

    /// <summary>
    /// 语言选择(语音)
    /// </summary>
    public Language language = Language.Mandarin;

    public enum Language
    {
        Mandarin,
        HangZhouLocalism
    }

}

 

游戏内音效的管理类,可通用

标签:ips   没有   volume   场景   vol   empty   rest   returns   ipa   

原文地址:http://www.cnblogs.com/0315cz/p/8046342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!