{"id":1144,"date":"2017-04-26T15:58:51","date_gmt":"2017-04-26T07:58:51","guid":{"rendered":"http:\/\/39.100.100.179\/?p=1144"},"modified":"2017-05-03T17:00:14","modified_gmt":"2017-05-03T09:00:14","slug":"%e5%a3%b0%e9%9f%b3%e7%ae%a1%e7%90%86%e7%b1%bbaudiomanager","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=1144","title":{"rendered":"\u58f0\u97f3\u7ba1\u7406\u7c7bAudioManager"},"content":{"rendered":"<p>\u4eca\u5929\u7ed9\u5927\u5bb6\u5206\u4eab\u4e0b\u521a\u521a\u505a\u7684AudioManager<\/p>\n<p>\u5b9e\u73b0\u4e86\u4ee5\u4e0b\u529f\u80fd\uff1a<\/p>\n<p>1.\u540c\u65f6\u64ad\u653e\u4e00\u9996\u80cc\u666f\u97f3\u4e50<\/p>\n<p>2.\u540c\u65f6\u64ad\u653eN\u4e2a\u97f3\u6548<\/p>\n<p>3.\u5bf9\u8c61\u6c60\u7ba1\u7406AudioSource\u7ec4\u4ef6<\/p>\n<p>4.\u80cc\u666f\u97f3\u4e50\u7684\u58f0\u97f3\u6de1\u5165\u6de1\u51fa<\/p>\n<p>5.\u8c03\u6574\u80cc\u666f\u97f3\u4e50\u7684\u97f3\u91cf<\/p>\n<p>6.\u8c03\u6574\u97f3\u6548\u7684\u97f3\u91cf<\/p>\n<p>7.\u672c\u5730\u7f13\u5b58\u58f0\u97f3\u97f3\u91cf<\/p>\n<p>8.\u5207\u6362\u573a\u666f\u4e0d\u9500\u6bc1<\/p>\n<p>9.\u83b7\u53d6\u64ad\u653e\u97f3\u6548\u7ed3\u675f\u56de\u8c03<\/p>\n<p>10.\u64ad\u653e3d\u97f3\u6548<\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\uff1a \u76f4\u63a5\u6302\u8f7d\u5230\u7a7a\u7269\u4f53\u4e0a<\/p>\n<p>\u5e94\u8be5\u662f\u5b9e\u73b0\u4e86\u5927\u90e8\u5206\u9879\u76ee\u4e2d\u9700\u8981\u7684\u58f0\u97f3\u7ba1\u7406\u7c7b\u7684\u529f\u80fd<\/p>\n<p>\u4e0b\u9762\u4e3a\u5b9e\u73b0\u7684\u4ee3\u7801<\/p>\n<pre class=\"lang:c# decode:true\">using UnityEngine;\r\nusing System.Collections;\r\nusing System.Collections.Generic;\r\nusing DG.Tweening;\r\nusing System;\r\n\r\npublic class AudioManager : MonoBehaviour\r\n{\r\n    private static AudioManager instance;\r\n\r\n    public static AudioManager Instance\r\n    {\r\n        get\r\n        {\r\n            return instance;\r\n        }\r\n    }\r\n\r\n    private Dictionary&lt;int, string&gt; audioPathDict;      \/\/ \u5b58\u653e\u97f3\u9891\u6587\u4ef6\u8def\u5f84\r\n\r\n    private AudioSource musicAudioSource;\r\n\r\n    private List&lt;AudioSource&gt; unusedSoundAudioSourceList;   \/\/ \u5b58\u653e\u53ef\u4ee5\u4f7f\u7528\u7684\u97f3\u9891\u7ec4\u4ef6\r\n\r\n    private List&lt;AudioSource&gt; usedSoundAudioSourceList;     \/\/ \u5b58\u653e\u6b63\u5728\u4f7f\u7528\u7684\u97f3\u9891\u7ec4\u4ef6\r\n\r\n    private Dictionary&lt;int, AudioClip&gt; audioClipDict;       \/\/ \u7f13\u5b58\u97f3\u9891\u6587\u4ef6\r\n\r\n    private float musicVolume = 1;\r\n\r\n    private float soundVolume = 1;\r\n\r\n    private string musicVolumePrefs = \"MusicVolume\";\r\n\r\n    private string soundVolumePrefs = \"SoundVolume\";\r\n\r\n    private int poolCount = 3;         \/\/ \u5bf9\u8c61\u6c60\u6570\u91cf\r\n\r\n    void Awake()\r\n    {\r\n        DontDestroyOnLoad( this.gameObject );\r\n        instance = this;\r\n\r\n        audioPathDict = new Dictionary&lt;int, string&gt;()       \/\/ \u8fd9\u91cc\u8bbe\u7f6e\u97f3\u9891\u6587\u4ef6\u8def\u5f84\u3002\u9700\u8981\u4fee\u6539\u3002 TODO\r\n        {\r\n            { 1, \"AudioClip\/Music\/MainMenuScene\" },\r\n            { 2, \"AudioClip\/Music\/BattleScene\" },\r\n            { 11, \"AudioClip\/Sound\/Sound1\"},\r\n            { 12, \"AudioClip\/Sound\/Sound2\"},\r\n            { 13, \"AudioClip\/Sound\/Sound3\"},\r\n            { 14, \"AudioClip\/Sound\/Sound4\"},\r\n            { 15, \"AudioClip\/Sound\/Sound5\"},\r\n        };\r\n\r\n        musicAudioSource = gameObject.AddComponent&lt;AudioSource&gt;();\r\n        unusedSoundAudioSourceList = new List&lt;AudioSource&gt;();\r\n        usedSoundAudioSourceList = new List&lt;AudioSource&gt;();\r\n        audioClipDict = new Dictionary&lt;int, AudioClip&gt;();\r\n    }\r\n\r\n    void Start()\r\n    {\r\n        \/\/ \u4ece\u672c\u5730\u7f13\u5b58\u8bfb\u53d6\u58f0\u97f3\u97f3\u91cf\r\n        if ( PlayerPrefs.HasKey( musicVolumePrefs ) )\r\n        {\r\n            musicVolume = PlayerPrefs.GetFloat( musicVolumePrefs );\r\n        }\r\n        if ( PlayerPrefs.HasKey( soundVolumePrefs ) )\r\n        {\r\n            musicVolume = PlayerPrefs.GetFloat( soundVolumePrefs );\r\n        }\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u64ad\u653e\u80cc\u666f\u97f3\u4e50\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"id\"&gt;&lt;\/param&gt;\r\n    \/\/\/ &lt;param name=\"loop\"&gt;&lt;\/param&gt;\r\n    public void PlayMusic( int id, bool loop = true )\r\n    {\r\n        \/\/ \u901a\u8fc7Tween\u5c06\u58f0\u97f3\u6de1\u5165\u6de1\u51fa\r\n        DOTween.To( () =&gt; musicAudioSource.volume, value =&gt; musicAudioSource.volume = value, 0, 0.5f ).OnComplete( () =&gt;\r\n        {\r\n            musicAudioSource.clip = GetAudioClip( id );\r\n            musicAudioSource.clip.LoadAudioData();\r\n            musicAudioSource.loop = loop;\r\n            musicAudioSource.volume = musicVolume;\r\n            musicAudioSource.Play();\r\n            DOTween.To( () =&gt; musicAudioSource.volume, value =&gt; musicAudioSource.volume = value, musicVolume, 0.5f );\r\n        } );\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u64ad\u653e\u97f3\u6548\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"id\"&gt;&lt;\/param&gt;\r\n    public void PlaySound( int id, Action action = null )\r\n    {\r\n        if ( unusedSoundAudioSourceList.Count != 0 )\r\n        {\r\n            AudioSource audioSource = UnusedToUsed();\r\n            audioSource.clip = GetAudioClip( id );\r\n            audioSource.clip.LoadAudioData();\r\n            audioSource.Play();\r\n\r\n            StartCoroutine( WaitPlayEnd( audioSource, action ) );\r\n        }\r\n        else\r\n        {\r\n            AddAudioSource();\r\n\r\n            AudioSource audioSource = UnusedToUsed();\r\n            audioSource.clip = GetAudioClip( id );\r\n            audioSource.clip.LoadAudioData();\r\n            audioSource.volume = soundVolume;\r\n            audioSource.loop = false;\r\n            audioSource.Play();\r\n\r\n            StartCoroutine( WaitPlayEnd( audioSource, action ) );\r\n        }\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u64ad\u653e3d\u97f3\u6548\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"id\"&gt;&lt;\/param&gt;\r\n    \/\/\/ &lt;param name=\"position\"&gt;&lt;\/param&gt;\r\n    public void Play3dSound( int id, Vector3 position )\r\n    {\r\n        AudioClip ac = GetAudioClip( id );\r\n        AudioSource.PlayClipAtPoint( ac, position );\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u5f53\u64ad\u653e\u97f3\u6548\u7ed3\u675f\u540e\uff0c\u5c06\u5176\u79fb\u81f3\u672a\u4f7f\u7528\u96c6\u5408\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"audioSource\"&gt;&lt;\/param&gt;\r\n    \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n    IEnumerator WaitPlayEnd( AudioSource audioSource, Action action )\r\n    {\r\n        yield return new WaitUntil( () =&gt; { return !audioSource.isPlaying; } );\r\n        UsedToUnused( audioSource );\r\n        if ( action != null )\r\n        {\r\n            action();\r\n        }\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u83b7\u53d6\u97f3\u9891\u6587\u4ef6\uff0c\u83b7\u53d6\u540e\u4f1a\u7f13\u5b58\u4e00\u4efd\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"id\"&gt;&lt;\/param&gt;\r\n    \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n    private AudioClip GetAudioClip( int id )\r\n    {\r\n        if ( !audioClipDict.ContainsKey( id ) )\r\n        {\r\n            if ( !audioPathDict.ContainsKey( id ) )\r\n                return null;\r\n            AudioClip ac = Resources.Load( audioPathDict[id] ) as AudioClip;\r\n            audioClipDict.Add( id, ac );\r\n        }\r\n        return audioClipDict[id];\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u6dfb\u52a0\u97f3\u9891\u7ec4\u4ef6\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n    private AudioSource AddAudioSource()\r\n    {\r\n        if ( unusedSoundAudioSourceList.Count != 0 )\r\n        {\r\n            return UnusedToUsed();\r\n        }\r\n        else\r\n        {\r\n            AudioSource audioSource = gameObject.AddComponent&lt;AudioSource&gt;();\r\n            unusedSoundAudioSourceList.Add( audioSource );\r\n            return audioSource;\r\n        }\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u5c06\u672a\u4f7f\u7528\u7684\u97f3\u9891\u7ec4\u4ef6\u79fb\u81f3\u5df2\u4f7f\u7528\u96c6\u5408\u91cc\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n    private AudioSource UnusedToUsed()\r\n    {\r\n        AudioSource audioSource = unusedSoundAudioSourceList[0];\r\n        unusedSoundAudioSourceList.RemoveAt( 0 );\r\n        usedSoundAudioSourceList.Add( audioSource );\r\n        return audioSource;\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u5c06\u4f7f\u7528\u5b8c\u7684\u97f3\u9891\u7ec4\u4ef6\u79fb\u81f3\u672a\u4f7f\u7528\u96c6\u5408\u91cc\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"audioSource\"&gt;&lt;\/param&gt;\r\n    private void UsedToUnused( AudioSource audioSource )\r\n    {\r\n        if ( usedSoundAudioSourceList.Contains( audioSource ) )\r\n        {\r\n            usedSoundAudioSourceList.Remove( audioSource );\r\n        }\r\n        if ( unusedSoundAudioSourceList.Count &gt;= poolCount )\r\n        {\r\n            Destroy( audioSource );\r\n        }\r\n        else if ( audioSource != null &amp;&amp; !unusedSoundAudioSourceList.Contains( audioSource ) )\r\n        {\r\n            unusedSoundAudioSourceList.Add( audioSource );\r\n        }\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u4fee\u6539\u80cc\u666f\u97f3\u4e50\u97f3\u91cf\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"volume\"&gt;&lt;\/param&gt;\r\n    public void ChangeMusicVolume( float volume )\r\n    {\r\n        musicVolume = volume;\r\n        musicAudioSource.volume = volume;\r\n\r\n        PlayerPrefs.SetFloat( musicVolumePrefs, volume );\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u4fee\u6539\u97f3\u6548\u97f3\u91cf\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"volume\"&gt;&lt;\/param&gt;\r\n    public void ChangeSoundVolume( float volume )\r\n    {\r\n        soundVolume = volume;\r\n        for ( int i = 0; i &lt; unusedSoundAudioSourceList.Count; i++ )\r\n        {\r\n            unusedSoundAudioSourceList[i].volume = volume;\r\n        }\r\n        for ( int i = 0; i &lt; usedSoundAudioSourceList.Count; i++ )\r\n        {\r\n            usedSoundAudioSourceList[i].volume = volume;\r\n        }\r\n\r\n        PlayerPrefs.SetFloat( soundVolumePrefs, volume );\r\n    }\r\n}<\/pre>\n<p>\u8c03\u7528\u65b9\u6cd5<\/p>\n<pre class=\"lang:c# decode:true\">using UnityEngine;\r\nusing System.Collections;\r\n\r\npublic class NewBehaviourScript : MonoBehaviour {\r\n\r\n\t\/\/ Use this for initialization\r\n\tvoid Start () {\r\n        AudioManager.Instance.PlayMusic( 1 );\r\n        AudioManager.Instance.PlaySound( 11, OnSoundPlayEnd );\r\n\r\n        AudioManager.Instance.ChangeMusicVolume( 0.5f );\r\n        AudioManager.Instance.ChangeSoundVolume( 0.5f );\r\n    }\r\n\r\n    void OnSoundPlayEnd()\r\n    {\r\n        Debug.Log( \"\u97f3\u6548\u64ad\u653e\u5b8c\u4e86\" );\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u7ed9\u5927\u5bb6\u5206\u4eab\u4e0b\u521a\u521a\u505a\u7684AudioManager \u5b9e\u73b0\u4e86\u4ee5\u4e0b\u529f\u80fd\uff1a 1.\u540c\u65f6\u64ad\u653e &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=1144\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,23],"tags":[256,257,255],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1144"}],"collection":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1144"}],"version-history":[{"count":11,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1144\/revisions"}],"predecessor-version":[{"id":1164,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1144\/revisions\/1164"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1144"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}