UnityUtil.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. using DG.Tweening;
  2. using ShadowStudio.Model;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. #if UNITY_EDITOR
  9. using UnityEditor;
  10. #endif
  11. using UnityEngine;
  12. using UnityEngine.Video;
  13. namespace XRTool.Util
  14. {
  15. /// <summary>
  16. /// 工具类,各种常用算法的集合脚本
  17. /// </summary>
  18. public static class UnityUtil
  19. {
  20. #if UNITY_EDITOR
  21. /// <summary>
  22. /// 尝试得到一个未命名的名称
  23. /// </summary>
  24. /// <typeparam name="T"></typeparam>
  25. /// <param name="path"></param>
  26. /// <param name="suffix"></param>
  27. /// <returns></returns>
  28. public static string TryGetName<T>(string path, string suffix = ".asset")
  29. {
  30. int index = 0;
  31. string confName = "";
  32. UnityEngine.Object obj = null;
  33. do
  34. {
  35. confName = path + "/" + typeof(T).Name + "_" + index + suffix;
  36. obj = AssetDatabase.LoadAssetAtPath(confName, typeof(T));
  37. index++;
  38. } while (obj);
  39. return confName;
  40. }
  41. #endif
  42. /// <summary>
  43. /// 获取指定名称类型的对象
  44. /// </summary>
  45. /// <typeparam name="T"></typeparam>
  46. /// <param name="childName"></param>
  47. /// <returns></returns>
  48. public static T GetChild<T>(Transform target, string childName)
  49. {
  50. var child = target.Find(childName);
  51. if (child)
  52. {
  53. return child.GetComponent<T>();
  54. }
  55. return default(T);
  56. }
  57. /// <summary>
  58. /// 深度优先搜索查找子物体
  59. /// </summary>
  60. /// <typeparam name="T"></typeparam>
  61. /// <param name="childName"></param>
  62. /// <returns></returns>
  63. public static T GetDepthChild<T>(Transform transform, string childName)
  64. {
  65. Transform target = FindDepthTransf(transform, childName);
  66. if (target)
  67. return GetT<T>(target.gameObject);
  68. return default(T);
  69. }
  70. /// <summary>
  71. /// 广度优先查找子物体
  72. /// </summary>
  73. /// <typeparam name="T"></typeparam>
  74. /// <param name="childName"></param>
  75. /// <returns></returns>
  76. public static T GetBreadthChild<T>(Transform transform, string childName)
  77. {
  78. Transform target = FindBreadthTransf(transform, childName);
  79. if (target)
  80. return GetT<T>(target.gameObject);
  81. return default(T);
  82. }
  83. public static GameObject GetBreadthChild(Transform transform, string childName)
  84. {
  85. Transform target = FindBreadthTransf(transform, childName);
  86. if (target)
  87. return target.gameObject;
  88. return null;
  89. }
  90. public static T GetParent<T>(Transform transform)
  91. {
  92. return transform.GetComponentInParent<T>();
  93. }
  94. public static T GetChild<T>(Transform trans)
  95. {
  96. return trans.GetComponentInChildren<T>();
  97. }
  98. public static T GetT<T>(GameObject target)
  99. {
  100. return target.GetComponent<T>();
  101. }
  102. /// <summary>
  103. /// 深度优先检索子物体
  104. /// </summary>
  105. /// <param name="check"></param>
  106. /// <param name="childName"></param>
  107. /// <returns></returns>
  108. public static Transform FindDepthTransf(Transform check, string childName)
  109. {
  110. if (check.name == childName) return check;
  111. for (int i = 0; i < check.childCount; i++)
  112. {
  113. Transform obj = FindDepthTransf(check.GetChild(i), childName);
  114. if (obj)
  115. return obj;
  116. }
  117. return null;
  118. }
  119. /// <summary>
  120. /// 广度优先检索子物体
  121. /// </summary>
  122. /// <param name="check"></param>
  123. /// <param name="childName"></param>
  124. /// <returns></returns>
  125. public static Transform FindBreadthTransf(Transform check, string childName)
  126. {
  127. Transform forreturn = check.Find(childName);
  128. if (forreturn)
  129. {
  130. return forreturn;
  131. }
  132. if (check.childCount > 0)
  133. {
  134. for (int i = 0; i < check.childCount; i++)
  135. {
  136. var target = FindBreadthTransf(check.GetChild(i), childName);
  137. if (target)
  138. {
  139. return target;
  140. }
  141. }
  142. }
  143. return forreturn;
  144. }
  145. public static void SetParent(Transform parent, Transform child)
  146. {
  147. child.SetParent(parent);
  148. child.localPosition = Vector3.zero;
  149. child.localRotation = Quaternion.identity;
  150. child.localScale = Vector3.one;
  151. }
  152. /// <summary>
  153.         /// 去除文件bom头后的字符
  154.         /// </summary>
  155.         /// <param name="buffer"></param>
  156.         /// <returns></returns>
  157. public static string GetUTF8String(byte[] buffer)
  158. {
  159. if (buffer == null)
  160. return null;
  161. if (buffer.Length <= 3)
  162. {
  163. return Encoding.UTF8.GetString(buffer);
  164. }
  165. byte[] bomBuffer = new byte[] { 0xef, 0xbb, 0xbf };
  166. if (buffer[0] == bomBuffer[0]
  167. && buffer[1] == bomBuffer[1]
  168. && buffer[2] == bomBuffer[2])
  169. {
  170. return new UTF8Encoding(false).GetString(buffer, 3, buffer.Length - 3);
  171. }
  172. return Encoding.UTF8.GetString(buffer);
  173. }
  174. /// <summary>
  175. /// 获取距离放歌最近的数字
  176. /// 四舍五入
  177. /// </summary>
  178. /// <param name="num"></param>
  179. /// <param name="cell"></param>
  180. /// <returns></returns>
  181. public static float GetNearst(float num, float cell)
  182. {
  183. int dir = num < 0 ? -1 : 1;
  184. return ((int)(num + cell / 2 * dir) / (int)cell) * cell;
  185. }
  186. /// <summary>
  187. /// 判断两个矩形是否相交
  188. /// 如果两个矩形中心点在x、y轴的投影距离分别小于矩形的边长之和,则此矩形相交
  189. /// </summary>
  190. /// <param name="a"></param>
  191. /// <param name="b"></param>
  192. /// <returns></returns>
  193. public static bool IsCrossLine(Rect a, Rect b)
  194. {
  195. Vector2 dis = b.center - a.center;
  196. if (((int)a.width + (int)b.width) / 2 > Math.Abs(dis.x) && ((int)a.height + (int)b.height) / 2 > Math.Abs(dis.y))
  197. {
  198. return true;
  199. }
  200. return false;
  201. }
  202. public static void ChangeMateColor(Renderer render, Color color, string name = "_Color")
  203. {
  204. if (render)
  205. {
  206. MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
  207. render.GetPropertyBlock(propertyBlock);
  208. propertyBlock.SetColor(name, color);
  209. render.SetPropertyBlock(propertyBlock);
  210. //ChangeMateValue(render, propertyBlock);
  211. propertyBlock = null;
  212. }
  213. }
  214. public static void ChangeMateColor(MaterialPropertyBlock propertyBlock, Renderer render, Color color, string name = "_Color")
  215. {
  216. if (render)
  217. {
  218. render.GetPropertyBlock(propertyBlock);
  219. propertyBlock.SetColor(name, color);
  220. render.SetPropertyBlock(propertyBlock);
  221. //ChangeMateValue(render, propertyBlock);
  222. //propertyBlock = null;
  223. }
  224. }
  225. public static void ChangeMateTexture2D(Renderer render, Texture2D img, string name = "_MainTex")
  226. {
  227. if (render)
  228. {
  229. MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
  230. render.GetPropertyBlock(propertyBlock);
  231. propertyBlock.SetTexture(name, img);
  232. render.SetPropertyBlock(propertyBlock);
  233. propertyBlock = null;
  234. }
  235. }
  236. public static void ChangeMateVideo(GameObject obj, VideoClip video, string name = "_MainTex")
  237. {
  238. if (obj)
  239. {
  240. VideoPlayer vp = obj.GetComponent<VideoPlayer>();
  241. vp.clip = video;
  242. }
  243. }
  244. public static void ChangeMateTexture(Renderer render, Texture img, string name = "_MainTex")
  245. {
  246. if (render)
  247. {
  248. MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
  249. render.GetPropertyBlock(propertyBlock);
  250. propertyBlock.SetTexture(name, img);
  251. render.SetPropertyBlock(propertyBlock);
  252. propertyBlock = null;
  253. }
  254. }
  255. public static void ChangeMateValue(Renderer render, float att, string name = "_Smoothness")
  256. {
  257. if (render)
  258. {
  259. MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
  260. render.GetPropertyBlock(propertyBlock);
  261. propertyBlock.SetFloat(name, att);
  262. render.SetPropertyBlock(propertyBlock);
  263. //ChangeMateValue(render, propertyBlock);
  264. propertyBlock = null;
  265. }
  266. }
  267. public static void ChangeMateValue(Renderer render, Vector4 att, string name = "_Smoothness")
  268. {
  269. if (render)
  270. {
  271. MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
  272. render.GetPropertyBlock(propertyBlock);
  273. propertyBlock.SetVector(name, att);
  274. render.SetPropertyBlock(propertyBlock);
  275. //ChangeMateValue(render, propertyBlock);
  276. propertyBlock = null;
  277. }
  278. }
  279. public static void ChangeMateValue(Renderer render, MaterialPropertyBlock propertyBlock)
  280. {
  281. if (render)
  282. {
  283. //MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
  284. render.GetPropertyBlock(propertyBlock);
  285. //propertyBlock.SetVector(name, att);
  286. render.SetPropertyBlock(propertyBlock);
  287. }
  288. }
  289. /// <summary>
  290. /// 材质复制
  291. /// </summary>
  292. /// <param name="resMate"></param>
  293. /// <param name="targetMate"></param>
  294. public static void CopyMate(Material resMate, Material targetMate)
  295. {
  296. if (resMate && resMate != targetMate)
  297. {
  298. resMate.shader = targetMate.shader;
  299. resMate.CopyPropertiesFromMaterial(targetMate);
  300. }
  301. }
  302. /// <summary>
  303. ///
  304. /// </summary>
  305. /// <returns></returns>
  306. public static string ArtTransferInfo(ArtInfo info)
  307. {
  308. Vector3 pos = GameSession.Instance.GetHeadForwadPos(info.Distance);
  309. if (GameNode.Instance)
  310. {
  311. pos = GameNode.Instance.transform.InverseTransformPoint(pos);
  312. }
  313. var angle = Vector3.zero;
  314. var scale = Vector3.one * info.Size;
  315. return TransferToString(pos, angle, scale, 2);
  316. }
  317. public static string ArtTransferInfo(ArtInfo info, int i, bool isperson = false)
  318. {
  319. Vector3 pos = GameSession.Instance.GetHeadForwadPos(info.Distance);
  320. if (GameNode.Instance)
  321. {
  322. pos.x = pos.x + i * 0.3f;
  323. pos = GameNode.Instance.transform.InverseTransformPoint(pos);
  324. if (isperson)
  325. {
  326. pos.z = -2;
  327. }
  328. }
  329. var angle = Vector3.zero;
  330. var scale = Vector3.one * info.Size;
  331. return TransferToString(pos, angle, scale, 2);
  332. }
  333. public static string TransferToString(Transform tranfer, int state)
  334. {
  335. return TransferToString(tranfer.localPosition, tranfer.localEulerAngles, tranfer.localScale, state);
  336. }
  337. public static int maxTransfer = 1000;
  338. public static string TransferToString(Vector3 pos, Vector3 ang, Vector3 sca, int state)
  339. {
  340. string info = "";
  341. pos *= maxTransfer;
  342. ang *= maxTransfer;
  343. sca *= maxTransfer;
  344. string position = (int)(pos.x) + "," + (int)(pos.y) + "," + (int)(pos.z);
  345. string angle = (int)(ang.x) + "," + (int)(ang.y) + "," + (int)(ang.z);
  346. string scale = (int)(sca.x) + "," + (int)(sca.y) + "," + (int)(sca.z);
  347. info = position + "|" + angle + "|" + scale;
  348. //if (state == 2)
  349. //{
  350. // info = pos + "|" + angle + "|" + scale;
  351. //}
  352. //else if (state == 1)
  353. //{
  354. // info = pos + "|" + angle;
  355. //}
  356. //else if (state == 1)
  357. //{
  358. // info = pos;
  359. //}
  360. return info;
  361. }
  362. public static Posture GetPosture(Transform info)
  363. {
  364. Posture posture = new Posture();
  365. posture.position = info.localPosition;
  366. posture.angle = info.localEulerAngles;
  367. posture.scale = info.localScale;
  368. posture.count = 2;
  369. return posture;
  370. }
  371. public static void SetPosture(Transform info, Posture posture, float time = -1)
  372. {
  373. if (time <= 0)
  374. {
  375. info.localPosition = posture.position;
  376. info.localEulerAngles = posture.angle;
  377. info.localScale = posture.scale;
  378. }
  379. else
  380. {
  381. info.DOKill();
  382. if (posture.count >= 0)
  383. {
  384. info.DOLocalMove(posture.position, time);
  385. }
  386. if (posture.count >= 1)
  387. {
  388. info.DOLocalRotate(posture.angle, time);
  389. }
  390. if (posture.count >= 2)
  391. {
  392. info.DOScale(posture.scale, time);
  393. }
  394. }
  395. }
  396. public static Posture GetPosture(string info)
  397. {
  398. Posture posture = new Posture();
  399. string[] arr = info.Split('|');
  400. for (int i = 0; i < arr.Length; i++)
  401. {
  402. if (string.IsNullOrEmpty(arr[i]))
  403. {
  404. continue;
  405. }
  406. string[] v = arr[i].Split(',');
  407. Vector3 vector = Vector3.zero;
  408. vector.x = float.Parse(v[0]);
  409. vector.y = float.Parse(v[1]);
  410. vector.z = float.Parse(v[2]);
  411. //for (int j = 0; j < v.Length; j++)
  412. //{
  413. // float value = float.Parse(v[j]);
  414. // if (j == 0)
  415. // {
  416. // vector.x = value;
  417. // }
  418. // else if (j == 1)
  419. // {
  420. // vector.y = value;
  421. // }
  422. // else if (j == 2)
  423. // {
  424. // vector.z = value;
  425. // }
  426. //}
  427. vector /= maxTransfer;
  428. if (i == 0)
  429. {
  430. posture.position = vector;
  431. }
  432. else if (i == 1)
  433. {
  434. posture.angle = vector;
  435. }
  436. else if (i == 2)
  437. {
  438. posture.scale = vector;
  439. }
  440. posture.count = i;
  441. }
  442. return posture;
  443. }
  444. //public static Posture GetPosture(string info)
  445. //{
  446. // Posture posture = new Posture();
  447. // string[] arr = info.Split('|');
  448. // for (int i = 0; i < arr.Length; i++)
  449. // {
  450. // if (string.IsNullOrEmpty(arr[i]))
  451. // {
  452. // continue;
  453. // }
  454. // string[] v = arr[i].Split(',');
  455. // Vector3 vector = Vector3.zero;
  456. // for (int j = 0; j < v.Length; j++)
  457. // {
  458. // float value = float.Parse(v[j]);
  459. // if (j == 0)
  460. // {
  461. // vector.x = value;
  462. // }
  463. // else if (j == 1)
  464. // {
  465. // vector.y = value;
  466. // }
  467. // else if (j == 2)
  468. // {
  469. // vector.z = value;
  470. // }
  471. // }
  472. // vector *= maxTransfer;
  473. // if (i == 0)
  474. // {
  475. // posture.position = vector;
  476. // }
  477. // else if (i == 1)
  478. // {
  479. // posture.angle = vector;
  480. // }
  481. // else if (i == 2)
  482. // {
  483. // posture.scale = vector;
  484. // }
  485. // posture.count = i;
  486. // }
  487. // return posture;
  488. //}
  489. public static Vector3 StringToVector3(string v)
  490. {
  491. Vector3 z = Vector3.zero;
  492. v = v.Replace("(", "").Replace(")", "");
  493. string[] s = v.Split(',');
  494. if (s.Length > 2)
  495. {
  496. z.x = float.Parse(s[0]);
  497. z.y = float.Parse(s[1]);
  498. z.z = float.Parse(s[2]);
  499. z /= maxTransfer;
  500. }
  501. return z;
  502. }
  503. public static string Vector3ToString(Vector3 v)
  504. {
  505. return v.ToString();
  506. }
  507. public static string QuaterToString(Quaternion q)
  508. {
  509. q.x *= maxTransfer;
  510. q.y *= maxTransfer;
  511. q.z *= maxTransfer;
  512. q.w *= maxTransfer;
  513. return q.ToString();
  514. }
  515. public static Quaternion StringToQuater(string v)
  516. {
  517. Quaternion q = Quaternion.identity;
  518. v = v.Replace("(", "").Replace(")", "");
  519. string[] s = v.Split(',');
  520. if (s.Length > 3)
  521. {
  522. q.x = float.Parse(s[0]) / maxTransfer;
  523. q.y = float.Parse(s[1]) / maxTransfer;
  524. q.z = float.Parse(s[2]) / maxTransfer;
  525. q.w = float.Parse(s[3]) / maxTransfer;
  526. }
  527. return q;
  528. }
  529. public static Vector3 RealForward(Transform body)
  530. {
  531. Vector3 right = body.right;
  532. right.y = 0;
  533. return Vector3.Cross(right, Vector3.up);
  534. }
  535. public static string CurTimeString
  536. {
  537. get { return DateTime.Now.ToString("MMddHHmmssf"); }
  538. }
  539. /// <summary>
  540. /// base 64 2 Texture2D
  541. /// </summary>
  542. /// <param name="base64"></param>
  543. /// <returns></returns>
  544. public static Texture Base642Tex2d(string base64)
  545. {
  546. string[] strs = base64.Split(',');
  547. byte[] bytes = Convert.FromBase64String(strs[strs.Length - 1]);
  548. Texture2D tex2D = new Texture2D(512, 512);
  549. tex2D.LoadImage(bytes);
  550. return tex2D;
  551. }
  552. }
  553. }