KasaiUtility.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class KasaiUtility
  6. {
  7. 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)
  8. {
  9. float timer = 0f;
  10. if (auto_setting)
  11. {
  12. if (is_fadeout)
  13. {
  14. if (!canvas.gameObject.activeSelf)
  15. {
  16. yield break;
  17. }
  18. canvas.interactable = false;
  19. }
  20. else
  21. {
  22. canvas.gameObject.SetActive(true);
  23. }
  24. }
  25. for (;;)
  26. {
  27. timer += Time.deltaTime;
  28. if (liner_valiable)
  29. {
  30. canvas.alpha = ((!is_fadeout) ? Mathf.Clamp01(timer / fade_time) : (1f - Mathf.Clamp01(timer / fade_time)));
  31. }
  32. else
  33. {
  34. canvas.alpha = KasaiUtility.SinRate01(timer, fade_time, is_fadeout, false);
  35. }
  36. if (timer > fade_time)
  37. {
  38. break;
  39. }
  40. yield return null;
  41. }
  42. if (end_action != null)
  43. {
  44. end_action();
  45. }
  46. if (auto_setting)
  47. {
  48. if (!is_fadeout)
  49. {
  50. canvas.interactable = true;
  51. }
  52. else
  53. {
  54. canvas.gameObject.SetActive(false);
  55. }
  56. }
  57. yield break;
  58. yield break;
  59. }
  60. 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)
  61. {
  62. float timer = 0f;
  63. if (auto_setting)
  64. {
  65. if (is_fadeout)
  66. {
  67. if (!ui_panel.gameObject.activeSelf)
  68. {
  69. yield break;
  70. }
  71. }
  72. else
  73. {
  74. ui_panel.gameObject.SetActive(true);
  75. }
  76. }
  77. for (;;)
  78. {
  79. timer += Time.deltaTime;
  80. if (liner_valiable)
  81. {
  82. ui_panel.alpha = ((!is_fadeout) ? Mathf.Clamp01(timer / fade_time) : (1f - Mathf.Clamp01(timer / fade_time)));
  83. }
  84. else
  85. {
  86. ui_panel.alpha = KasaiUtility.SinRate01(timer, fade_time, is_fadeout, false);
  87. }
  88. if (timer > fade_time)
  89. {
  90. break;
  91. }
  92. yield return null;
  93. }
  94. if (end_action != null)
  95. {
  96. end_action();
  97. }
  98. if (auto_setting && is_fadeout)
  99. {
  100. ui_panel.gameObject.SetActive(false);
  101. }
  102. yield break;
  103. yield break;
  104. }
  105. public static IEnumerator TimeCroutine(float time_rimmit, Action<float> update_call, Action end_call = null)
  106. {
  107. float timer = 0f;
  108. for (;;)
  109. {
  110. timer += Time.deltaTime;
  111. if (update_call != null)
  112. {
  113. update_call(timer);
  114. }
  115. if (timer > time_rimmit)
  116. {
  117. break;
  118. }
  119. yield return null;
  120. }
  121. if (end_call != null)
  122. {
  123. end_call();
  124. }
  125. yield break;
  126. yield break;
  127. }
  128. public static IEnumerator CharaLoadWait(Action end_call)
  129. {
  130. while (GameMain.Instance.CharacterMgr.IsBusy())
  131. {
  132. yield return null;
  133. }
  134. yield return null;
  135. if (end_call != null)
  136. {
  137. end_call();
  138. }
  139. yield break;
  140. }
  141. public static float SinRate01(float current_time, float total_time, bool is_reverse = false, bool allow_timeover = false)
  142. {
  143. float rate = (!allow_timeover) ? Mathf.Clamp01(current_time / total_time) : (current_time / total_time);
  144. return KasaiUtility.SinRate01(rate, is_reverse);
  145. }
  146. public static float SinRate01(float rate, bool is_reverse = false)
  147. {
  148. if (!is_reverse)
  149. {
  150. return Mathf.Sin(rate * 90f * 0.0174532924f);
  151. }
  152. return 1f - Mathf.Sin(rate * 90f * 0.0174532924f);
  153. }
  154. public static float CosRate11(float current_time, float total_time, bool is_abs = false, bool is_reverse = false, bool allow_timeover = false)
  155. {
  156. float num = (!allow_timeover) ? Mathf.Clamp01(current_time / total_time) : (current_time / total_time);
  157. if (is_abs)
  158. {
  159. if (is_reverse)
  160. {
  161. return 1f - Mathf.Abs(Mathf.Cos(num * 180f * 0.0174532924f));
  162. }
  163. return Mathf.Abs(Mathf.Cos(num * 180f * 0.0174532924f));
  164. }
  165. else
  166. {
  167. if (is_reverse)
  168. {
  169. return -Mathf.Cos(num * 180f * 0.0174532924f);
  170. }
  171. return Mathf.Cos(num * 180f * 0.0174532924f);
  172. }
  173. }
  174. public static float DistanceIgnoreY(Vector3 a, Vector3 b)
  175. {
  176. a.y = (b.y = 0f);
  177. return Vector3.Distance(a, b);
  178. }
  179. public static Vector2 AngleDirection(float angle)
  180. {
  181. Vector2 zero = Vector2.zero;
  182. zero.x = Mathf.Cos(angle * 0.0174532924f);
  183. zero.y = Mathf.Sin(angle * 0.0174532924f);
  184. return zero;
  185. }
  186. 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)
  187. {
  188. if (file_name.IndexOf(".nei") < 0)
  189. {
  190. file_name += ".nei";
  191. }
  192. if (!GameUty.FileSystem.IsExistentFile(file_name))
  193. {
  194. if (not_exist_call != null)
  195. {
  196. not_exist_call();
  197. return;
  198. }
  199. NDebug.Assert("表がありません。" + file_name, false);
  200. }
  201. using (AFileBase afileBase = GameUty.FileSystem.FileOpen(file_name))
  202. {
  203. using (CsvParser csvParser = new CsvParser())
  204. {
  205. bool condition = csvParser.Open(afileBase);
  206. NDebug.Assert(condition, file_name + "\nopen failed.");
  207. for (int i = first_y; i < csvParser.max_cell_y; i++)
  208. {
  209. if (csvParser.IsCellToExistData(0, i))
  210. {
  211. for (int j = first_x; j < csvParser.max_cell_x; j++)
  212. {
  213. if (csvParser.IsCellToExistData(j, i))
  214. {
  215. if (read_callback != null)
  216. {
  217. read_callback(csvParser, j, i);
  218. }
  219. }
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }
  226. public static void CsvReadY(string file_name, Action<CsvParser, int> read_callback, int first_y = 1, Action not_exist_call = null)
  227. {
  228. if (file_name.IndexOf(".nei") < 0)
  229. {
  230. file_name += ".nei";
  231. }
  232. if (!GameUty.FileSystem.IsExistentFile(file_name))
  233. {
  234. if (not_exist_call != null)
  235. {
  236. not_exist_call();
  237. return;
  238. }
  239. NDebug.Assert("表がありません。" + file_name, false);
  240. }
  241. using (AFileBase afileBase = GameUty.FileSystem.FileOpen(file_name))
  242. {
  243. using (CsvParser csvParser = new CsvParser())
  244. {
  245. bool condition = csvParser.Open(afileBase);
  246. NDebug.Assert(condition, file_name + "\nopen failed.");
  247. for (int i = first_y; i < csvParser.max_cell_y; i++)
  248. {
  249. if (csvParser.IsCellToExistData(0, i))
  250. {
  251. if (read_callback != null)
  252. {
  253. read_callback(csvParser, i);
  254. }
  255. }
  256. }
  257. }
  258. }
  259. }
  260. public static void CsvReadAll(string file_name, Action<CsvParser> read_callback, Action not_exist_call = null)
  261. {
  262. if (file_name.IndexOf(".nei") < 0)
  263. {
  264. file_name += ".nei";
  265. }
  266. if (!GameUty.FileSystem.IsExistentFile(file_name))
  267. {
  268. if (not_exist_call != null)
  269. {
  270. not_exist_call();
  271. return;
  272. }
  273. NDebug.Assert("表がありません。" + file_name, false);
  274. }
  275. using (AFileBase afileBase = GameUty.FileSystem.FileOpen(file_name))
  276. {
  277. using (CsvParser csvParser = new CsvParser())
  278. {
  279. bool condition = csvParser.Open(afileBase);
  280. NDebug.Assert(condition, file_name + "\nopen failed.");
  281. if (read_callback != null)
  282. {
  283. read_callback(csvParser);
  284. }
  285. }
  286. }
  287. }
  288. public static void DrawAxis(Vector3 pos, Quaternion rot, Color x_color, Color y_color, Color z_color, float line_length = 0.0625f)
  289. {
  290. Debug.DrawLine(pos, pos + rot * Vector3.right * line_length, x_color);
  291. Debug.DrawLine(pos, pos + rot * Vector3.up * line_length, y_color);
  292. Debug.DrawLine(pos, pos + rot * Vector3.forward * line_length, z_color);
  293. }
  294. public static void DrawAxis(Vector3 pos, Quaternion rot, float line_length = 0.0625f)
  295. {
  296. KasaiUtility.DrawAxis(pos, rot, Color.red, Color.green, Color.blue, line_length);
  297. }
  298. public static void DrawObjAxis(Transform obj_trans, Color x_color, Color y_color, Color z_color, float line_length = 0.0625f)
  299. {
  300. KasaiUtility.DrawAxis(obj_trans.position, obj_trans.rotation, x_color, y_color, z_color, line_length);
  301. }
  302. public static void DrawObjAxis(Transform obj_trans, float ray_length = 0.0625f)
  303. {
  304. KasaiUtility.DrawObjAxis(obj_trans, Color.red, Color.green, Color.blue, ray_length);
  305. }
  306. public static Vector3 Bezier(Vector3 p0, Vector3 p1, Vector3 c, float t)
  307. {
  308. Vector3 a = Vector3.Lerp(p0, c, t);
  309. Vector3 b = Vector3.Lerp(c, p1, t);
  310. return Vector3.Lerp(a, b, t);
  311. }
  312. public static Vector3 Hermite(Vector3 p0, Vector3 p1, Vector3 v0, Vector3 v1, float t)
  313. {
  314. Vector3 a = 2f * p0 + -2f * p1 + v0 + v1;
  315. Vector3 a2 = -3f * p0 + 3f * p1 + -2f * v0 - v1;
  316. float num = t * t;
  317. float d = num * t;
  318. return a * d + a2 * num + v0 * t + p0;
  319. }
  320. public static Vector3 Catmul(List<Vector3> point_list, float t)
  321. {
  322. if (point_list.Count == 0)
  323. {
  324. return Vector3.zero;
  325. }
  326. if (point_list.Count == 1)
  327. {
  328. return point_list[0];
  329. }
  330. float num = (float)(point_list.Count - 1) * t;
  331. int num2 = Mathf.FloorToInt(num);
  332. float t2 = num - (float)num2;
  333. if (num2 >= point_list.Count - 1)
  334. {
  335. num2 = point_list.Count - 2;
  336. t2 = 1f;
  337. }
  338. Vector3 p = point_list[num2];
  339. Vector3 p2 = point_list[num2 + 1];
  340. Vector3 v = Vector3.zero;
  341. if (num2 > 0)
  342. {
  343. v = 0.5f * (point_list[num2 + 1] - point_list[num2 - 1]);
  344. }
  345. else
  346. {
  347. v = point_list[num2 + 1] - point_list[num2];
  348. }
  349. Vector3 v2 = Vector3.zero;
  350. if (num2 < point_list.Count - 2)
  351. {
  352. v2 = 0.5f * (point_list[num2 + 2] - point_list[num2]);
  353. }
  354. else
  355. {
  356. v2 = point_list[num2 + 1] - point_list[num2];
  357. }
  358. return KasaiUtility.Hermite(p, p2, v, v2, t2);
  359. }
  360. public static Vector3 Vec3Multiply(Vector3 a, Vector3 b)
  361. {
  362. Vector3 zero = Vector3.zero;
  363. zero.x = a.x * b.x;
  364. zero.y = a.y * b.y;
  365. zero.z = a.z * b.z;
  366. return zero;
  367. }
  368. public static Vector3 Vec3Parse(string str, char split_ch = ',')
  369. {
  370. string[] array = str.Split(new char[]
  371. {
  372. split_ch
  373. });
  374. Vector3 zero = Vector3.zero;
  375. zero.x = float.Parse(array[0]);
  376. zero.y = float.Parse(array[1]);
  377. zero.z = float.Parse(array[2]);
  378. return zero;
  379. }
  380. public static Vector3 AngleRimmit360(Vector3 orijin_angle)
  381. {
  382. Func<float, float> func = delegate(float angle)
  383. {
  384. if (Mathf.Abs(angle) > 360f)
  385. {
  386. if (angle < 0f)
  387. {
  388. angle += 360f;
  389. }
  390. else
  391. {
  392. angle -= 360f;
  393. }
  394. }
  395. return angle;
  396. };
  397. orijin_angle.x = func(orijin_angle.x);
  398. orijin_angle.y = func(orijin_angle.y);
  399. orijin_angle.z = func(orijin_angle.z);
  400. return orijin_angle;
  401. }
  402. }