UnityUtil.cs 19 KB

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