UnityUtil.cs 19 KB

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