From

作者: 醉酒青牛_fa4e | 来源:发表于2019-04-14 13:43 被阅读0次

    门面模式

    image.png

    功能:
    AudioSource

    1.能够重复利用AudioSource(objPool)
    2.无限扩展
    3.去除多余的AudioSource

    AudioClips

    1.管理类
    2.加载到内存中
    3.通过名字查找到对应的片段


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    /// <summary>
    /// source管理类
    /// 初始化3个source
    /// 不够用时new,闲置时调用释放方法Release释放source
    /// </summary>
    public class SourcesManager {

    GameObject ower;
    
    List<AudioSource> allSources;
    /// <summary>
    /// 初始化3个source
    /// </summary>
    /// <param name="tmpOwer"></param>
    public SourcesManager(GameObject tmpOwer)
    {
        this.ower = tmpOwer;
    
        allSources = new List<AudioSource>();
    
        for (int i = 0; i < 3; i++)
        {
            AudioSource tmpSource = ower.AddComponent<AudioSource>();
    
            allSources.Add(tmpSource);
        }
    
    
    }
    
    /// <summary>
    /// 获取闲置的source
    /// </summary>
    /// <returns></returns>
    public AudioSource GetFreeAudio()
    {
        for (int i = 0; i < allSources.Count; i++)
        {
            if (!allSources[i].isPlaying)
            {
                return allSources[i];
            }
        }
        AudioSource tmpSource = new AudioSource();
        allSources.Add(tmpSource);
    
        return tmpSource; ;
    }
    /// <summary>
    /// 释放
    /// </summary>
    public void Release()
    {
        int tmpCount = 0;
    
        List<AudioSource> tmpAudio = new List<AudioSource>();
    
    
        for (int i = 0; i < allSources.Count; i++)
        {
            if (!allSources[i].isPlaying)
            {
                tmpCount++;
                if (tmpCount > 3)
                {
                    tmpAudio.Add(allSources[i]);
                }
            }
        }
        if (tmpAudio.Count > 0)
        {
            for (int i = 0; i < tmpAudio.Count; i++)
            {
                //在集合中删除
                allSources.Remove(tmpAudio[i]);
                //在场景中删除
                GameObject.Destroy(tmpAudio[i]);
            }
            
        }
        tmpAudio.Clear();
    }
    /// <summary>
    /// 通过music名字获取正在使用的source
    /// </summary>
    /// <param name="musicName"></param>
    /// <returns></returns>
    public AudioSource GetAudioSource(string musicName)
    {
        for (int i = 0; i < allSources.Count; i++)
        {
            if (allSources[i].isPlaying && allSources[i].clip.name.Equals(musicName))
            {
                return allSources[i];
            }
        }
        return null;
    }
    
    /// <summary>
    /// 通过music名字停止相应的source
    /// </summary>
    /// <param name="musicName"></param>
    public void StopMusic(string musicName)
    {
        AudioSource tmpSource = GetAudioSource(musicName);
    
        if (tmpSource != null)
        {
            tmpSource.Stop();
        }
    }
    

    }

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    /// <summary>
    /// 单个clip管理类
    /// 对单个clip进行操作
    /// </summary>
    public class SingleClip {

    AudioClip myClip;
    
    AudioSource source;
    
    public SingleClip(AudioClip tmpClip)
    {
        myClip = tmpClip;
    }
    
    public void Play(AudioSource tmpSource)
    {
        source = tmpSource;
    
        source.clip = myClip;
    
        source.Play();
    }
    

    }

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    /// <summary>
    /// clip管理类
    /// </summary>
    public class ClipsManager {

    public SingleClip[] allClips;
    
    
    /// <summary>
    /// 这里文件名要通过读写txt得到,给数组
    /// </summary>
    public string[] clipsName = { "", "", "" };
    
    public ClipsManager() {
        Init();
    }
    
    
    
    /// <summary>
    /// 初始化SingleClip,把要用的clip加载到内存
    /// 
    /// </summary>
    public void Init()
    {
        allClips = new SingleClip[clipsName.Length];
    
        for (int i = 0; i < clipsName.Length; i++)
        {
            AudioClip tmpClip= Resources.Load(clipsName[i]) as AudioClip;
    
            allClips[i] = new SingleClip(tmpClip);
        }
    }
    
    /// <summary>
    /// 获取clip的索引
    /// </summary>
    /// <param name="clipName">名字</param>
    /// <returns>返回索引</returns>
    public int GetClipIndex(string clipName)
    {
        for (int i = 0; i < clipName.Length; i++)
        {
            if (clipName.Equals(clipName[i]))
            {
                return i;
            }
        }
        return -1;
    }
    /// <summary>
    /// 获取clip
    /// </summary>
    /// <param name="clipName"></param>
    /// <returns>SingleClip</returns>
    public SingleClip GetClip(string clipName)
    {
        int tmpIndex = GetClipIndex(clipName);
        if (tmpIndex != -1)
        {
            return allClips[tmpIndex];
        }
        else
        {
            return null;
        }
    }
    

    }

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    /// <summary>
    /// 管理类
    /// 外部使用此类来完成一些操作
    /// </summary>
    public class AudioManager : MonoBehaviour {

    public static AudioManager Instance;
    
    SourcesManager sourceManager;
    
    ClipsManager clipManager;
    private void Awake()
    {
        Instance = this;
        sourceManager = new SourcesManager(gameObject);
        clipManager = new ClipsManager();
    }
    public void Play(string musicName)
    {
        AudioSource tmpSource = sourceManager.GetFreeAudio();
    
        SingleClip tmpSingle = clipManager.GetClip(musicName);
    
        tmpSingle.Play(tmpSource);
    }
    public void Stop(string musicName)
    {       sourceManager.StopMusic(musicName);
    }    
    

    }

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    /// <summary>
    /// 使用audio类
    /// </summary>
    public class UseAudio : MonoBehaviour {

    // Update is called once per frame
    void Update () {
        if (Input.GetKey(KeyCode.A))
        {
            AudioManager.Instance.Play("");
        }
        if (Input.GetKey(KeyCode.B))
        {
            AudioManager.Instance.Stop("");
        }
    }
    

    }

    相关文章

      网友评论

          本文标题:From

          本文链接:https://www.haomeiwen.com/subject/jraiiqtx.html