UIStretch.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. [AddComponentMenu("NGUI/UI/Stretch")]
  5. public class UIStretch : MonoBehaviour
  6. {
  7. private void Awake()
  8. {
  9. this.mAnim = base.GetComponent<Animation>();
  10. this.mRect = default(Rect);
  11. this.mTrans = base.transform;
  12. this.mWidget = base.GetComponent<UIWidget>();
  13. this.mSprite = base.GetComponent<UISprite>();
  14. this.mPanel = base.GetComponent<UIPanel>();
  15. UICamera.onScreenResize = (UICamera.OnScreenResize)Delegate.Combine(UICamera.onScreenResize, new UICamera.OnScreenResize(this.ScreenSizeChanged));
  16. }
  17. private void OnDestroy()
  18. {
  19. UICamera.onScreenResize = (UICamera.OnScreenResize)Delegate.Remove(UICamera.onScreenResize, new UICamera.OnScreenResize(this.ScreenSizeChanged));
  20. }
  21. private void ScreenSizeChanged()
  22. {
  23. if (this.mStarted && this.runOnlyOnce)
  24. {
  25. this.Update();
  26. }
  27. }
  28. private void Start()
  29. {
  30. if (this.container == null && this.widgetContainer != null)
  31. {
  32. this.container = this.widgetContainer.gameObject;
  33. this.widgetContainer = null;
  34. }
  35. if (this.uiCamera == null)
  36. {
  37. this.uiCamera = NGUITools.FindCameraForLayer(base.gameObject.layer);
  38. }
  39. this.mRoot = NGUITools.FindInParents<UIRoot>(base.gameObject);
  40. this.Update();
  41. this.mStarted = true;
  42. }
  43. private void Update()
  44. {
  45. if (this.mAnim != null && this.mAnim.isPlaying)
  46. {
  47. return;
  48. }
  49. if (this.style != UIStretch.Style.None)
  50. {
  51. UIWidget uiwidget = (!(this.container == null)) ? this.container.GetComponent<UIWidget>() : null;
  52. UIPanel uipanel = (!(this.container == null) || !(uiwidget == null)) ? this.container.GetComponent<UIPanel>() : null;
  53. float num = 1f;
  54. if (uiwidget != null)
  55. {
  56. Bounds bounds = uiwidget.CalculateBounds(base.transform.parent);
  57. this.mRect.x = bounds.min.x;
  58. this.mRect.y = bounds.min.y;
  59. this.mRect.width = bounds.size.x;
  60. this.mRect.height = bounds.size.y;
  61. }
  62. else if (uipanel != null)
  63. {
  64. if (uipanel.clipping == UIDrawCall.Clipping.None)
  65. {
  66. float num2 = (!(this.mRoot != null)) ? 0.5f : ((float)this.mRoot.activeHeight / (float)UICamera.ScreenHeight * 0.5f);
  67. this.mRect.xMin = (float)(-(float)UICamera.ScreenWidth) * num2;
  68. this.mRect.yMin = (float)(-(float)UICamera.ScreenHeight) * num2;
  69. this.mRect.xMax = -this.mRect.xMin;
  70. this.mRect.yMax = -this.mRect.yMin;
  71. }
  72. else
  73. {
  74. Vector4 finalClipRegion = uipanel.finalClipRegion;
  75. this.mRect.x = finalClipRegion.x - finalClipRegion.z * 0.5f;
  76. this.mRect.y = finalClipRegion.y - finalClipRegion.w * 0.5f;
  77. this.mRect.width = finalClipRegion.z;
  78. this.mRect.height = finalClipRegion.w;
  79. }
  80. }
  81. else if (this.container != null)
  82. {
  83. Transform parent = base.transform.parent;
  84. Bounds bounds2 = (!(parent != null)) ? NGUIMath.CalculateRelativeWidgetBounds(this.container.transform) : NGUIMath.CalculateRelativeWidgetBounds(parent, this.container.transform);
  85. this.mRect.x = bounds2.min.x;
  86. this.mRect.y = bounds2.min.y;
  87. this.mRect.width = bounds2.size.x;
  88. this.mRect.height = bounds2.size.y;
  89. }
  90. else
  91. {
  92. if (!(this.uiCamera != null))
  93. {
  94. return;
  95. }
  96. this.mRect = this.uiCamera.pixelRect;
  97. if (this.mRoot != null)
  98. {
  99. num = this.mRoot.pixelSizeAdjustment;
  100. }
  101. }
  102. float num3 = this.mRect.width;
  103. float num4 = this.mRect.height;
  104. if (num != 1f && num4 > 1f)
  105. {
  106. float num5 = (float)this.mRoot.activeHeight / num4;
  107. num3 *= num5;
  108. num4 *= num5;
  109. }
  110. Vector3 vector = (!(this.mWidget != null)) ? this.mTrans.localScale : new Vector3((float)this.mWidget.width, (float)this.mWidget.height);
  111. if (this.style == UIStretch.Style.BasedOnHeight)
  112. {
  113. vector.x = this.relativeSize.x * num4;
  114. vector.y = this.relativeSize.y * num4;
  115. }
  116. else if (this.style == UIStretch.Style.FillKeepingRatio)
  117. {
  118. float num6 = num3 / num4;
  119. float num7 = this.initialSize.x / this.initialSize.y;
  120. if (num7 < num6)
  121. {
  122. float num8 = num3 / this.initialSize.x;
  123. vector.x = num3;
  124. vector.y = this.initialSize.y * num8;
  125. }
  126. else
  127. {
  128. float num9 = num4 / this.initialSize.y;
  129. vector.x = this.initialSize.x * num9;
  130. vector.y = num4;
  131. }
  132. }
  133. else if (this.style == UIStretch.Style.FitInternalKeepingRatio)
  134. {
  135. float num10 = num3 / num4;
  136. float num11 = this.initialSize.x / this.initialSize.y;
  137. if (num11 > num10)
  138. {
  139. float num12 = num3 / this.initialSize.x;
  140. vector.x = num3;
  141. vector.y = this.initialSize.y * num12;
  142. }
  143. else
  144. {
  145. float num13 = num4 / this.initialSize.y;
  146. vector.x = this.initialSize.x * num13;
  147. vector.y = num4;
  148. }
  149. }
  150. else
  151. {
  152. if (this.style != UIStretch.Style.Vertical)
  153. {
  154. vector.x = this.relativeSize.x * num3;
  155. }
  156. if (this.style != UIStretch.Style.Horizontal)
  157. {
  158. vector.y = this.relativeSize.y * num4;
  159. }
  160. }
  161. if (this.mSprite != null)
  162. {
  163. float num14 = (!(this.mSprite.atlas != null)) ? 1f : this.mSprite.atlas.pixelSize;
  164. vector.x -= this.borderPadding.x * num14;
  165. vector.y -= this.borderPadding.y * num14;
  166. if (this.style != UIStretch.Style.Vertical)
  167. {
  168. this.mSprite.width = Mathf.RoundToInt(vector.x);
  169. }
  170. if (this.style != UIStretch.Style.Horizontal)
  171. {
  172. this.mSprite.height = Mathf.RoundToInt(vector.y);
  173. }
  174. vector = Vector3.one;
  175. }
  176. else if (this.mWidget != null)
  177. {
  178. if (this.style != UIStretch.Style.Vertical)
  179. {
  180. this.mWidget.width = Mathf.RoundToInt(vector.x - this.borderPadding.x);
  181. }
  182. if (this.style != UIStretch.Style.Horizontal)
  183. {
  184. this.mWidget.height = Mathf.RoundToInt(vector.y - this.borderPadding.y);
  185. }
  186. vector = Vector3.one;
  187. }
  188. else if (this.mPanel != null)
  189. {
  190. Vector4 baseClipRegion = this.mPanel.baseClipRegion;
  191. if (this.style != UIStretch.Style.Vertical)
  192. {
  193. baseClipRegion.z = vector.x - this.borderPadding.x;
  194. }
  195. if (this.style != UIStretch.Style.Horizontal)
  196. {
  197. baseClipRegion.w = vector.y - this.borderPadding.y;
  198. }
  199. this.mPanel.baseClipRegion = baseClipRegion;
  200. vector = Vector3.one;
  201. }
  202. else
  203. {
  204. if (this.style != UIStretch.Style.Vertical)
  205. {
  206. vector.x -= this.borderPadding.x;
  207. }
  208. if (this.style != UIStretch.Style.Horizontal)
  209. {
  210. vector.y -= this.borderPadding.y;
  211. }
  212. }
  213. if (this.mTrans.localScale != vector)
  214. {
  215. this.mTrans.localScale = vector;
  216. }
  217. if (this.runOnlyOnce && Application.isPlaying)
  218. {
  219. base.enabled = false;
  220. }
  221. }
  222. }
  223. public Camera uiCamera;
  224. public GameObject container;
  225. public UIStretch.Style style;
  226. public bool runOnlyOnce = true;
  227. public Vector2 relativeSize = Vector2.one;
  228. public Vector2 initialSize = Vector2.one;
  229. public Vector2 borderPadding = Vector2.zero;
  230. [HideInInspector]
  231. [SerializeField]
  232. private UIWidget widgetContainer;
  233. private Transform mTrans;
  234. private UIWidget mWidget;
  235. private UISprite mSprite;
  236. private UIPanel mPanel;
  237. private UIRoot mRoot;
  238. private Animation mAnim;
  239. private Rect mRect;
  240. private bool mStarted;
  241. public enum Style
  242. {
  243. None,
  244. Horizontal,
  245. Vertical,
  246. Both,
  247. BasedOnHeight,
  248. FillKeepingRatio,
  249. FitInternalKeepingRatio
  250. }
  251. }