﻿{"id":189,"date":"2025-03-23T13:24:49","date_gmt":"2025-03-23T05:24:49","guid":{"rendered":"http:\/\/www.d7gd.com\/?p=189"},"modified":"2025-03-24T14:21:30","modified_gmt":"2025-03-24T06:21:30","slug":"unity%e4%bd%bf%e7%94%a8ogg%e9%9f%b3%e4%b9%90%e5%88%b6%e4%bd%9cloop%e5%be%aa%e7%8e%af%e7%82%b9","status":"publish","type":"post","link":"https:\/\/www.d7gd.com\/?p=189","title":{"rendered":"unity\u4f7f\u7528ogg\u97f3\u4e50\u5236\u4f5cloop\u5faa\u73af\u70b9"},"content":{"rendered":"\n<p>\u6587\uff1a721\uff08www.d7gd.com\uff09<\/p>\n\n\n\n<p>ogg\u683c\u5f0f\u97f3\u4e50\u548cmp3\u97f3\u4e50\u76f8\u6bd4\uff0c\u4e00\u4e2a\u91cd\u8981\u7684\u529f\u80fd\u5c31\u662f\u97f3\u4e50\u5faa\u73af\u65f6\u5e76\u4e0d\u662f\u4ece\u5f00\u59cb\u4f4d\u7f6e\u5faa\u73af\uff0c\u800c\u662f\u4e2d\u95f4\u67092\u4e2a\u5faa\u73af\u70b9\uff0c\u4f1a\u5728\u64ad\u653e\u5230\u7b2c\u4e8c\u4e2a\u5faa\u73af\u70b9\u65f6\uff0c\u8df3\u8f6c\u5230\u7b2c\u4e00\u4e2a\u5faa\u73af\u70b9\u64ad\u653e\uff0c\u964d\u4f4e\u4e86\u5faa\u73af\u64ad\u653e\u65f6\u7684\u5272\u88c2\u611f\uff0c\u901a\u5e38\u5546\u4e1a\u6e38\u620f\u90fd\u4f1a\u7528\u8fd9\u79cd\u65b9\u6cd5\u64ad\u653eBGM\uff0c\u800c\u4e0d\u662f\u4ece\u5934\u5f00\u59cb\u5faa\u73af<\/p>\n\n\n\n<p>\u7531\u4e8echatgpt\u4ea7\u751f\u5e7b\u89c9\u544a\u8bc9\u6211unity\u80fd\u8bfbogg\u7684\u5faa\u73af\u70b9\uff08\u5b9e\u9645\u4e0a\u4e0d\u53ef\u4ee5\uff09\uff0c\u6211\u7814\u7a76\u4e86\u534a\u5929\u7f16\u8f91ogg\u6587\u4ef6\uff0c\u7ed3\u679c\u53d1\u73b0\u5e76\u4e0d\u662f\u6211\u7f16\u8f91\u7684\u4e0d\u5bf9\uff0c\u800c\u662funity\u8bfb\u4e0d\u4e86\uff0c\u8fd8\u662f\u8981\u9760\u4ee3\u7801\u89e3\u51b3\uff0c\u7f51\u4e0a\u67e5\u5230\u76f8\u5173\u5185\u5bb9\u4e0d\u591a\uff0c\u81ea\u5df1\u5199\u4e00\u7bc7<\/p>\n\n\n\n<p>\u9996\u5148\u8981\u770b\u4f60\u83b7\u53d6\u7684\u97f3\u4e50\u91cc\u9762\u6709\u6ca1\u6709\u5185\u7f6e\u5faa\u73af\u70b9LOOPSTART\uff0c\u5982\u679c\u662f\u4e13\u95e8\u7d20\u6750\u7f51\u7ad9\u4e0b\u8f7d\u7684\uff0c\u6216\u8005\u5176\u4ed6\u6e38\u620f\uff08\u6bd4\u5982RPGMaker\uff09\u62c6\u5305\u83b7\u53d6\u7684\uff0c\u53ef\u80fd\u4f1a\u6709\uff0cmp3\u6587\u4ef6\u80af\u5b9a\u6ca1\u6709\uff0c\u8fd9\u91cc\u63d0\u4f9b\u4e24\u79cd\u65b9\u6cd5<\/p>\n\n\n\n<p>1.foobar2000\u5b89\u88c5vgmstream\u63d2\u4ef6\u540e\uff0c\u53f3\u51fb\u66f2\u76ee\uff0c\u9009\u62e9properties\u53ef\u4ee5\u67e5\u770b\u6587\u4ef6\u7684\u6807\u7b7e\u4fe1\u606f<br>\u5176\u4e2dLOOPSTART\u5c31\u662f\u5faa\u73af\u5f00\u59cb\u70b9\uff0cLOOPLENGTH\u4e3a\u5faa\u73af\u957f\u5ea6\uff0cLOOPEND\uff08\u4e0d\u4e00\u5b9a\u6709\uff0c\u6570\u503c\u4e3a\u524d\u4e24\u8005\u76f8\u52a0\uff09\u4e3a\u5faa\u73af\u7ed3\u675f\u70b9\uff0c\u901a\u5e38\u4e3a\u66f2\u76ee\u7684\u7ed3\u5c3e<\/p>\n\n\n\n<p>2.\u4f7f\u7528\u97f3\u4e50\u7f16\u8f91\u8f6f\u4ef6Audacity\uff0c\u6253\u5f00\u6587\u4ef6\u540e\uff0c\u7f16\u8f91-\u5143\u6570\u636e\u7f16\u8f91\u5668\uff08Edit Metadata\uff09\uff0c\u91cc\u9762\u4e5f\u6709LOOPSTART\u548cLOOPLENGTH\u4fe1\u606f<\/p>\n\n\n\n<p>LOOPSTART\u548cLOOPLENGTH\u7684\u5355\u4f4d\u662f\u91c7\u6837\u70b9\uff08Sample Position\uff09\uff0c\u867d\u7136unity\u4e0d\u80fd\u76f4\u63a5\u8bfbogg\u6587\u4ef6\u91cc\u7684\u8fd9\u4e2a\u6807\u7b7e\uff0c\u4f46\u662f\u8ba4\u91c7\u6837\u70b9\uff0c\u6570\u503c\u901a\u5e38\u662f6-8\u4f4d\u6570\u7684\u6570\u5b57<\/p>\n\n\n\n<p>\u5982\u679c\u6587\u4ef6\u672c\u8eab\u6ca1\u6709\u5185\u7f6e\u5faa\u73af\u70b9\uff0c\u6216\u8005\u662fmp3\u6587\u4ef6\uff0c\u5c31\u8981\u81ea\u5df1\u542c\uff0c\u8bbe\u7f6e\u5faa\u73af\u70b9<br>\u97f3\u4e50\u7f16\u8f91\u8f6f\u4ef6Audacity\u6253\u5f00\u6587\u4ef6\uff0c\u89c6\u56fe-\u5de5\u5177\u680f-\u9009\u62e9\u5de5\u5177\u680f\uff08Selection Toolbar\uff09\uff0c\u8fd9\u4e00\u9879\u52fe\u9009\uff0c\u53f3\u4e0b\u89d2\u4f1a\u663e\u793a\u4f60\u5f53\u524d\u9009\u533a\u7684\u65f6\u95f4\u7b49\u4fe1\u606f\uff0c\u53f3\u51fb\u8fd9\u4e00\u5757\uff0c\u5c06\u663e\u793a\u5185\u5bb9\u6539\u4e3a<strong>\u91c7\u6837<\/strong>\uff08Sample\uff09 \uff0c\u8fd9\u91cc\u663e\u793a\u7684\u6570\u503c\u5c31\u53d8\u6210\u4e86\u4f60\u9009\u533a\u7684\u91c7\u6837\u70b9\uff0c\u70b9\u51fb\u4f60\u60f3\u8981\u5faa\u73af\u7684\u5730\u65b9\uff0c\u8bb0\u5f55\u4e0b\u8fd9\u4e24\u4e2aLOOPSTART\u548cLOOPEND\u7684\u91c7\u6837\u6570\u503c\uff0cLOOPLENGTH\u5c31\u662f\u4e24\u8005\u7684\u5dee\u503c\uff0c<br>LOOPEND\u540e\u9762\u7684\u97f3\u4e50\u53ef\u4ee5\u5220\u6389\uff0c\u8282\u7701\u5bb9\u91cf\uff0c\u7531\u4e8e\u6211\u4eec\u5e76\u4e0d\u9700\u8981unity\u8bfbogg\u4fe1\u606f\uff0c\u6240\u4ee5\u4e0d\u8f6c\u6362\u6210ogg\uff0c\u76f4\u63a5\u7528mp3\u4e5f\u884c\uff0c\u4f46\u662fogg\u53ef\u80fd\u6548\u679c\u4f1a\u597d\u4e00\u70b9\uff08chatgpt\u8bf4mp3\u5faa\u73af\u65f6\u5bb9\u6613\u6709\u5361\u987f\uff09<\/p>\n\n\n\n<p>unity\u4e2d\u4ee3\u7801\uff0c\u64ad\u653e\u90e8\u5206<br>BGM\u4e3a\u97f3\u4e50\u7684ogg\u6216mp3\u6587\u4ef6\uff0cBGMLoopStart\u662f\u5faa\u73af\u5f00\u59cb\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u4ece\u5934\u5faa\u73af\uff0c\u5c31\u586b0\uff0c\u628a\u4e0a\u9762\u83b7\u53d6\u7684\u6570\u503c\u586b\u8fdb\u53bb\uff0cBGMLoopEnd\u662f\u7ed3\u675f\u70b9\uff0c\u5982\u679c\u6587\u4ef6\u91cc\u6ca1\u6709\uff0c\u5c31\u662fLOOPSTART+LOOPLENGTH\u7684\u503c\uff0c<br>\u6211\u8fd9\u91cc\u8fd8\u6709\u4e2a\u97f3\u4e50\u6de1\u51fa\u6de1\u5165\u7684\u6548\u679c\uff0cBGMvolume\u4e3a\u97f3\u91cf\u4e0a\u9650\uff0c\u6570\u503c\u57280-1\u4e4b\u95f4\uff0c\u5982\u679c\u4e0d\u9700\u8981\u6de1\u51fa\u6de1\u5165\uff0c\u5c31\u76f4\u63a5bgmSource.clip = BGM[ID]; bgmSource.Play();<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\npublic AudioSource bgmSource;\npublic AudioClip&#91;] BGM;\npublic int&#91;] BGMLoopStart,BGMLoopEnd;\nfloat BGMvolume\nprivate int currentBGMID = -1,loopStart=0,loopEnd=0;\nprivate Coroutine currentBGMCoroutine;\n\npublic void BGMPlay(int ID){\n        \/\/\u5982\u679c\u6b63\u5728\u64ad\u653e\u5f53\u524d\u97f3\u4e50\uff0c\u5219\u4e0d\u91cd\u65b0\u64ad\u653e\n        if (bgmSource.clip == BGM&#91;ID] &amp;&amp; bgmSource.isPlaying){ return; }\n        \/\/ \u5982\u679c\u6709\u6b63\u5728\u64ad\u653e\u7684\u534f\u7a0b\uff0c\u505c\u6b62\u5b83\n        if (currentBGMCoroutine != null){\n            StopCoroutine(currentBGMCoroutine);}\n        \/\/ \u5f00\u59cb\u65b0\u7684\u80cc\u666f\u97f3\u4e50\u64ad\u653e\u534f\u7a0b\n        currentBGMCoroutine = StartCoroutine(PlayBGMWithFade(ID));\n}\n\nprivate IEnumerator PlayBGMWithFade(int ID){\n        float fadeOutTime = 1.0f; \/\/ \u6de1\u51fa\u65f6\u95f4\n        float fadeInTime = 1.0f; \/\/ \u6de1\u5165\u65f6\u95f4\n        \/\/ \u5982\u679c\u5f53\u524d\u6709\u97f3\u4e50\u5728\u64ad\u653e\uff0c\u5148\u6de1\u51fa\n        if (bgmSource.isPlaying)\n        {\n            for (float t = 0; t &lt; fadeOutTime; t += Time.unscaledDeltaTime)\n            {\n                bgmSource.volume = Mathf.Lerp(BGMvolume, 0.0f, t \/ fadeOutTime);\n                yield return null;\n            }\n            bgmSource.Stop(); \/\/ \u505c\u6b62\u5f53\u524d\u97f3\u4e50\n        }\n\n        \/\/ \u8bbe\u7f6e\u65b0\u7684\u97f3\u4e50\u5e76\u6de1\u5165\n        bgmSource.clip = BGM&#91;ID];\n        bgmSource.Play();\n\n        for (float t = 0; t &lt; fadeInTime; t += Time.unscaledDeltaTime)\n        {\n            bgmSource.volume = Mathf.Lerp(0.0f, BGMvolume, t \/ fadeInTime);\n            yield return null;\n        }\n        if(BGMLoopStart&#91;ID]&gt;0){\n            bgmSource.loop = false;\n        }else{\n            bgmSource.loop = true;\n        }\n        currentBGMID = ID;\n        loopStart = BGMLoopStart&#91;ID];\n        loopEnd = BGMLoopEnd&#91;ID];\n    }\n<\/code><\/pre>\n\n\n\n<p>\u5faa\u73af\u90e8\u5206\uff0cBGMLoop()\u653e\u5230Update()\u4e2d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nvoid BGMLoop(){\n        \/\/ \u5982\u679c\u5f53\u524d\u6709 Loop \u4fe1\u606f\n        if ( currentBGMID > 0 &amp;&amp; BGMLoopStart&#91;currentBGMID] > 0)\n        {\n            if (bgmSource.isPlaying){\n                if (bgmSource.timeSamples >= loopEnd||bgmSource.timeSamples >= bgmSource.clip.samples)\n                {\n                    bgmSource.timeSamples = loopStart;\n                }\n            }else{\n                bgmSource.Play();\n                bgmSource.timeSamples = loopStart;\n            }\n        }\n    }\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u662f\u7528\u4ee3\u7801\u63a7\u5236\u5faa\u73af\u70b9\u7684\u97f3\u4e50\uff0c\u6211\u8fd9\u91cc\u628aunity\u81ea\u8eab\u7684bgmSource.loop\u5173\u4e86<br>\u56e0\u4e3a\u5982\u679c\u97f3\u4e50\u64ad\u653e\u5b8c\u6bd5\uff0cunity\u4f1a\u81ea\u52a8\u4ece\u5934\u5f00\u59cb\u5faa\u73af\uff0c\u8fd9\u4e2a\u4f18\u5148\u7ea7\u9ad8\u4e8e\u6211\u7684\u4ee3\u7801<br>\u8fd9\u6837\u5199\uff0c\u65e0\u8bba\u662f\u5148\u5230\u8fbeloopEnd\u7684\u5faa\u73af\u70b9\uff0c\u8fd8\u662f\u97f3\u4e50\u64ad\u653e\u5b8c\u6bd5\uff0c\u90fd\u4f1a\u5230loopStart\u4f4d\u7f6e\u5f00\u59cb\u5faa\u73af<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\uff1a721\uff08www.d7gd.com\uff09 ogg\u683c\u5f0f\u97f3\u4e50\u548cmp3\u97f3\u4e50\u76f8\u6bd4\uff0c\u4e00\u4e2a\u91cd\u8981\u7684\u529f\u80fd\u5c31\u662f\u97f3\u4e50\u5faa\u73af\u65f6\u5e76\u4e0d\u662f\u4ece\u5f00 &#8230; <a title=\"unity\u4f7f\u7528ogg\u97f3\u4e50\u5236\u4f5cloop\u5faa\u73af\u70b9\" class=\"read-more\" href=\"https:\/\/www.d7gd.com\/?p=189\" aria-label=\"\u9605\u8bfb unity\u4f7f\u7528ogg\u97f3\u4e50\u5236\u4f5cloop\u5faa\u73af\u70b9\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[13,12],"class_list":["post-189","post","type-post","status-publish","format-standard","hentry","category-14","tag-ogg","tag-unity"],"_links":{"self":[{"href":"https:\/\/www.d7gd.com\/index.php?rest_route=\/wp\/v2\/posts\/189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.d7gd.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.d7gd.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.d7gd.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.d7gd.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=189"}],"version-history":[{"count":3,"href":"https:\/\/www.d7gd.com\/index.php?rest_route=\/wp\/v2\/posts\/189\/revisions"}],"predecessor-version":[{"id":192,"href":"https:\/\/www.d7gd.com\/index.php?rest_route=\/wp\/v2\/posts\/189\/revisions\/192"}],"wp:attachment":[{"href":"https:\/\/www.d7gd.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.d7gd.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.d7gd.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}