UIRoot.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [ExecuteInEditMode]
  5. [AddComponentMenu("NGUI/UI/Root")]
  6. public class UIRoot : MonoBehaviour
  7. {
  8. public UIRoot.Constraint constraint
  9. {
  10. get
  11. {
  12. if (this.fitWidth)
  13. {
  14. if (this.fitHeight)
  15. {
  16. return UIRoot.Constraint.Fit;
  17. }
  18. return UIRoot.Constraint.FitWidth;
  19. }
  20. else
  21. {
  22. if (this.fitHeight)
  23. {
  24. return UIRoot.Constraint.FitHeight;
  25. }
  26. return UIRoot.Constraint.Fill;
  27. }
  28. }
  29. }
  30. public UIRoot.Scaling activeScaling
  31. {
  32. get
  33. {
  34. UIRoot.Scaling scaling = this.scalingStyle;
  35. if (scaling == UIRoot.Scaling.ConstrainedOnMobiles)
  36. {
  37. return UIRoot.Scaling.Flexible;
  38. }
  39. return scaling;
  40. }
  41. }
  42. public int activeHeight
  43. {
  44. get
  45. {
  46. if (this.activeScaling == UIRoot.Scaling.Flexible)
  47. {
  48. Vector2 screenSize = NGUITools.screenSize;
  49. float num = screenSize.x / screenSize.y;
  50. if (screenSize.y < (float)this.minimumHeight)
  51. {
  52. screenSize.y = (float)this.minimumHeight;
  53. screenSize.x = screenSize.y * num;
  54. }
  55. else if (screenSize.y > (float)this.maximumHeight)
  56. {
  57. screenSize.y = (float)this.maximumHeight;
  58. screenSize.x = screenSize.y * num;
  59. }
  60. int num2 = Mathf.RoundToInt((!this.shrinkPortraitUI || screenSize.y <= screenSize.x) ? screenSize.y : (screenSize.y / num));
  61. return (!this.adjustByDPI) ? num2 : NGUIMath.AdjustByDPI((float)num2);
  62. }
  63. UIRoot.Constraint constraint = this.constraint;
  64. if (constraint == UIRoot.Constraint.FitHeight)
  65. {
  66. return this.manualHeight;
  67. }
  68. Vector2 screenSize2 = NGUITools.screenSize;
  69. float num3 = screenSize2.x / screenSize2.y;
  70. float num4 = (float)this.manualWidth / (float)this.manualHeight;
  71. if (constraint == UIRoot.Constraint.FitWidth)
  72. {
  73. return Mathf.RoundToInt((float)this.manualWidth / num3);
  74. }
  75. if (constraint == UIRoot.Constraint.Fit)
  76. {
  77. return (num4 <= num3) ? this.manualHeight : Mathf.RoundToInt((float)this.manualWidth / num3);
  78. }
  79. if (constraint != UIRoot.Constraint.Fill)
  80. {
  81. return this.manualHeight;
  82. }
  83. return (num4 >= num3) ? this.manualHeight : Mathf.RoundToInt((float)this.manualWidth / num3);
  84. }
  85. }
  86. public float pixelSizeAdjustment
  87. {
  88. get
  89. {
  90. int num = Mathf.RoundToInt(NGUITools.screenSize.y);
  91. return (num != -1) ? this.GetPixelSizeAdjustment(num) : 1f;
  92. }
  93. }
  94. public static float GetPixelSizeAdjustment(GameObject go)
  95. {
  96. UIRoot uiroot = NGUITools.FindInParents<UIRoot>(go);
  97. return (!(uiroot != null)) ? 1f : uiroot.pixelSizeAdjustment;
  98. }
  99. public float GetPixelSizeAdjustment(int height)
  100. {
  101. height = Mathf.Max(2, height);
  102. if (this.activeScaling == UIRoot.Scaling.Constrained)
  103. {
  104. return (float)this.activeHeight / (float)height;
  105. }
  106. if (height < this.minimumHeight)
  107. {
  108. return (float)this.minimumHeight / (float)height;
  109. }
  110. if (height > this.maximumHeight)
  111. {
  112. return (float)this.maximumHeight / (float)height;
  113. }
  114. return 1f;
  115. }
  116. protected virtual void Awake()
  117. {
  118. if (GameMain.Instance != null && GameMain.Instance.VRMode)
  119. {
  120. this.scalingStyle = UIRoot.Scaling.Constrained;
  121. this.minimumHeight = 1080;
  122. this.maximumHeight = 1080;
  123. this.fitWidth = false;
  124. this.fitHeight = true;
  125. }
  126. this.mTrans = base.transform;
  127. }
  128. protected virtual void OnEnable()
  129. {
  130. UIRoot.list.Add(this);
  131. }
  132. protected virtual void OnDisable()
  133. {
  134. UIRoot.list.Remove(this);
  135. }
  136. protected virtual void Start()
  137. {
  138. UIOrthoCamera componentInChildren = base.GetComponentInChildren<UIOrthoCamera>();
  139. if (componentInChildren != null)
  140. {
  141. Debug.LogWarning("UIRoot should not be active at the same time as UIOrthoCamera. Disabling UIOrthoCamera.", componentInChildren);
  142. Camera component = componentInChildren.gameObject.GetComponent<Camera>();
  143. componentInChildren.enabled = false;
  144. if (component != null)
  145. {
  146. component.orthographicSize = 1f;
  147. }
  148. }
  149. else
  150. {
  151. this.Update();
  152. }
  153. }
  154. private void Update()
  155. {
  156. if (this.mTrans != null)
  157. {
  158. float num = (float)this.activeHeight;
  159. if (num > 0f)
  160. {
  161. float num2 = 2f / num;
  162. Vector3 localScale = this.mTrans.localScale;
  163. if (Mathf.Abs(localScale.x - num2) > 1.401298E-45f || Mathf.Abs(localScale.y - num2) > 1.401298E-45f || Mathf.Abs(localScale.z - num2) > 1.401298E-45f)
  164. {
  165. this.mTrans.localScale = new Vector3(num2, num2, num2);
  166. }
  167. }
  168. }
  169. }
  170. public static void Broadcast(string funcName)
  171. {
  172. int i = 0;
  173. int count = UIRoot.list.Count;
  174. while (i < count)
  175. {
  176. UIRoot uiroot = UIRoot.list[i];
  177. if (uiroot != null)
  178. {
  179. uiroot.BroadcastMessage(funcName, SendMessageOptions.DontRequireReceiver);
  180. }
  181. i++;
  182. }
  183. }
  184. public static void Broadcast(string funcName, object param)
  185. {
  186. if (param == null)
  187. {
  188. Debug.LogError("SendMessage is bugged when you try to pass 'null' in the parameter field. It behaves as if no parameter was specified.");
  189. }
  190. else
  191. {
  192. int i = 0;
  193. int count = UIRoot.list.Count;
  194. while (i < count)
  195. {
  196. UIRoot uiroot = UIRoot.list[i];
  197. if (uiroot != null)
  198. {
  199. uiroot.BroadcastMessage(funcName, param, SendMessageOptions.DontRequireReceiver);
  200. }
  201. i++;
  202. }
  203. }
  204. }
  205. public static List<UIRoot> list = new List<UIRoot>();
  206. public UIRoot.Scaling scalingStyle;
  207. public int manualWidth = 1280;
  208. public int manualHeight = 720;
  209. public int minimumHeight = 320;
  210. public int maximumHeight = 1536;
  211. public bool fitWidth;
  212. public bool fitHeight = true;
  213. public bool adjustByDPI;
  214. public bool shrinkPortraitUI;
  215. private Transform mTrans;
  216. public enum Scaling
  217. {
  218. Flexible,
  219. Constrained,
  220. ConstrainedOnMobiles
  221. }
  222. public enum Constraint
  223. {
  224. Fit,
  225. Fill,
  226. FitWidth,
  227. FitHeight
  228. }
  229. }