Audio.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /**
  2. * 魔力宝贝图档解析脚本 - CGTool
  3. *
  4. * @Author HonorLee (dev@honorlee.me)
  5. * @Version 1.0 (2023-08-26)
  6. * @License GPL-3.0
  7. *
  8. * AudioTool.cs 音频工具
  9. * 本工具用于加载音频AudioClip,音频文件位于Assets/Resources/Audio目录下并使用Resources.Load加载
  10. * 请将Crossgate的音频目录bgm、se拷贝到Assets/Resources/Audio目录下
  11. * 如有其他需要可调整加载方式
  12. */
  13. using System.Collections;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using Unity.VisualScripting;
  17. using UnityEngine;
  18. using UnityEngine.Networking;
  19. namespace CrossgateToolkit
  20. {
  21. public static class Audio
  22. {
  23. // 背景音频缓存
  24. private static Dictionary<int, AudioClip> _bgmDic = new Dictionary<int, AudioClip>();
  25. // 声效音频缓存
  26. private static Dictionary<int, AudioClip> _effectDic = new Dictionary<int, AudioClip>();
  27. public enum Type
  28. {
  29. BGM,
  30. EFFECT
  31. }
  32. // 播放指定类型、编号的音频AudioClip
  33. public static void Play(AudioSource audioSource,Type type, int id)
  34. {
  35. AudioClip audioClip;
  36. Dictionary<int,AudioClip> dic = type == Type.BGM ? _bgmDic : _effectDic;
  37. if (dic.TryGetValue(id, out audioClip))
  38. {
  39. _playAudio(audioSource, audioClip);
  40. }
  41. else
  42. {
  43. Dictionary<int,string> map = type == Type.BGM ? _bgmMap : _effectMap;
  44. if(map.TryGetValue(id, out string audioName))
  45. {
  46. string path = type == Type.BGM ? CGTool.PATH.BGM : CGTool.PATH.AUDIO;
  47. if (string.IsNullOrEmpty(path))
  48. {
  49. path = type == Type.BGM ? "Audio/bgm" : "Audio/se";
  50. audioClip = Resources.Load<AudioClip>(path + "/" + audioName);
  51. if (audioClip == null) return;
  52. dic[id] = audioClip;
  53. _playAudio(audioSource, audioClip);
  54. }
  55. else
  56. {
  57. DirectoryInfo directoryInfo = new DirectoryInfo(path);
  58. FileInfo[] files = directoryInfo.GetFiles(audioName + ".wav", SearchOption.AllDirectories);
  59. if (files.Length > 0)
  60. {
  61. string filePath = files[0].FullName;
  62. CoroutineRunner.instance.StartCoroutine(LoadAudioClipAsync(filePath, loadedAudioClip =>
  63. {
  64. if (loadedAudioClip != null)
  65. {
  66. dic[id] = loadedAudioClip;
  67. _playAudio(audioSource, loadedAudioClip);
  68. }
  69. }));
  70. }
  71. }
  72. }
  73. }
  74. }
  75. private static void _playAudio(AudioSource audioSource, AudioClip audioClip)
  76. {
  77. audioSource.Stop();
  78. audioSource.clip = audioClip;
  79. audioSource.Play();
  80. }
  81. private delegate void AudioClipLoaded(AudioClip audioClip);
  82. private static IEnumerator LoadAudioClipAsync(string filePath, AudioClipLoaded onAudioLoaded)
  83. {
  84. if (File.Exists(filePath))
  85. {
  86. string audioURL = "file://" + filePath;
  87. using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(audioURL, AudioType.UNKNOWN))
  88. {
  89. yield return www.SendWebRequest();
  90. if (www.result == UnityWebRequest.Result.Success)
  91. {
  92. AudioClip audioClip = DownloadHandlerAudioClip.GetContent(www);
  93. onAudioLoaded?.Invoke(audioClip);
  94. }
  95. else
  96. {
  97. onAudioLoaded?.Invoke(null);
  98. }
  99. }
  100. }
  101. else
  102. {
  103. onAudioLoaded?.Invoke(null);
  104. }
  105. }
  106. private static Dictionary<int, string> _bgmMap = new Dictionary<int, string>()
  107. {
  108. [200] = "cgbgm_m0",
  109. [201] = "cgbgm_m1",
  110. [202] = "cgbgm_m2",
  111. [203] = "cgbgm_m3",
  112. [204] = "cgbgm_m4",
  113. [209] = "cgbgm_f0",
  114. [210] = "cgbgm_f1",
  115. [211] = "cgbgm_f2",
  116. [212] = "cgbgm_d0",
  117. [213] = "cgbgm_d1",
  118. [214] = "cgbgm_d2",
  119. [215] = "cgbgm_d3",
  120. [216] = "cgbgm_d4",
  121. [205] = "cgbgm_b0",
  122. [206] = "cgbgm_b1",
  123. [207] = "cgbgm_b2",
  124. [208] = "cgbgm_b3",
  125. [217] = "cgbgm_t0",
  126. [219] = "exbgm_s0",
  127. [220] = "exbgm_f0",
  128. [221] = "exbgm_m0",
  129. [222] = "v2bgm_f0",
  130. [223] = "v2bgm_m0",
  131. [224] = "v2bgm_ex",
  132. [225] = "v2bgm_ex",
  133. [226] = "puk2_battle1",
  134. [227] = "puk2_battle2",
  135. [228] = "puk2_field1",
  136. [229] = "puk2_mati",
  137. [230] = "puk2_sinden",
  138. [231] = "puk2_yama",
  139. [232] = "puk2_haikyo",
  140. [233] = "puk2_m_town",
  141. [234] = "puk2_OP",
  142. [235] = "puk3_battle1",
  143. [236] = "puk3_battle2",
  144. [237] = "puk3_dungeon",
  145. [238] = "puk3_kame",
  146. [239] = "puk3_kujira",
  147. [240] = "puk3_kumo",
  148. [241] = "puk3_love",
  149. [242] = "puk3_playerbattle",
  150. [243] = "PUK3_title",
  151. };
  152. private static Dictionary<int, string> _effectMap = new Dictionary<int, string>()
  153. {
  154. [1] = "cgnat00",
  155. [2] = "cgnat01",
  156. [3] = "cgnat02",
  157. [4] = "cgnat03",
  158. [5] = "cgnat04",
  159. [6] = "cgnat05a",
  160. [7] = "cgnat05b",
  161. [8] = "cgnat06a",
  162. [9] = "cgnat06b",
  163. [10] = "cgnat07",
  164. [11] = "cgnat08",
  165. [12] = "cgnat09",
  166. [13] = "cgnat10",
  167. [14] = "cgnat11",
  168. [15] = "exnat00",
  169. [16] = "v2mon150a",
  170. [17] = "34sand_clock",
  171. [18] = "35sand_clock",
  172. [19] = "36wind",
  173. [20] = "37bird",
  174. [21] = "puk3_Wind01",
  175. [22] = "puk3_Wind02",
  176. [23] = "puk3_Wind03",
  177. [24] = "puk3_gaya01",
  178. [25] = "puk3_drop01",
  179. [26] = "puk3_drop02",
  180. [51] = "cgsys00",
  181. [52] = "cgsys01",
  182. [53] = "cgsys02",
  183. [54] = "cgsys03",
  184. [55] = "cgsys04",
  185. [56] = "cgsys05",
  186. [57] = "cgsys06",
  187. [58] = "cgsys07",
  188. [59] = "cgsys08",
  189. [60] = "cgsys09",
  190. [61] = "cgsys10a",
  191. [62] = "cgsys10b",
  192. [63] = "cgsys11",
  193. [64] = "cgsys12",
  194. [65] = "cgsys13a",
  195. [66] = "cgsys13b",
  196. [67] = "cgsys13c",
  197. [68] = "cgsys14",
  198. [69] = "cgsys15",
  199. [71] = "cgsys17",
  200. [72] = "cgsys18",
  201. [73] = "cgsys19",
  202. [74] = "cgsys20",
  203. [75] = "cgsys21",
  204. [76] = "cgsys22",
  205. [77] = "cgsys23",
  206. [78] = "cgsys24",
  207. [79] = "cgsys25",
  208. [101] = "cgply00a",
  209. [102] = "cgply00b",
  210. [103] = "cgply01a",
  211. [104] = "cgply01b",
  212. [105] = "cgply02a",
  213. [106] = "cgply02b",
  214. [107] = "cgply03a",
  215. [108] = "cgply03b",
  216. [109] = "cgply04a",
  217. [110] = "cgply04b",
  218. [111] = "cgply05a",
  219. [112] = "cgply05b",
  220. [113] = "cgply06a1",
  221. [114] = "cgply06b1",
  222. [115] = "cgply06a2",
  223. [116] = "cgply06b2",
  224. [117] = "cgply07a",
  225. [118] = "cgply07b",
  226. [131] = "cgply06a2",
  227. [132] = "cgply06b2",
  228. [133] = "cgply11a",
  229. [134] = "cgply11b",
  230. [135] = "cgply12a",
  231. [136] = "cgply12b",
  232. [137] = "cgply13a",
  233. [138] = "cgply13b",
  234. [139] = "cgply14a",
  235. [140] = "cgply14b",
  236. [141] = "cgply15",
  237. [142] = "cgply16",
  238. [143] = "cgply17",
  239. [147] = "cgply00a",
  240. [150] = "cgply11b",
  241. [151] = "cgmon00a",
  242. [152] = "cgmon00b",
  243. [153] = "cgmon01",
  244. [154] = "cgmon02a",
  245. [155] = "cgmon02b",
  246. [156] = "cgmon03b",
  247. [157] = "cgmon10",
  248. [158] = "cgmon20",
  249. [159] = "cgmon24",
  250. [160] = "cgmon30",
  251. [161] = "cgmon31",
  252. [162] = "cgmon41",
  253. [163] = "cgmon43",
  254. [164] = "cgmon50a",
  255. [165] = "cgmon50b",
  256. [166] = "cgmon51",
  257. [167] = "cgmon52",
  258. [168] = "cgmon60",
  259. [169] = "cgmon61",
  260. [171] = "cgmon63",
  261. [172] = "cgmon90",
  262. [173] = "cgmon91",
  263. [174] = "cgmon92",
  264. [175] = "cgmon93",
  265. [180] = "cgmon_bs1",
  266. [181] = "cgmon_bs2",
  267. [182] = "cgmon_bs3",
  268. [183] = "cgmon_bs4",
  269. [184] = "cgmon_bh1",
  270. [185] = "cgmon_bh2",
  271. [186] = "cgmon_bh3",
  272. [187] = "cgmon_bh4",
  273. [190] = "cgmon_m00",
  274. [191] = "cgmon_m01",
  275. [192] = "cgmon_m02",
  276. [198] = "cgmon_sample01",
  277. [199] = "cgmon_sample02",
  278. [200] = "cgmon_sample03",
  279. [201] = "cgbtl00",
  280. [202] = "cgbtl01",
  281. [204] = "cgbtl03",
  282. [205] = "cgbtl04",
  283. [206] = "cgbtl05",
  284. [207] = "cgbtl06",
  285. [208] = "cgbtl07",
  286. [209] = "cgbtl08",
  287. [210] = "cgbtl09",
  288. [211] = "cgbtl10",
  289. [212] = "cgbtl11",
  290. [213] = "cgbtl12",
  291. [214] = "cgbtl13",
  292. [215] = "cgbtl14",
  293. [216] = "cgbtl15",
  294. [217] = "cgbtl16",
  295. [218] = "cgbtl17",
  296. [251] = "cgefc00",
  297. [252] = "cgefc01",
  298. [253] = "cgefc02",
  299. [254] = "cgefc03",
  300. [255] = "cgefc04",
  301. [256] = "cgefc05",
  302. [257] = "cgefc06",
  303. [258] = "cgefc07",
  304. [259] = "cgefc08",
  305. [260] = "cgefc09",
  306. [261] = "cgefc10",
  307. [262] = "cgefc11",
  308. [263] = "cgefc12",
  309. [264] = "cgefc13",
  310. [266] = "cgefc15",
  311. [267] = "cgefc16",
  312. [268] = "cgefc17",
  313. [269] = "cgefc18",
  314. [270] = "cgefc19",
  315. [271] = "cgefc20",
  316. [272] = "cgefc21",
  317. [273] = "cgefc22",
  318. [274] = "cgefc23",
  319. [275] = "cgefc24",
  320. [276] = "cgefc25",
  321. [277] = "cgefc26",
  322. [278] = "cgefc27",
  323. [279] = "cgefc28",
  324. [280] = "cgefc29",
  325. [281] = "cgefc30",
  326. [282] = "cgefc31",
  327. [283] = "cgefc32",
  328. [284] = "cgefc33",
  329. [285] = "cgefc34",
  330. [286] = "cgefc35",
  331. [287] = "cgefc36",
  332. [288] = "cgefc37a",
  333. [289] = "cgefc37b",
  334. [290] = "cgefc37c",
  335. [291] = "cgefc38",
  336. [296] = "v2monex1",
  337. [297] = "v2monex2",
  338. [298] = "v2monex3",
  339. [300] = "v2mon100",
  340. [301] = "v2mon110",
  341. [302] = "v2mon111a",
  342. [303] = "v2mon111b",
  343. [304] = "v2mon120",
  344. [305] = "v2mon121a",
  345. [306] = "v2mon121b",
  346. [307] = "v2mon121c",
  347. [308] = "v2mon130",
  348. [309] = "v2mon140",
  349. [310] = "v2mon150a",
  350. [311] = "v2mon150b",
  351. [312] = "v2mon161",
  352. [313] = "v2mon170a",
  353. [314] = "v2mon170b",
  354. [315] = "v2mon171a",
  355. [316] = "v2mon171b",
  356. [317] = "v2mon190",
  357. [318] = "v2mon191",
  358. [319] = "v2monex0",
  359. [400] = "01small_amae_new",
  360. [401] = "02small_normal",
  361. [402] = "02small_normal_new",
  362. [403] = "03small_iyaiya",
  363. [404] = "03small_iyaiya_new",
  364. [405] = "04fish_normal",
  365. [406] = "05fish_shout",
  366. [407] = "06fish_amae",
  367. [408] = "09kame_amae",
  368. [409] = "10yagi_shout",
  369. [410] = "11yagi_normal",
  370. [411] = "12yagi_amae",
  371. [412] = "13bird_normal",
  372. [413] = "14bird_shout",
  373. [414] = "15bird_amae",
  374. [415] = "16fish_normal",
  375. [416] = "17fish_shout",
  376. [417] = "18kame_normal",
  377. [418] = "19kame_shout",
  378. [419] = "20animal_normal",
  379. [420] = "21animal_shout",
  380. [421] = "22bird_normal",
  381. [422] = "23bird_shout",
  382. [423] = "24Monstor",
  383. [424] = "25_1_off",
  384. [425] = "26ground_on",
  385. [426] = "27ground_off",
  386. [427] = "28_water_on",
  387. [428] = "29_water_of",
  388. [429] = "30fire_on",
  389. [430] = "31fire_of",
  390. [431] = "32_wind_on",
  391. [432] = "32_wind_on2",
  392. [433] = "33_wind_off",
  393. [435] = "36wind",
  394. [436] = "37bird",
  395. [437] = "38make_gild",
  396. [438] = "39levelup",
  397. };
  398. }
  399. }