KasaiUtility.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. public class KasaiUtility
  7. {
  8. public static IEnumerator FadeCoroutine(CanvasGroup canvas, bool is_fadeout, float fade_time = 0.5f, Action end_action = null, bool auto_setting = true, bool liner_valiable = true)
  9. {
  10. float timer = 0f;
  11. if (auto_setting)
  12. {
  13. if (is_fadeout)
  14. {
  15. if (!canvas.gameObject.activeSelf)
  16. {
  17. yield break;
  18. }
  19. canvas.interactable = false;
  20. }
  21. else
  22. {
  23. canvas.gameObject.SetActive(true);
  24. }
  25. }
  26. for (;;)
  27. {
  28. timer += Time.deltaTime;
  29. if (liner_valiable)
  30. {
  31. canvas.alpha = ((!is_fadeout) ? Mathf.Clamp01(timer / fade_time) : (1f - Mathf.Clamp01(timer / fade_time)));
  32. }
  33. else
  34. {
  35. canvas.alpha = KasaiUtility.SinRate01(timer, fade_time, is_fadeout, false);
  36. }
  37. if (timer > fade_time)
  38. {
  39. break;
  40. }
  41. yield return null;
  42. }
  43. if (end_action != null)
  44. {
  45. end_action();
  46. }
  47. if (auto_setting)
  48. {
  49. if (!is_fadeout)
  50. {
  51. canvas.interactable = true;
  52. }
  53. else
  54. {
  55. canvas.gameObject.SetActive(false);
  56. }
  57. }
  58. yield break;
  59. yield break;
  60. }
  61. public static IEnumerator FadeCoroutine(UIPanel ui_panel, bool is_fadeout, float fade_time = 0.5f, Action end_action = null, bool auto_setting = true, bool liner_valiable = true)
  62. {
  63. float timer = 0f;
  64. if (auto_setting)
  65. {
  66. if (is_fadeout)
  67. {
  68. if (!ui_panel.gameObject.activeSelf)
  69. {
  70. yield break;
  71. }
  72. }
  73. else
  74. {
  75. ui_panel.gameObject.SetActive(true);
  76. }
  77. }
  78. for (;;)
  79. {
  80. timer += Time.deltaTime;
  81. if (liner_valiable)
  82. {
  83. ui_panel.alpha = ((!is_fadeout) ? Mathf.Clamp01(timer / fade_time) : (1f - Mathf.Clamp01(timer / fade_time)));
  84. }
  85. else
  86. {
  87. ui_panel.alpha = KasaiUtility.SinRate01(timer, fade_time, is_fadeout, false);
  88. }
  89. if (timer > fade_time)
  90. {
  91. break;
  92. }
  93. yield return null;
  94. }
  95. if (end_action != null)
  96. {
  97. end_action();
  98. }
  99. if (auto_setting && is_fadeout)
  100. {
  101. ui_panel.gameObject.SetActive(false);
  102. }
  103. yield break;
  104. yield break;
  105. }
  106. public static IEnumerator TimeCroutine(float time_rimmit, Action<float> update_call, Action end_call = null)
  107. {
  108. float timer = 0f;
  109. for (;;)
  110. {
  111. timer += Time.deltaTime;
  112. if (update_call != null)
  113. {
  114. update_call(timer);
  115. }
  116. if (timer > time_rimmit)
  117. {
  118. break;
  119. }
  120. yield return null;
  121. }
  122. if (end_call != null)
  123. {
  124. end_call();
  125. }
  126. yield break;
  127. yield break;
  128. }
  129. public static IEnumerator CharaLoadWait(Action end_call)
  130. {
  131. while (GameMain.Instance.CharacterMgr.IsBusy())
  132. {
  133. yield return null;
  134. }
  135. yield return null;
  136. if (end_call != null)
  137. {
  138. end_call();
  139. }
  140. yield break;
  141. }
  142. public static float SinRate01(float current_time, float total_time, bool is_reverse = false, bool allow_timeover = false)
  143. {
  144. float rate = (!allow_timeover) ? Mathf.Clamp01(current_time / total_time) : (current_time / total_time);
  145. return KasaiUtility.SinRate01(rate, is_reverse);
  146. }
  147. public static float SinRate01(float rate, bool is_reverse = false)
  148. {
  149. if (!is_reverse)
  150. {
  151. return Mathf.Sin(rate * 90f * 0.017453292f);
  152. }
  153. return 1f - Mathf.Sin(rate * 90f * 0.017453292f);
  154. }
  155. public static float CosRate11(float current_time, float total_time, bool is_abs = false, bool is_reverse = false, bool allow_timeover = false)
  156. {
  157. float num = (!allow_timeover) ? Mathf.Clamp01(current_time / total_time) : (current_time / total_time);
  158. if (is_abs)
  159. {
  160. if (is_reverse)
  161. {
  162. return 1f - Mathf.Abs(Mathf.Cos(num * 180f * 0.017453292f));
  163. }
  164. return Mathf.Abs(Mathf.Cos(num * 180f * 0.017453292f));
  165. }
  166. else
  167. {
  168. if (is_reverse)
  169. {
  170. return -Mathf.Cos(num * 180f * 0.017453292f);
  171. }
  172. return Mathf.Cos(num * 180f * 0.017453292f);
  173. }
  174. }
  175. public static float DistanceIgnoreY(Vector3 a, Vector3 b)
  176. {
  177. a.y = (b.y = 0f);
  178. return Vector3.Distance(a, b);
  179. }
  180. public static Vector2 AngleDirection(float angle)
  181. {
  182. Vector2 zero = Vector2.zero;
  183. zero.x = Mathf.Cos(angle * 0.017453292f);
  184. zero.y = Mathf.Sin(angle * 0.017453292f);
  185. return zero;
  186. }
  187. public static void CsvRead(string file_name, Action<CsvParser, int, int> read_callback, int first_x = 0, int first_y = 1, Action not_exist_call = null)
  188. {
  189. if (file_name.IndexOf(".nei") < 0)
  190. {
  191. file_name += ".nei";
  192. }
  193. if (!GameUty.FileSystem.IsExistentFile(file_name))
  194. {
  195. if (not_exist_call != null)
  196. {
  197. not_exist_call();
  198. return;
  199. }
  200. NDebug.Assert("表がありません。" + file_name, false);
  201. }
  202. using (AFileBase afileBase = GameUty.FileSystem.FileOpen(file_name))
  203. {
  204. using (CsvParser csvParser = new CsvParser())
  205. {
  206. bool condition = csvParser.Open(afileBase);
  207. NDebug.Assert(condition, file_name + "\nopen failed.");
  208. for (int i = first_y; i < csvParser.max_cell_y; i++)
  209. {
  210. if (csvParser.IsCellToExistData(0, i))
  211. {
  212. for (int j = first_x; j < csvParser.max_cell_x; j++)
  213. {
  214. if (csvParser.IsCellToExistData(j, i))
  215. {
  216. if (read_callback != null)
  217. {
  218. read_callback(csvParser, j, i);
  219. }
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }
  226. }
  227. public static void CsvReadY(string file_name, Action<CsvParser, int> read_callback, int first_y = 1, Action not_exist_call = null)
  228. {
  229. if (file_name.IndexOf(".nei") < 0)
  230. {
  231. file_name += ".nei";
  232. }
  233. if (!GameUty.FileSystem.IsExistentFile(file_name))
  234. {
  235. if (not_exist_call != null)
  236. {
  237. not_exist_call();
  238. return;
  239. }
  240. NDebug.Assert("表がありません。" + file_name, false);
  241. }
  242. using (AFileBase afileBase = GameUty.FileSystem.FileOpen(file_name))
  243. {
  244. using (CsvParser csvParser = new CsvParser())
  245. {
  246. bool condition = csvParser.Open(afileBase);
  247. NDebug.Assert(condition, file_name + "\nopen failed.");
  248. for (int i = first_y; i < csvParser.max_cell_y; i++)
  249. {
  250. if (csvParser.IsCellToExistData(0, i))
  251. {
  252. if (read_callback != null)
  253. {
  254. read_callback(csvParser, i);
  255. }
  256. }
  257. }
  258. }
  259. }
  260. }
  261. public static void CsvReadAll(string file_name, Action<CsvParser> read_callback, Action not_exist_call = null)
  262. {
  263. if (file_name.IndexOf(".nei") < 0)
  264. {
  265. file_name += ".nei";
  266. }
  267. if (!GameUty.FileSystem.IsExistentFile(file_name))
  268. {
  269. if (not_exist_call != null)
  270. {
  271. not_exist_call();
  272. return;
  273. }
  274. NDebug.Assert("表がありません。" + file_name, false);
  275. }
  276. using (AFileBase afileBase = GameUty.FileSystem.FileOpen(file_name))
  277. {
  278. using (CsvParser csvParser = new CsvParser())
  279. {
  280. bool condition = csvParser.Open(afileBase);
  281. NDebug.Assert(condition, file_name + "\nopen failed.");
  282. if (read_callback != null)
  283. {
  284. read_callback(csvParser);
  285. }
  286. }
  287. }
  288. }
  289. public static void DrawAxis(Vector3 pos, Quaternion rot, Color x_color, Color y_color, Color z_color, float line_length = 0.0625f)
  290. {
  291. Debug.DrawLine(pos, pos + rot * Vector3.right * line_length, x_color);
  292. Debug.DrawLine(pos, pos + rot * Vector3.up * line_length, y_color);
  293. Debug.DrawLine(pos, pos + rot * Vector3.forward * line_length, z_color);
  294. }
  295. public static void DrawAxis(Vector3 pos, Quaternion rot, float line_length = 0.0625f)
  296. {
  297. KasaiUtility.DrawAxis(pos, rot, Color.red, Color.green, Color.blue, line_length);
  298. }
  299. public static void DrawObjAxis(Transform obj_trans, Color x_color, Color y_color, Color z_color, float line_length = 0.0625f)
  300. {
  301. KasaiUtility.DrawAxis(obj_trans.position, obj_trans.rotation, x_color, y_color, z_color, line_length);
  302. }
  303. public static void DrawObjAxis(Transform obj_trans, float ray_length = 0.0625f)
  304. {
  305. KasaiUtility.DrawObjAxis(obj_trans, Color.red, Color.green, Color.blue, ray_length);
  306. }
  307. public static Vector3 Bezier(Vector3 p0, Vector3 p1, Vector3 c, float t)
  308. {
  309. Vector3 a = Vector3.Lerp(p0, c, t);
  310. Vector3 b = Vector3.Lerp(c, p1, t);
  311. return Vector3.Lerp(a, b, t);
  312. }
  313. public static Vector3 Hermite(Vector3 p0, Vector3 p1, Vector3 v0, Vector3 v1, float t)
  314. {
  315. Vector3 a = 2f * p0 + -2f * p1 + v0 + v1;
  316. Vector3 a2 = -3f * p0 + 3f * p1 + -2f * v0 - v1;
  317. float num = t * t;
  318. float d = num * t;
  319. return a * d + a2 * num + v0 * t + p0;
  320. }
  321. public static Vector3 Catmul(List<Vector3> point_list, float t)
  322. {
  323. if (point_list.Count == 0)
  324. {
  325. return Vector3.zero;
  326. }
  327. if (point_list.Count == 1)
  328. {
  329. return point_list[0];
  330. }
  331. float num = (float)(point_list.Count - 1) * t;
  332. int num2 = Mathf.FloorToInt(num);
  333. float t2 = num - (float)num2;
  334. if (num2 >= point_list.Count - 1)
  335. {
  336. num2 = point_list.Count - 2;
  337. t2 = 1f;
  338. }
  339. Vector3 p = point_list[num2];
  340. Vector3 p2 = point_list[num2 + 1];
  341. Vector3 v = Vector3.zero;
  342. if (num2 > 0)
  343. {
  344. v = 0.5f * (point_list[num2 + 1] - point_list[num2 - 1]);
  345. }
  346. else
  347. {
  348. v = point_list[num2 + 1] - point_list[num2];
  349. }
  350. Vector3 v2 = Vector3.zero;
  351. if (num2 < point_list.Count - 2)
  352. {
  353. v2 = 0.5f * (point_list[num2 + 2] - point_list[num2]);
  354. }
  355. else
  356. {
  357. v2 = point_list[num2 + 1] - point_list[num2];
  358. }
  359. return KasaiUtility.Hermite(p, p2, v, v2, t2);
  360. }
  361. public static Vector3 Vec3Multiply(Vector3 a, Vector3 b)
  362. {
  363. Vector3 zero = Vector3.zero;
  364. zero.x = a.x * b.x;
  365. zero.y = a.y * b.y;
  366. zero.z = a.z * b.z;
  367. return zero;
  368. }
  369. public static Vector3 Vec3Parse(string str, char split_ch = ',')
  370. {
  371. string[] array = str.Split(new char[]
  372. {
  373. split_ch
  374. });
  375. Vector3 zero = Vector3.zero;
  376. zero.x = float.Parse(array[0]);
  377. zero.y = float.Parse(array[1]);
  378. zero.z = float.Parse(array[2]);
  379. return zero;
  380. }
  381. public static Vector3 AngleRimmit360(Vector3 orijin_angle)
  382. {
  383. Func<float, float> func = delegate(float angle)
  384. {
  385. if (Mathf.Abs(angle) > 360f)
  386. {
  387. if (angle < 0f)
  388. {
  389. angle += 360f;
  390. }
  391. else
  392. {
  393. angle -= 360f;
  394. }
  395. }
  396. return angle;
  397. };
  398. orijin_angle.x = func(orijin_angle.x);
  399. orijin_angle.y = func(orijin_angle.y);
  400. orijin_angle.z = func(orijin_angle.z);
  401. return orijin_angle;
  402. }
  403. public static void BinarySave(string file_path, string header, int version, Action<BinaryWriter> on_save)
  404. {
  405. string directoryName = Path.GetDirectoryName(file_path);
  406. if (!Directory.Exists(directoryName))
  407. {
  408. Directory.CreateDirectory(directoryName);
  409. }
  410. try
  411. {
  412. using (MemoryStream memoryStream = new MemoryStream())
  413. {
  414. using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
  415. {
  416. binaryWriter.Write(header);
  417. binaryWriter.Write(version);
  418. if (on_save != null)
  419. {
  420. on_save(binaryWriter);
  421. }
  422. }
  423. if (File.Exists(file_path))
  424. {
  425. File.Delete(file_path);
  426. }
  427. File.WriteAllBytes(file_path, memoryStream.ToArray());
  428. }
  429. }
  430. catch (Exception ex)
  431. {
  432. Debug.LogErrorFormat("BinarySaveエラー:{0}", new object[]
  433. {
  434. ex.StackTrace
  435. });
  436. }
  437. }
  438. public static void BinarySave(string file_path, Action<BinaryWriter> on_save)
  439. {
  440. string directoryName = Path.GetDirectoryName(file_path);
  441. if (!Directory.Exists(directoryName))
  442. {
  443. Directory.CreateDirectory(directoryName);
  444. }
  445. try
  446. {
  447. using (MemoryStream memoryStream = new MemoryStream())
  448. {
  449. using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
  450. {
  451. on_save(binaryWriter);
  452. }
  453. if (File.Exists(file_path))
  454. {
  455. File.Delete(file_path);
  456. }
  457. File.WriteAllBytes(file_path, memoryStream.ToArray());
  458. }
  459. }
  460. catch (Exception ex)
  461. {
  462. Debug.LogErrorFormat("BinarySaveエラー。ファイル名:{0}", new object[]
  463. {
  464. file_path
  465. });
  466. Debug.LogErrorFormat("BinarySaveエラー。エラー種別:{0}", new object[]
  467. {
  468. ex.Message
  469. });
  470. Debug.LogErrorFormat("BinarySaveエラー。エラー箇所:{0}", new object[]
  471. {
  472. ex.StackTrace
  473. });
  474. }
  475. }
  476. public static void BinaryLoad(string file_path, string header, Action<BinaryReader, int> on_load, Action on_notexist = null)
  477. {
  478. if (!File.Exists(file_path))
  479. {
  480. if (on_notexist != null)
  481. {
  482. on_notexist();
  483. }
  484. return;
  485. }
  486. try
  487. {
  488. using (FileStream fileStream = new FileStream(file_path, FileMode.Open, FileAccess.Read))
  489. {
  490. using (BinaryReader binaryReader = new BinaryReader(fileStream))
  491. {
  492. string text = binaryReader.ReadString();
  493. if (text != header)
  494. {
  495. Debug.LogErrorFormat("ヘッダーが違います。正しいヘッダー:{0} 実際のヘッダー:{1}", new object[]
  496. {
  497. header,
  498. text
  499. });
  500. }
  501. else
  502. {
  503. int arg = binaryReader.ReadInt32();
  504. if (on_load != null)
  505. {
  506. on_load(binaryReader, arg);
  507. }
  508. }
  509. }
  510. }
  511. }
  512. catch (Exception ex)
  513. {
  514. Debug.LogErrorFormat("BinaryLoadエラー:{0}", new object[]
  515. {
  516. ex.StackTrace
  517. });
  518. }
  519. }
  520. public static void BinaryLoad(string file_path, Action<BinaryReader> on_load, Action on_notexist = null)
  521. {
  522. if (!File.Exists(file_path))
  523. {
  524. if (on_notexist != null)
  525. {
  526. on_notexist();
  527. }
  528. return;
  529. }
  530. try
  531. {
  532. using (FileStream fileStream = new FileStream(file_path, FileMode.Open, FileAccess.Read))
  533. {
  534. using (BinaryReader binaryReader = new BinaryReader(fileStream))
  535. {
  536. on_load(binaryReader);
  537. }
  538. }
  539. }
  540. catch (Exception ex)
  541. {
  542. Debug.LogErrorFormat("BinaryLoadエラー。ファイル名:{0}", new object[]
  543. {
  544. file_path
  545. });
  546. Debug.LogErrorFormat("BinaryLoadエラー。エラー種別:{0}", new object[]
  547. {
  548. ex.Message
  549. });
  550. Debug.LogErrorFormat("BinaryLoadエラー。エラー箇所:{0}", new object[]
  551. {
  552. ex.StackTrace
  553. });
  554. }
  555. }
  556. public static void FileSave(string file_path, Action<StreamWriter> on_save)
  557. {
  558. string directoryName = Path.GetDirectoryName(file_path);
  559. if (!Directory.Exists(directoryName))
  560. {
  561. Directory.CreateDirectory(directoryName);
  562. }
  563. try
  564. {
  565. using (MemoryStream memoryStream = new MemoryStream())
  566. {
  567. using (StreamWriter streamWriter = new StreamWriter(memoryStream))
  568. {
  569. on_save(streamWriter);
  570. }
  571. if (File.Exists(file_path))
  572. {
  573. File.Delete(file_path);
  574. }
  575. File.WriteAllBytes(file_path, memoryStream.ToArray());
  576. }
  577. }
  578. catch (Exception ex)
  579. {
  580. Debug.LogErrorFormat("FileSaveエラー。ファイル名:{0}", new object[]
  581. {
  582. file_path
  583. });
  584. Debug.LogErrorFormat("FileSaveエラー。エラー種別:{0}", new object[]
  585. {
  586. ex.Message
  587. });
  588. Debug.LogErrorFormat("FileSaveエラー。エラー箇所:{0}", new object[]
  589. {
  590. ex.StackTrace
  591. });
  592. }
  593. }
  594. public static void FileLoad(string file_path, Action<StreamReader> on_load, Action on_notexist = null)
  595. {
  596. if (!File.Exists(file_path))
  597. {
  598. if (on_notexist != null)
  599. {
  600. on_notexist();
  601. }
  602. return;
  603. }
  604. try
  605. {
  606. using (FileStream fileStream = new FileStream(file_path, FileMode.Open, FileAccess.Read))
  607. {
  608. using (StreamReader streamReader = new StreamReader(fileStream))
  609. {
  610. on_load(streamReader);
  611. }
  612. }
  613. }
  614. catch (Exception ex)
  615. {
  616. Debug.LogErrorFormat("FileLoadエラー。ファイル名:{0}", new object[]
  617. {
  618. file_path
  619. });
  620. Debug.LogErrorFormat("FileLoadエラー。エラー種別:{0}", new object[]
  621. {
  622. ex.Message
  623. });
  624. Debug.LogErrorFormat("FileLoadエラー。エラー箇所:{0}", new object[]
  625. {
  626. ex.StackTrace
  627. });
  628. }
  629. }
  630. public static void FileLoadGameData(string file_name, Action<StreamReader> on_load, Action on_notexist = null)
  631. {
  632. if (!GameUty.FileSystem.IsExistentFile(file_name))
  633. {
  634. if (on_notexist != null)
  635. {
  636. on_notexist();
  637. }
  638. return;
  639. }
  640. try
  641. {
  642. byte[] buffer = null;
  643. using (AFileBase afileBase = GameUty.FileSystem.FileOpen(file_name))
  644. {
  645. if (afileBase.IsValid())
  646. {
  647. buffer = afileBase.ReadAll();
  648. }
  649. else
  650. {
  651. NDebug.Assert(string.Format("{0}の読み込みに失敗しました。", file_name), false);
  652. }
  653. }
  654. using (MemoryStream memoryStream = new MemoryStream(buffer))
  655. {
  656. using (StreamReader streamReader = new StreamReader(memoryStream))
  657. {
  658. on_load(streamReader);
  659. }
  660. }
  661. }
  662. catch (Exception ex)
  663. {
  664. Debug.LogErrorFormat("FileLoadGameDataエラー。ファイル名:{0}", new object[]
  665. {
  666. file_name
  667. });
  668. Debug.LogErrorFormat("FileLoadGameDataエラー。エラー種別:{0}", new object[]
  669. {
  670. ex.Message
  671. });
  672. Debug.LogErrorFormat("FileLoadGameDataエラー。エラー箇所:{0}", new object[]
  673. {
  674. ex.StackTrace
  675. });
  676. }
  677. }
  678. }