NGUITools.cs 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Runtime.CompilerServices;
  6. using UnityEngine;
  7. public static class NGUITools
  8. {
  9. public static float soundVolume
  10. {
  11. get
  12. {
  13. if (!NGUITools.mLoaded)
  14. {
  15. NGUITools.mLoaded = true;
  16. NGUITools.mGlobalVolume = PlayerPrefs.GetFloat("Sound", 1f);
  17. }
  18. return NGUITools.mGlobalVolume;
  19. }
  20. set
  21. {
  22. if (NGUITools.mGlobalVolume != value)
  23. {
  24. NGUITools.mLoaded = true;
  25. NGUITools.mGlobalVolume = value;
  26. PlayerPrefs.SetFloat("Sound", value);
  27. }
  28. }
  29. }
  30. public static bool fileAccess
  31. {
  32. get
  33. {
  34. return Application.platform != RuntimePlatform.WindowsWebPlayer && Application.platform != RuntimePlatform.OSXWebPlayer;
  35. }
  36. }
  37. public static AudioSource PlaySound(AudioClip clip)
  38. {
  39. return NGUITools.PlaySound(clip, 1f, 1f);
  40. }
  41. public static AudioSource PlaySound(AudioClip clip, float volume)
  42. {
  43. return NGUITools.PlaySound(clip, volume, 1f);
  44. }
  45. public static AudioSource PlaySound(AudioClip clip, float volume, float pitch)
  46. {
  47. volume *= NGUITools.soundVolume;
  48. if (clip != null && volume > 0.01f)
  49. {
  50. if (NGUITools.mListener == null || !NGUITools.GetActive(NGUITools.mListener))
  51. {
  52. AudioListener[] array = UnityEngine.Object.FindObjectsOfType(typeof(AudioListener)) as AudioListener[];
  53. if (array != null)
  54. {
  55. for (int i = 0; i < array.Length; i++)
  56. {
  57. if (NGUITools.GetActive(array[i]))
  58. {
  59. NGUITools.mListener = array[i];
  60. break;
  61. }
  62. }
  63. }
  64. if (NGUITools.mListener == null)
  65. {
  66. Camera camera = Camera.main;
  67. if (camera == null)
  68. {
  69. camera = (UnityEngine.Object.FindObjectOfType(typeof(Camera)) as Camera);
  70. }
  71. if (camera != null)
  72. {
  73. NGUITools.mListener = camera.gameObject.AddComponent<AudioListener>();
  74. }
  75. }
  76. }
  77. if (NGUITools.mListener != null && NGUITools.mListener.enabled && NGUITools.GetActive(NGUITools.mListener.gameObject))
  78. {
  79. AudioSource audioSource = NGUITools.mListener.GetComponent<AudioSource>();
  80. if (audioSource == null)
  81. {
  82. audioSource = NGUITools.mListener.gameObject.AddComponent<AudioSource>();
  83. }
  84. audioSource.priority = 50;
  85. audioSource.pitch = pitch;
  86. audioSource.PlayOneShot(clip, volume);
  87. return audioSource;
  88. }
  89. }
  90. return null;
  91. }
  92. public static int RandomRange(int min, int max)
  93. {
  94. if (min == max)
  95. {
  96. return min;
  97. }
  98. return UnityEngine.Random.Range(min, max + 1);
  99. }
  100. public static string GetHierarchy(GameObject obj)
  101. {
  102. if (obj == null)
  103. {
  104. return string.Empty;
  105. }
  106. string text = obj.name;
  107. while (obj.transform.parent != null)
  108. {
  109. obj = obj.transform.parent.gameObject;
  110. text = obj.name + "\\" + text;
  111. }
  112. return text;
  113. }
  114. public static T[] FindActive<T>() where T : Component
  115. {
  116. return UnityEngine.Object.FindObjectsOfType(typeof(T)) as T[];
  117. }
  118. public static Camera FindCameraForLayer(int layer)
  119. {
  120. int num = 1 << layer;
  121. Camera camera;
  122. for (int i = 0; i < UICamera.list.size; i++)
  123. {
  124. camera = UICamera.list.buffer[i].cachedCamera;
  125. if (camera && (camera.cullingMask & num) != 0)
  126. {
  127. return camera;
  128. }
  129. }
  130. camera = Camera.main;
  131. if (camera && (camera.cullingMask & num) != 0)
  132. {
  133. return camera;
  134. }
  135. Camera[] array = new Camera[Camera.allCamerasCount];
  136. int allCameras = Camera.GetAllCameras(array);
  137. for (int j = 0; j < allCameras; j++)
  138. {
  139. camera = array[j];
  140. if (camera && camera.enabled && (camera.cullingMask & num) != 0)
  141. {
  142. return camera;
  143. }
  144. }
  145. return null;
  146. }
  147. public static void AddWidgetCollider(GameObject go)
  148. {
  149. NGUITools.AddWidgetCollider(go, false);
  150. }
  151. public static void AddWidgetCollider(GameObject go, bool considerInactive)
  152. {
  153. if (go != null)
  154. {
  155. Collider component = go.GetComponent<Collider>();
  156. BoxCollider boxCollider = component as BoxCollider;
  157. if (boxCollider != null)
  158. {
  159. NGUITools.UpdateWidgetCollider(boxCollider, considerInactive);
  160. return;
  161. }
  162. if (component != null)
  163. {
  164. return;
  165. }
  166. BoxCollider2D boxCollider2D = go.GetComponent<BoxCollider2D>();
  167. if (boxCollider2D != null)
  168. {
  169. NGUITools.UpdateWidgetCollider(boxCollider2D, considerInactive);
  170. return;
  171. }
  172. UICamera uicamera = UICamera.FindCameraForLayer(go.layer);
  173. if (uicamera != null && (uicamera.eventType == UICamera.EventType.World_2D || uicamera.eventType == UICamera.EventType.UI_2D))
  174. {
  175. boxCollider2D = go.AddComponent<BoxCollider2D>();
  176. boxCollider2D.isTrigger = true;
  177. UIWidget component2 = go.GetComponent<UIWidget>();
  178. if (component2 != null)
  179. {
  180. component2.autoResizeBoxCollider = true;
  181. }
  182. NGUITools.UpdateWidgetCollider(boxCollider2D, considerInactive);
  183. return;
  184. }
  185. boxCollider = go.AddComponent<BoxCollider>();
  186. boxCollider.isTrigger = true;
  187. UIWidget component3 = go.GetComponent<UIWidget>();
  188. if (component3 != null)
  189. {
  190. component3.autoResizeBoxCollider = true;
  191. }
  192. NGUITools.UpdateWidgetCollider(boxCollider, considerInactive);
  193. }
  194. }
  195. public static void UpdateWidgetCollider(GameObject go)
  196. {
  197. NGUITools.UpdateWidgetCollider(go, false);
  198. }
  199. public static void UpdateWidgetCollider(GameObject go, bool considerInactive)
  200. {
  201. if (go != null)
  202. {
  203. BoxCollider component = go.GetComponent<BoxCollider>();
  204. if (component != null)
  205. {
  206. NGUITools.UpdateWidgetCollider(component, considerInactive);
  207. return;
  208. }
  209. BoxCollider2D component2 = go.GetComponent<BoxCollider2D>();
  210. if (component2 != null)
  211. {
  212. NGUITools.UpdateWidgetCollider(component2, considerInactive);
  213. }
  214. }
  215. }
  216. public static void UpdateWidgetCollider(BoxCollider box, bool considerInactive)
  217. {
  218. if (box != null)
  219. {
  220. GameObject gameObject = box.gameObject;
  221. UIWidget component = gameObject.GetComponent<UIWidget>();
  222. if (component != null)
  223. {
  224. Vector4 drawRegion = component.drawRegion;
  225. if (drawRegion.x != 0f || drawRegion.y != 0f || drawRegion.z != 1f || drawRegion.w != 1f)
  226. {
  227. Vector4 drawingDimensions = component.drawingDimensions;
  228. box.center = new Vector3((drawingDimensions.x + drawingDimensions.z) * 0.5f, (drawingDimensions.y + drawingDimensions.w) * 0.5f);
  229. box.size = new Vector3(drawingDimensions.z - drawingDimensions.x, drawingDimensions.w - drawingDimensions.y);
  230. }
  231. else
  232. {
  233. Vector3[] localCorners = component.localCorners;
  234. box.center = Vector3.Lerp(localCorners[0], localCorners[2], 0.5f);
  235. box.size = localCorners[2] - localCorners[0];
  236. }
  237. }
  238. else
  239. {
  240. Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(gameObject.transform, considerInactive);
  241. box.center = bounds.center;
  242. box.size = new Vector3(bounds.size.x, bounds.size.y, 0f);
  243. }
  244. }
  245. }
  246. public static void UpdateWidgetCollider(BoxCollider2D box, bool considerInactive)
  247. {
  248. if (box != null)
  249. {
  250. GameObject gameObject = box.gameObject;
  251. UIWidget component = gameObject.GetComponent<UIWidget>();
  252. if (component != null)
  253. {
  254. Vector3[] localCorners = component.localCorners;
  255. box.offset = Vector3.Lerp(localCorners[0], localCorners[2], 0.5f);
  256. box.size = localCorners[2] - localCorners[0];
  257. }
  258. else
  259. {
  260. Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(gameObject.transform, considerInactive);
  261. box.offset = bounds.center;
  262. box.size = new Vector2(bounds.size.x, bounds.size.y);
  263. }
  264. }
  265. }
  266. public static string GetTypeName<T>()
  267. {
  268. string text = typeof(T).ToString();
  269. if (text.StartsWith("UI"))
  270. {
  271. text = text.Substring(2);
  272. }
  273. else if (text.StartsWith("UnityEngine."))
  274. {
  275. text = text.Substring(12);
  276. }
  277. return text;
  278. }
  279. public static string GetTypeName(UnityEngine.Object obj)
  280. {
  281. if (obj == null)
  282. {
  283. return "Null";
  284. }
  285. string text = obj.GetType().ToString();
  286. if (text.StartsWith("UI"))
  287. {
  288. text = text.Substring(2);
  289. }
  290. else if (text.StartsWith("UnityEngine."))
  291. {
  292. text = text.Substring(12);
  293. }
  294. return text;
  295. }
  296. public static void RegisterUndo(UnityEngine.Object obj, string name)
  297. {
  298. }
  299. public static void SetDirty(UnityEngine.Object obj)
  300. {
  301. }
  302. public static GameObject AddChild(GameObject parent)
  303. {
  304. return NGUITools.AddChild(parent, true);
  305. }
  306. public static GameObject AddChild(GameObject parent, bool undo)
  307. {
  308. GameObject gameObject = new GameObject();
  309. if (parent != null)
  310. {
  311. Transform transform = gameObject.transform;
  312. transform.parent = parent.transform;
  313. transform.localPosition = Vector3.zero;
  314. transform.localRotation = Quaternion.identity;
  315. transform.localScale = Vector3.one;
  316. gameObject.layer = parent.layer;
  317. }
  318. return gameObject;
  319. }
  320. public static GameObject AddChild(GameObject parent, GameObject prefab)
  321. {
  322. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(prefab);
  323. if (gameObject != null && parent != null)
  324. {
  325. Transform transform = gameObject.transform;
  326. transform.parent = parent.transform;
  327. transform.localPosition = Vector3.zero;
  328. transform.localRotation = Quaternion.identity;
  329. transform.localScale = Vector3.one;
  330. gameObject.layer = parent.layer;
  331. }
  332. return gameObject;
  333. }
  334. public static int CalculateRaycastDepth(GameObject go)
  335. {
  336. UIWidget component = go.GetComponent<UIWidget>();
  337. if (component != null)
  338. {
  339. return component.raycastDepth;
  340. }
  341. UIWidget[] componentsInChildren = go.GetComponentsInChildren<UIWidget>();
  342. if (componentsInChildren.Length == 0)
  343. {
  344. return 0;
  345. }
  346. int num = int.MaxValue;
  347. int i = 0;
  348. int num2 = componentsInChildren.Length;
  349. while (i < num2)
  350. {
  351. if (componentsInChildren[i].enabled)
  352. {
  353. num = Mathf.Min(num, componentsInChildren[i].raycastDepth);
  354. }
  355. i++;
  356. }
  357. return num;
  358. }
  359. public static int CalculateNextDepth(GameObject go)
  360. {
  361. int num = -1;
  362. UIWidget[] componentsInChildren = go.GetComponentsInChildren<UIWidget>();
  363. int i = 0;
  364. int num2 = componentsInChildren.Length;
  365. while (i < num2)
  366. {
  367. num = Mathf.Max(num, componentsInChildren[i].depth);
  368. i++;
  369. }
  370. return num + 1;
  371. }
  372. public static int CalculateNextDepth(GameObject go, bool ignoreChildrenWithColliders)
  373. {
  374. if (ignoreChildrenWithColliders)
  375. {
  376. int num = -1;
  377. UIWidget[] componentsInChildren = go.GetComponentsInChildren<UIWidget>();
  378. int i = 0;
  379. int num2 = componentsInChildren.Length;
  380. while (i < num2)
  381. {
  382. UIWidget uiwidget = componentsInChildren[i];
  383. if (!(uiwidget.cachedGameObject != go) || (!(uiwidget.GetComponent<Collider>() != null) && !(uiwidget.GetComponent<Collider2D>() != null)))
  384. {
  385. num = Mathf.Max(num, uiwidget.depth);
  386. }
  387. i++;
  388. }
  389. return num + 1;
  390. }
  391. return NGUITools.CalculateNextDepth(go);
  392. }
  393. public static int AdjustDepth(GameObject go, int adjustment)
  394. {
  395. if (!(go != null))
  396. {
  397. return 0;
  398. }
  399. UIPanel uipanel = go.GetComponent<UIPanel>();
  400. if (uipanel != null)
  401. {
  402. foreach (UIPanel uipanel2 in go.GetComponentsInChildren<UIPanel>(true))
  403. {
  404. uipanel2.depth += adjustment;
  405. }
  406. return 1;
  407. }
  408. uipanel = NGUITools.FindInParents<UIPanel>(go);
  409. if (uipanel == null)
  410. {
  411. return 0;
  412. }
  413. UIWidget[] componentsInChildren2 = go.GetComponentsInChildren<UIWidget>(true);
  414. int j = 0;
  415. int num = componentsInChildren2.Length;
  416. while (j < num)
  417. {
  418. UIWidget uiwidget = componentsInChildren2[j];
  419. if (!(uiwidget.panel != uipanel))
  420. {
  421. uiwidget.depth += adjustment;
  422. }
  423. j++;
  424. }
  425. return 2;
  426. }
  427. public static void BringForward(GameObject go)
  428. {
  429. int num = NGUITools.AdjustDepth(go, 1000);
  430. if (num == 1)
  431. {
  432. NGUITools.NormalizePanelDepths();
  433. }
  434. else if (num == 2)
  435. {
  436. NGUITools.NormalizeWidgetDepths();
  437. }
  438. }
  439. public static void PushBack(GameObject go)
  440. {
  441. int num = NGUITools.AdjustDepth(go, -1000);
  442. if (num == 1)
  443. {
  444. NGUITools.NormalizePanelDepths();
  445. }
  446. else if (num == 2)
  447. {
  448. NGUITools.NormalizeWidgetDepths();
  449. }
  450. }
  451. public static void NormalizeDepths()
  452. {
  453. NGUITools.NormalizeWidgetDepths();
  454. NGUITools.NormalizePanelDepths();
  455. }
  456. public static void NormalizeWidgetDepths()
  457. {
  458. NGUITools.NormalizeWidgetDepths(NGUITools.FindActive<UIWidget>());
  459. }
  460. public static void NormalizeWidgetDepths(GameObject go)
  461. {
  462. NGUITools.NormalizeWidgetDepths(go.GetComponentsInChildren<UIWidget>());
  463. }
  464. public static void NormalizeWidgetDepths(UIWidget[] list)
  465. {
  466. int num = list.Length;
  467. if (num > 0)
  468. {
  469. if (NGUITools.<>f__mg$cache0 == null)
  470. {
  471. NGUITools.<>f__mg$cache0 = new Comparison<UIWidget>(UIWidget.FullCompareFunc);
  472. }
  473. Array.Sort<UIWidget>(list, NGUITools.<>f__mg$cache0);
  474. int num2 = 0;
  475. int depth = list[0].depth;
  476. for (int i = 0; i < num; i++)
  477. {
  478. UIWidget uiwidget = list[i];
  479. if (uiwidget.depth == depth)
  480. {
  481. uiwidget.depth = num2;
  482. }
  483. else
  484. {
  485. depth = uiwidget.depth;
  486. num2 = (uiwidget.depth = num2 + 1);
  487. }
  488. }
  489. }
  490. }
  491. public static void NormalizePanelDepths()
  492. {
  493. UIPanel[] array = NGUITools.FindActive<UIPanel>();
  494. int num = array.Length;
  495. if (num > 0)
  496. {
  497. UIPanel[] array2 = array;
  498. if (NGUITools.<>f__mg$cache1 == null)
  499. {
  500. NGUITools.<>f__mg$cache1 = new Comparison<UIPanel>(UIPanel.CompareFunc);
  501. }
  502. Array.Sort<UIPanel>(array2, NGUITools.<>f__mg$cache1);
  503. int num2 = 0;
  504. int depth = array[0].depth;
  505. for (int i = 0; i < num; i++)
  506. {
  507. UIPanel uipanel = array[i];
  508. if (uipanel.depth == depth)
  509. {
  510. uipanel.depth = num2;
  511. }
  512. else
  513. {
  514. depth = uipanel.depth;
  515. num2 = (uipanel.depth = num2 + 1);
  516. }
  517. }
  518. }
  519. }
  520. public static UIPanel CreateUI(bool advanced3D)
  521. {
  522. return NGUITools.CreateUI(null, advanced3D, -1);
  523. }
  524. public static UIPanel CreateUI(bool advanced3D, int layer)
  525. {
  526. return NGUITools.CreateUI(null, advanced3D, layer);
  527. }
  528. public static UIPanel CreateUI(Transform trans, bool advanced3D, int layer)
  529. {
  530. UIRoot uiroot = (!(trans != null)) ? null : NGUITools.FindInParents<UIRoot>(trans.gameObject);
  531. if (uiroot == null && UIRoot.list.Count > 0)
  532. {
  533. foreach (UIRoot uiroot2 in UIRoot.list)
  534. {
  535. if (uiroot2.gameObject.layer == layer)
  536. {
  537. uiroot = uiroot2;
  538. break;
  539. }
  540. }
  541. }
  542. if (uiroot != null)
  543. {
  544. UICamera componentInChildren = uiroot.GetComponentInChildren<UICamera>();
  545. if (componentInChildren != null && componentInChildren.GetComponent<Camera>().orthographic == advanced3D)
  546. {
  547. trans = null;
  548. uiroot = null;
  549. }
  550. }
  551. if (uiroot == null)
  552. {
  553. GameObject gameObject = NGUITools.AddChild(null, false);
  554. uiroot = gameObject.AddComponent<UIRoot>();
  555. if (layer == -1)
  556. {
  557. layer = LayerMask.NameToLayer("UI");
  558. }
  559. if (layer == -1)
  560. {
  561. layer = LayerMask.NameToLayer("2D UI");
  562. }
  563. gameObject.layer = layer;
  564. if (advanced3D)
  565. {
  566. gameObject.name = "UI Root (3D)";
  567. uiroot.scalingStyle = UIRoot.Scaling.Constrained;
  568. }
  569. else
  570. {
  571. gameObject.name = "UI Root";
  572. uiroot.scalingStyle = UIRoot.Scaling.Flexible;
  573. }
  574. }
  575. UIPanel uipanel = uiroot.GetComponentInChildren<UIPanel>();
  576. if (uipanel == null)
  577. {
  578. Camera[] array = NGUITools.FindActive<Camera>();
  579. float num = -1f;
  580. bool flag = false;
  581. int num2 = 1 << uiroot.gameObject.layer;
  582. foreach (Camera camera in array)
  583. {
  584. if (camera.clearFlags == CameraClearFlags.Color || camera.clearFlags == CameraClearFlags.Skybox)
  585. {
  586. flag = true;
  587. }
  588. num = Mathf.Max(num, camera.depth);
  589. camera.cullingMask &= ~num2;
  590. }
  591. Camera camera2 = NGUITools.AddChild<Camera>(uiroot.gameObject, false);
  592. camera2.gameObject.AddComponent<UICamera>();
  593. camera2.clearFlags = ((!flag) ? CameraClearFlags.Color : CameraClearFlags.Depth);
  594. camera2.backgroundColor = Color.grey;
  595. camera2.cullingMask = num2;
  596. camera2.depth = num + 1f;
  597. if (advanced3D)
  598. {
  599. camera2.nearClipPlane = 0.1f;
  600. camera2.farClipPlane = 4f;
  601. camera2.transform.localPosition = new Vector3(0f, 0f, -700f);
  602. }
  603. else
  604. {
  605. camera2.orthographic = true;
  606. camera2.orthographicSize = 1f;
  607. camera2.nearClipPlane = -10f;
  608. camera2.farClipPlane = 10f;
  609. }
  610. AudioListener[] array2 = NGUITools.FindActive<AudioListener>();
  611. if (array2 == null || array2.Length == 0)
  612. {
  613. camera2.gameObject.AddComponent<AudioListener>();
  614. }
  615. uipanel = uiroot.gameObject.AddComponent<UIPanel>();
  616. }
  617. if (trans != null)
  618. {
  619. while (trans.parent != null)
  620. {
  621. trans = trans.parent;
  622. }
  623. if (NGUITools.IsChild(trans, uipanel.transform))
  624. {
  625. uipanel = trans.gameObject.AddComponent<UIPanel>();
  626. }
  627. else
  628. {
  629. trans.parent = uipanel.transform;
  630. trans.localScale = Vector3.one;
  631. trans.localPosition = Vector3.zero;
  632. NGUITools.SetChildLayer(uipanel.cachedTransform, uipanel.cachedGameObject.layer);
  633. }
  634. }
  635. return uipanel;
  636. }
  637. public static void SetChildLayer(Transform t, int layer)
  638. {
  639. for (int i = 0; i < t.childCount; i++)
  640. {
  641. Transform child = t.GetChild(i);
  642. child.gameObject.layer = layer;
  643. NGUITools.SetChildLayer(child, layer);
  644. }
  645. }
  646. public static T AddChild<T>(GameObject parent) where T : Component
  647. {
  648. GameObject gameObject = NGUITools.AddChild(parent);
  649. gameObject.name = NGUITools.GetTypeName<T>();
  650. return gameObject.AddComponent<T>();
  651. }
  652. public static T AddChild<T>(GameObject parent, bool undo) where T : Component
  653. {
  654. GameObject gameObject = NGUITools.AddChild(parent, undo);
  655. gameObject.name = NGUITools.GetTypeName<T>();
  656. return gameObject.AddComponent<T>();
  657. }
  658. public static T AddWidget<T>(GameObject go) where T : UIWidget
  659. {
  660. int depth = NGUITools.CalculateNextDepth(go);
  661. T result = NGUITools.AddChild<T>(go);
  662. result.width = 100;
  663. result.height = 100;
  664. result.depth = depth;
  665. return result;
  666. }
  667. public static T AddWidget<T>(GameObject go, int depth) where T : UIWidget
  668. {
  669. T result = NGUITools.AddChild<T>(go);
  670. result.width = 100;
  671. result.height = 100;
  672. result.depth = depth;
  673. return result;
  674. }
  675. public static UISprite AddSprite(GameObject go, UIAtlas atlas, string spriteName)
  676. {
  677. UISpriteData uispriteData = (!(atlas != null)) ? null : atlas.GetSprite(spriteName);
  678. UISprite uisprite = NGUITools.AddWidget<UISprite>(go);
  679. uisprite.type = ((uispriteData != null && uispriteData.hasBorder) ? UIBasicSprite.Type.Sliced : UIBasicSprite.Type.Simple);
  680. uisprite.atlas = atlas;
  681. uisprite.spriteName = spriteName;
  682. return uisprite;
  683. }
  684. public static GameObject GetRoot(GameObject go)
  685. {
  686. Transform transform = go.transform;
  687. for (;;)
  688. {
  689. Transform parent = transform.parent;
  690. if (parent == null)
  691. {
  692. break;
  693. }
  694. transform = parent;
  695. }
  696. return transform.gameObject;
  697. }
  698. public static T FindInParents<T>(GameObject go) where T : Component
  699. {
  700. if (go == null)
  701. {
  702. return (T)((object)null);
  703. }
  704. T component = go.GetComponent<T>();
  705. if (component == null)
  706. {
  707. Transform parent = go.transform.parent;
  708. while (parent != null && component == null)
  709. {
  710. component = parent.gameObject.GetComponent<T>();
  711. parent = parent.parent;
  712. }
  713. }
  714. return component;
  715. }
  716. public static T FindInParents<T>(Transform trans) where T : Component
  717. {
  718. if (trans == null)
  719. {
  720. return (T)((object)null);
  721. }
  722. return trans.GetComponentInParent<T>();
  723. }
  724. public static void Destroy(UnityEngine.Object obj)
  725. {
  726. if (obj != null)
  727. {
  728. if (Application.isPlaying)
  729. {
  730. if (obj is GameObject)
  731. {
  732. GameObject gameObject = obj as GameObject;
  733. gameObject.transform.parent = null;
  734. }
  735. UnityEngine.Object.Destroy(obj);
  736. }
  737. else
  738. {
  739. UnityEngine.Object.DestroyImmediate(obj);
  740. }
  741. }
  742. }
  743. public static void DestroyImmediate(UnityEngine.Object obj)
  744. {
  745. if (obj != null)
  746. {
  747. if (Application.isEditor)
  748. {
  749. UnityEngine.Object.DestroyImmediate(obj);
  750. }
  751. else
  752. {
  753. UnityEngine.Object.Destroy(obj);
  754. }
  755. }
  756. }
  757. public static void Broadcast(string funcName)
  758. {
  759. GameObject[] array = UnityEngine.Object.FindObjectsOfType(typeof(GameObject)) as GameObject[];
  760. int i = 0;
  761. int num = array.Length;
  762. while (i < num)
  763. {
  764. array[i].SendMessage(funcName, SendMessageOptions.DontRequireReceiver);
  765. i++;
  766. }
  767. }
  768. public static void Broadcast(string funcName, object param)
  769. {
  770. GameObject[] array = UnityEngine.Object.FindObjectsOfType(typeof(GameObject)) as GameObject[];
  771. int i = 0;
  772. int num = array.Length;
  773. while (i < num)
  774. {
  775. array[i].SendMessage(funcName, param, SendMessageOptions.DontRequireReceiver);
  776. i++;
  777. }
  778. }
  779. public static bool IsChild(Transform parent, Transform child)
  780. {
  781. if (parent == null || child == null)
  782. {
  783. return false;
  784. }
  785. while (child != null)
  786. {
  787. if (child == parent)
  788. {
  789. return true;
  790. }
  791. child = child.parent;
  792. }
  793. return false;
  794. }
  795. private static void Activate(Transform t)
  796. {
  797. NGUITools.Activate(t, false);
  798. }
  799. private static void Activate(Transform t, bool compatibilityMode)
  800. {
  801. NGUITools.SetActiveSelf(t.gameObject, true);
  802. if (compatibilityMode)
  803. {
  804. int i = 0;
  805. int childCount = t.childCount;
  806. while (i < childCount)
  807. {
  808. Transform child = t.GetChild(i);
  809. if (child.gameObject.activeSelf)
  810. {
  811. return;
  812. }
  813. i++;
  814. }
  815. int j = 0;
  816. int childCount2 = t.childCount;
  817. while (j < childCount2)
  818. {
  819. Transform child2 = t.GetChild(j);
  820. NGUITools.Activate(child2, true);
  821. j++;
  822. }
  823. }
  824. }
  825. private static void Deactivate(Transform t)
  826. {
  827. NGUITools.SetActiveSelf(t.gameObject, false);
  828. }
  829. public static void SetActive(GameObject go, bool state)
  830. {
  831. NGUITools.SetActive(go, state, true);
  832. }
  833. public static void SetActive(GameObject go, bool state, bool compatibilityMode)
  834. {
  835. if (go)
  836. {
  837. if (state)
  838. {
  839. NGUITools.Activate(go.transform, compatibilityMode);
  840. NGUITools.CallCreatePanel(go.transform);
  841. }
  842. else
  843. {
  844. NGUITools.Deactivate(go.transform);
  845. }
  846. }
  847. }
  848. [DebuggerHidden]
  849. [DebuggerStepThrough]
  850. private static void CallCreatePanel(Transform t)
  851. {
  852. UIWidget component = t.GetComponent<UIWidget>();
  853. if (component != null)
  854. {
  855. component.CreatePanel();
  856. }
  857. int i = 0;
  858. int childCount = t.childCount;
  859. while (i < childCount)
  860. {
  861. NGUITools.CallCreatePanel(t.GetChild(i));
  862. i++;
  863. }
  864. }
  865. public static void SetActiveChildren(GameObject go, bool state)
  866. {
  867. Transform transform = go.transform;
  868. if (state)
  869. {
  870. int i = 0;
  871. int childCount = transform.childCount;
  872. while (i < childCount)
  873. {
  874. Transform child = transform.GetChild(i);
  875. NGUITools.Activate(child);
  876. i++;
  877. }
  878. }
  879. else
  880. {
  881. int j = 0;
  882. int childCount2 = transform.childCount;
  883. while (j < childCount2)
  884. {
  885. Transform child2 = transform.GetChild(j);
  886. NGUITools.Deactivate(child2);
  887. j++;
  888. }
  889. }
  890. }
  891. [Obsolete("Use NGUITools.GetActive instead")]
  892. public static bool IsActive(Behaviour mb)
  893. {
  894. return mb != null && mb.enabled && mb.gameObject.activeInHierarchy;
  895. }
  896. [DebuggerHidden]
  897. [DebuggerStepThrough]
  898. public static bool GetActive(Behaviour mb)
  899. {
  900. return mb && mb.enabled && mb.gameObject.activeInHierarchy;
  901. }
  902. [DebuggerHidden]
  903. [DebuggerStepThrough]
  904. public static bool GetActive(GameObject go)
  905. {
  906. return go && go.activeInHierarchy;
  907. }
  908. [DebuggerHidden]
  909. [DebuggerStepThrough]
  910. public static void SetActiveSelf(GameObject go, bool state)
  911. {
  912. go.SetActive(state);
  913. }
  914. public static void SetLayer(GameObject go, int layer)
  915. {
  916. go.layer = layer;
  917. Transform transform = go.transform;
  918. int i = 0;
  919. int childCount = transform.childCount;
  920. while (i < childCount)
  921. {
  922. Transform child = transform.GetChild(i);
  923. NGUITools.SetLayer(child.gameObject, layer);
  924. i++;
  925. }
  926. }
  927. public static Vector3 Round(Vector3 v)
  928. {
  929. v.x = Mathf.Round(v.x);
  930. v.y = Mathf.Round(v.y);
  931. v.z = Mathf.Round(v.z);
  932. return v;
  933. }
  934. public static void MakePixelPerfect(Transform t)
  935. {
  936. UIWidget component = t.GetComponent<UIWidget>();
  937. if (component != null)
  938. {
  939. component.MakePixelPerfect();
  940. }
  941. if (t.GetComponent<UIAnchor>() == null && t.GetComponent<UIRoot>() == null)
  942. {
  943. t.localPosition = NGUITools.Round(t.localPosition);
  944. t.localScale = NGUITools.Round(t.localScale);
  945. }
  946. int i = 0;
  947. int childCount = t.childCount;
  948. while (i < childCount)
  949. {
  950. NGUITools.MakePixelPerfect(t.GetChild(i));
  951. i++;
  952. }
  953. }
  954. public static bool Save(string fileName, byte[] bytes)
  955. {
  956. if (!NGUITools.fileAccess)
  957. {
  958. return false;
  959. }
  960. string path = Application.persistentDataPath + "/" + fileName;
  961. if (bytes == null)
  962. {
  963. if (File.Exists(path))
  964. {
  965. File.Delete(path);
  966. }
  967. return true;
  968. }
  969. FileStream fileStream = null;
  970. try
  971. {
  972. fileStream = File.Create(path);
  973. }
  974. catch (Exception ex)
  975. {
  976. UnityEngine.Debug.LogError(ex.Message);
  977. return false;
  978. }
  979. fileStream.Write(bytes, 0, bytes.Length);
  980. fileStream.Close();
  981. return true;
  982. }
  983. public static byte[] Load(string fileName)
  984. {
  985. if (!NGUITools.fileAccess)
  986. {
  987. return null;
  988. }
  989. string path = Application.persistentDataPath + "/" + fileName;
  990. if (File.Exists(path))
  991. {
  992. return File.ReadAllBytes(path);
  993. }
  994. return null;
  995. }
  996. public static Color ApplyPMA(Color c)
  997. {
  998. if (c.a != 1f)
  999. {
  1000. c.r *= c.a;
  1001. c.g *= c.a;
  1002. c.b *= c.a;
  1003. }
  1004. return c;
  1005. }
  1006. public static void MarkParentAsChanged(GameObject go)
  1007. {
  1008. UIRect[] componentsInChildren = go.GetComponentsInChildren<UIRect>();
  1009. int i = 0;
  1010. int num = componentsInChildren.Length;
  1011. while (i < num)
  1012. {
  1013. componentsInChildren[i].ParentHasChanged();
  1014. i++;
  1015. }
  1016. }
  1017. public static string clipboard
  1018. {
  1019. get
  1020. {
  1021. TextEditor textEditor = new TextEditor();
  1022. textEditor.Paste();
  1023. return textEditor.content.text;
  1024. }
  1025. set
  1026. {
  1027. TextEditor textEditor = new TextEditor();
  1028. textEditor.content = new GUIContent(value);
  1029. textEditor.OnFocus();
  1030. textEditor.Copy();
  1031. }
  1032. }
  1033. [Obsolete("Use NGUIText.EncodeColor instead")]
  1034. public static string EncodeColor(Color c)
  1035. {
  1036. return NGUIText.EncodeColor24(c);
  1037. }
  1038. [Obsolete("Use NGUIText.ParseColor instead")]
  1039. public static Color ParseColor(string text, int offset)
  1040. {
  1041. return NGUIText.ParseColor24(text, offset);
  1042. }
  1043. [Obsolete("Use NGUIText.StripSymbols instead")]
  1044. public static string StripSymbols(string text)
  1045. {
  1046. return NGUIText.StripSymbols(text);
  1047. }
  1048. public static T AddMissingComponent<T>(this GameObject go) where T : Component
  1049. {
  1050. T t = go.GetComponent<T>();
  1051. if (t == null)
  1052. {
  1053. t = go.AddComponent<T>();
  1054. }
  1055. return t;
  1056. }
  1057. public static Vector3[] GetSides(this Camera cam)
  1058. {
  1059. return cam.GetSides(Mathf.Lerp(cam.nearClipPlane, cam.farClipPlane, 0.5f), null);
  1060. }
  1061. public static Vector3[] GetSides(this Camera cam, float depth)
  1062. {
  1063. return cam.GetSides(depth, null);
  1064. }
  1065. public static Vector3[] GetSides(this Camera cam, Transform relativeTo)
  1066. {
  1067. return cam.GetSides(Mathf.Lerp(cam.nearClipPlane, cam.farClipPlane, 0.5f), relativeTo);
  1068. }
  1069. public static Vector3[] GetSides(this Camera cam, float depth, Transform relativeTo)
  1070. {
  1071. if (cam.orthographic)
  1072. {
  1073. float orthographicSize = cam.orthographicSize;
  1074. float num = -orthographicSize;
  1075. float num2 = orthographicSize;
  1076. float y = -orthographicSize;
  1077. float y2 = orthographicSize;
  1078. Rect rect = cam.rect;
  1079. Vector2 screenSize = NGUITools.screenSize;
  1080. float num3 = screenSize.x / screenSize.y;
  1081. num3 *= rect.width / rect.height;
  1082. num *= num3;
  1083. num2 *= num3;
  1084. Transform transform = cam.transform;
  1085. Quaternion rotation = transform.rotation;
  1086. Vector3 position = transform.position;
  1087. NGUITools.mSides[0] = rotation * new Vector3(num, 0f, depth) + position;
  1088. NGUITools.mSides[1] = rotation * new Vector3(0f, y2, depth) + position;
  1089. NGUITools.mSides[2] = rotation * new Vector3(num2, 0f, depth) + position;
  1090. NGUITools.mSides[3] = rotation * new Vector3(0f, y, depth) + position;
  1091. }
  1092. else
  1093. {
  1094. NGUITools.mSides[0] = cam.ViewportToWorldPoint(new Vector3(0f, 0.5f, depth));
  1095. NGUITools.mSides[1] = cam.ViewportToWorldPoint(new Vector3(0.5f, 1f, depth));
  1096. NGUITools.mSides[2] = cam.ViewportToWorldPoint(new Vector3(1f, 0.5f, depth));
  1097. NGUITools.mSides[3] = cam.ViewportToWorldPoint(new Vector3(0.5f, 0f, depth));
  1098. }
  1099. if (relativeTo != null)
  1100. {
  1101. for (int i = 0; i < 4; i++)
  1102. {
  1103. NGUITools.mSides[i] = relativeTo.InverseTransformPoint(NGUITools.mSides[i]);
  1104. }
  1105. }
  1106. return NGUITools.mSides;
  1107. }
  1108. public static Vector3[] GetWorldCorners(this Camera cam)
  1109. {
  1110. float depth = Mathf.Lerp(cam.nearClipPlane, cam.farClipPlane, 0.5f);
  1111. return cam.GetWorldCorners(depth, null);
  1112. }
  1113. public static Vector3[] GetWorldCorners(this Camera cam, float depth)
  1114. {
  1115. return cam.GetWorldCorners(depth, null);
  1116. }
  1117. public static Vector3[] GetWorldCorners(this Camera cam, Transform relativeTo)
  1118. {
  1119. return cam.GetWorldCorners(Mathf.Lerp(cam.nearClipPlane, cam.farClipPlane, 0.5f), relativeTo);
  1120. }
  1121. public static Vector3[] GetWorldCorners(this Camera cam, float depth, Transform relativeTo)
  1122. {
  1123. if (cam.orthographic)
  1124. {
  1125. float orthographicSize = cam.orthographicSize;
  1126. float num = -orthographicSize;
  1127. float num2 = orthographicSize;
  1128. float y = -orthographicSize;
  1129. float y2 = orthographicSize;
  1130. Rect rect = cam.rect;
  1131. Vector2 screenSize = NGUITools.screenSize;
  1132. float num3 = screenSize.x / screenSize.y;
  1133. num3 *= rect.width / rect.height;
  1134. num *= num3;
  1135. num2 *= num3;
  1136. Transform transform = cam.transform;
  1137. Quaternion rotation = transform.rotation;
  1138. Vector3 position = transform.position;
  1139. NGUITools.mSides[0] = rotation * new Vector3(num, y, depth) + position;
  1140. NGUITools.mSides[1] = rotation * new Vector3(num, y2, depth) + position;
  1141. NGUITools.mSides[2] = rotation * new Vector3(num2, y2, depth) + position;
  1142. NGUITools.mSides[3] = rotation * new Vector3(num2, y, depth) + position;
  1143. }
  1144. else
  1145. {
  1146. NGUITools.mSides[0] = cam.ViewportToWorldPoint(new Vector3(0f, 0f, depth));
  1147. NGUITools.mSides[1] = cam.ViewportToWorldPoint(new Vector3(0f, 1f, depth));
  1148. NGUITools.mSides[2] = cam.ViewportToWorldPoint(new Vector3(1f, 1f, depth));
  1149. NGUITools.mSides[3] = cam.ViewportToWorldPoint(new Vector3(1f, 0f, depth));
  1150. }
  1151. if (relativeTo != null)
  1152. {
  1153. for (int i = 0; i < 4; i++)
  1154. {
  1155. NGUITools.mSides[i] = relativeTo.InverseTransformPoint(NGUITools.mSides[i]);
  1156. }
  1157. }
  1158. return NGUITools.mSides;
  1159. }
  1160. public static string GetFuncName(object obj, string method)
  1161. {
  1162. if (obj == null)
  1163. {
  1164. return "<null>";
  1165. }
  1166. string text = obj.GetType().ToString();
  1167. int num = text.LastIndexOf('/');
  1168. if (num > 0)
  1169. {
  1170. text = text.Substring(num + 1);
  1171. }
  1172. return (!string.IsNullOrEmpty(method)) ? (text + "/" + method) : text;
  1173. }
  1174. public static void Execute<T>(GameObject go, string funcName) where T : Component
  1175. {
  1176. T[] components = go.GetComponents<T>();
  1177. foreach (T t in components)
  1178. {
  1179. MethodInfo method = t.GetType().GetMethod(funcName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  1180. if (method != null)
  1181. {
  1182. method.Invoke(t, null);
  1183. }
  1184. }
  1185. }
  1186. public static void ExecuteAll<T>(GameObject root, string funcName) where T : Component
  1187. {
  1188. NGUITools.Execute<T>(root, funcName);
  1189. Transform transform = root.transform;
  1190. int i = 0;
  1191. int childCount = transform.childCount;
  1192. while (i < childCount)
  1193. {
  1194. NGUITools.ExecuteAll<T>(transform.GetChild(i).gameObject, funcName);
  1195. i++;
  1196. }
  1197. }
  1198. public static void ImmediatelyCreateDrawCalls(GameObject root)
  1199. {
  1200. NGUITools.ExecuteAll<UIWidget>(root, "Start");
  1201. NGUITools.ExecuteAll<UIPanel>(root, "Start");
  1202. NGUITools.ExecuteAll<UIWidget>(root, "Update");
  1203. NGUITools.ExecuteAll<UIPanel>(root, "Update");
  1204. NGUITools.ExecuteAll<UIPanel>(root, "LateUpdate");
  1205. }
  1206. public static Vector2 screenSize
  1207. {
  1208. get
  1209. {
  1210. return new Vector2((float)Screen.width, (float)Screen.height);
  1211. }
  1212. }
  1213. private static AudioListener mListener;
  1214. private static bool mLoaded = false;
  1215. private static float mGlobalVolume = 1f;
  1216. private static Vector3[] mSides = new Vector3[4];
  1217. [CompilerGenerated]
  1218. private static Comparison<UIWidget> <>f__mg$cache0;
  1219. [CompilerGenerated]
  1220. private static Comparison<UIPanel> <>f__mg$cache1;
  1221. }