Unity 声音播放管理模块

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

public class SoundManager : MonoBehaviour
{

    private static SoundManager main;

    public static SoundManager Instance
    {
        get
        {
            if (main == null)
            {
                GameObject obj = new GameObject("SoundManager");
                main = obj.AddComponent<SoundManager>();
            }
            return main;
        }
    }

    private List<Sound> soundList = new List<Sound>();

    private float _deltaTime;

    void Update()
    {

        soundList.ToList().ForEach(x =>
        {
            if(x.isScaleTime == false){
                _deltaTime = Time.unscaledDeltaTime;
            }
            else {
                _deltaTime = Time.deltaTime;
            }

            x.playTime += _deltaTime;

            if (!x.audioSource.isPlaying && x.isFinish)
            {
                if(!(x.loop))
                x.Finish();
            }

        });
    }

    public Sound PlayFromAssetbundle(string name,bool isScaleTime = false)
    {
        AudioClip clip = Resources.Load<AudioClip>(name);
        if (clip == null)
            clip = Resources.Load<AudioClip>("通用点击");
        return PlaySound(clip, name);
    }

    public Sound PlayFromResource(string path,string name)
    {
        AudioClip clip = Resources.Load<AudioClip>(path + name);
        if (clip == null)
            clip = Resources.Load<AudioClip>("00通用点击");
        return PlaySound(clip,name);

    }
    /// <summary>
    /// 人声
    /// </summary>
    public Sound PlayVoice(string name)
    {
        AudioClip clip = Resources.Load<AudioClip>("Sound/Voice/zh/" + name);
        if (clip == null)
        {
            Debug.LogError("错误的语音:" + name);
            clip = Resources.Load<AudioClip>("00通用点击");
        }

        return PlaySound(clip, name);
    }
    /// <summary>
    /// 场景音效
    /// </summary>
    public Sound PlaySceneSound(string name)
    {
        AudioClip clip = Resources.Load<AudioClip>("Sound/SceneSound/" + name);
        if (clip == null)
        {
            Debug.LogError("错误的语音:" + name);
            clip = Resources.Load<AudioClip>("00通用点击");
        }
        return PlaySound(clip, name);
    }
    /// <summary>
    /// 动作音效
    /// </summary>
    public Sound PlayAnimaSound(string name)
    {
        //AudioClip clip = Resources.Load<AudioClip>("Sound/AnimaSound/" + name);
        //if (clip == null)
        //{
        //    Debug.LogError("错误的语音:" + name);
        //    clip = Resources.Load<AudioClip>("00通用点击");
        //}
        //return PlaySound(clip, name);

        return PlaySceneSound(name);
    }
    public Sound PlaySound(AudioClip clip,string soundName,bool isScaleTime = false)
    {
        Sound sound = new Sound(this);
        sound.clip = clip;
        sound.isScaleTime = isScaleTime;
        sound.name = soundName;
        sound.audioSource = this.gameObject.AddComponent<AudioSource>();
        sound.audioSource.clip = clip;
        sound.audioSource.Play();

        soundList.Add(sound);
        return sound;
    }
    public bool isPlay(string soundName)
    {
        List<Sound> sound = new List<Sound>(this.soundList.Where(s => s.name.Contains(soundName)));
        if(sound.Count>=1)
        {
            if (sound[0].audioSource.isPlaying)
                return true;
        }
        return false;
    }
    public void DestorySound(string name)
    {
        List<Sound> destoryList = SearchSound(name);

        destoryList.ToList().ForEach(x => x.Finish());
    }

    public void DestorySoundByID(string ID)
    {
        List<Sound> destoryList = SearchSoundByID(ID);
        destoryList.ToList().ForEach(x => x.Finish());
    }
    public void DestorySoundNoAction(string name)
    {
        List<Sound> destoryList = SearchSound(name);
        destoryList.ToList().ForEach(x => x.FinishNoComplete());
    }

    public List<Sound> SearchSound(string soundName)
    {
        List<Sound> sound = new List<Sound>(this.soundList.Where(s => s.name.Contains(soundName)));
        return sound;
    }
    public List<Sound> SearchSoundByID(string ID)
    {
        List<Sound> sound = new List<Sound>(this.soundList.Where(s => s.ID.Equals(ID)));
        return sound;
    }
    public float GetClipLength(string name)
    {
        AudioClip clip = Resources.Load<AudioClip>("Sound/Voice/zh/" + name);
        if (clip == null)
        {
            Debug.LogError("错误的语音:" + name);
            clip = Resources.Load<AudioClip>("00通用点击");
        }

        return clip.length;
    }
    public void RemoveSound(Sound sound)
    {
        Destroy(sound.audioSource);

        soundList.Remove(sound);
        sound = null;

    }

    public void PauseAllSound(){
        for(int i = 0;i<soundList.Count;i++){
            soundList [i].audioSource.Pause ();
        }
    }

    public void ResumeAllSound(){
        for(int i = 0;i<soundList.Count;i++){
            soundList [i].audioSource.UnPause ();
        }
    }

    public void ClearSound()
    {
        soundList.ToList().ForEach(x => x.FinishNoComplete());
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity音频管理器是一个用于在游戏项目中管理音频资源的工具。它的作用是加载、播放、暂停、调节音量等一系列与音频相关的操作。在项目中,我们可以将不同类型的音效资源拖拽到管理器中,然后通过代码调用管理器的接口来对音效进行控制。对于常规的音效操作,管理器已经提供了一些常用的功能,可以满足大部分需求。但对于一些特殊功能,比如在线剪切音频片段、动态调整音乐元素等,可能需要我们自行实现。 Unity中有两个重要的音频组件:AudioListener和AudioSource。通过AudioListener进行监听,通过AudioSource进行音频的播放。因此,使用音频管理器可以统一管理音频的相关操作,提高代码的模块化程度,方便维护和管理音频资源。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【Unity开发小技巧】AudioManager声音管理器](https://blog.csdn.net/qq_37310110/article/details/103311193)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Unity实现音频播放管理器](https://download.csdn.net/download/weixin_38735887/14840003)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [U3D游戏开发框架(四)——音频管理器](https://blog.csdn.net/LLLLL__/article/details/115150281)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值