UIAnchor.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. [AddComponentMenu("NGUI/UI/Anchor")]
  5. public class UIAnchor : MonoBehaviour
  6. {
  7. private void Awake()
  8. {
  9. this.mTrans = base.transform;
  10. this.mAnim = base.GetComponent<Animation>();
  11. UICamera.onScreenResize = (UICamera.OnScreenResize)Delegate.Combine(UICamera.onScreenResize, new UICamera.OnScreenResize(this.ScreenSizeChanged));
  12. }
  13. private void OnDestroy()
  14. {
  15. UICamera.onScreenResize = (UICamera.OnScreenResize)Delegate.Remove(UICamera.onScreenResize, new UICamera.OnScreenResize(this.ScreenSizeChanged));
  16. }
  17. private void ScreenSizeChanged()
  18. {
  19. if (this.mStarted && this.runOnlyOnce)
  20. {
  21. this.Update();
  22. }
  23. }
  24. private void Start()
  25. {
  26. if (this.container == null && this.widgetContainer != null)
  27. {
  28. this.container = this.widgetContainer.gameObject;
  29. this.widgetContainer = null;
  30. }
  31. this.mRoot = NGUITools.FindInParents<UIRoot>(base.gameObject);
  32. if (this.uiCamera == null)
  33. {
  34. this.uiCamera = NGUITools.FindCameraForLayer(base.gameObject.layer);
  35. }
  36. this.Update();
  37. this.mStarted = true;
  38. }
  39. private void Update()
  40. {
  41. if (this.mAnim != null && this.mAnim.enabled && this.mAnim.isPlaying)
  42. {
  43. return;
  44. }
  45. bool flag = false;
  46. UIWidget uiwidget = (!(this.container == null)) ? this.container.GetComponent<UIWidget>() : null;
  47. UIPanel uipanel = (!(this.container == null) || !(uiwidget == null)) ? this.container.GetComponent<UIPanel>() : null;
  48. if (uiwidget != null)
  49. {
  50. Bounds bounds = uiwidget.CalculateBounds(this.container.transform.parent);
  51. this.mRect.x = bounds.min.x;
  52. this.mRect.y = bounds.min.y;
  53. this.mRect.width = bounds.size.x;
  54. this.mRect.height = bounds.size.y;
  55. }
  56. else if (uipanel != null)
  57. {
  58. if (uipanel.clipping == UIDrawCall.Clipping.None)
  59. {
  60. float num = (!(this.mRoot != null)) ? 0.5f : ((float)this.mRoot.activeHeight / (float)UICamera.ScreenHeight * 0.5f);
  61. if (GameMain.Instance.VRMode && Application.isPlaying)
  62. {
  63. this.mRect.xMin = -1280f * num;
  64. this.mRect.yMin = -720f * num;
  65. }
  66. else
  67. {
  68. this.mRect.xMin = (float)(-(float)UICamera.ScreenWidth) * num;
  69. this.mRect.yMin = (float)(-(float)UICamera.ScreenHeight) * num;
  70. }
  71. this.mRect.xMax = -this.mRect.xMin;
  72. this.mRect.yMax = -this.mRect.yMin;
  73. }
  74. else
  75. {
  76. Vector4 finalClipRegion = uipanel.finalClipRegion;
  77. this.mRect.x = finalClipRegion.x - finalClipRegion.z * 0.5f;
  78. this.mRect.y = finalClipRegion.y - finalClipRegion.w * 0.5f;
  79. this.mRect.width = finalClipRegion.z;
  80. this.mRect.height = finalClipRegion.w;
  81. }
  82. }
  83. else if (this.container != null)
  84. {
  85. Transform parent = this.container.transform.parent;
  86. Bounds bounds2 = (!(parent != null)) ? NGUIMath.CalculateRelativeWidgetBounds(this.container.transform) : NGUIMath.CalculateRelativeWidgetBounds(parent, this.container.transform);
  87. this.mRect.x = bounds2.min.x;
  88. this.mRect.y = bounds2.min.y;
  89. this.mRect.width = bounds2.size.x;
  90. this.mRect.height = bounds2.size.y;
  91. }
  92. else
  93. {
  94. if (!(this.uiCamera != null))
  95. {
  96. return;
  97. }
  98. flag = true;
  99. this.mRect = this.uiCamera.pixelRect;
  100. }
  101. float x = (this.mRect.xMin + this.mRect.xMax) * 0.5f;
  102. float y = (this.mRect.yMin + this.mRect.yMax) * 0.5f;
  103. Vector3 vector = new Vector3(x, y, 0f);
  104. if (this.side != UIAnchor.Side.Center)
  105. {
  106. if (this.side == UIAnchor.Side.Right || this.side == UIAnchor.Side.TopRight || this.side == UIAnchor.Side.BottomRight)
  107. {
  108. vector.x = this.mRect.xMax;
  109. }
  110. else if (this.side == UIAnchor.Side.Top || this.side == UIAnchor.Side.Center || this.side == UIAnchor.Side.Bottom)
  111. {
  112. vector.x = x;
  113. }
  114. else
  115. {
  116. vector.x = this.mRect.xMin;
  117. }
  118. if (this.side == UIAnchor.Side.Top || this.side == UIAnchor.Side.TopRight || this.side == UIAnchor.Side.TopLeft)
  119. {
  120. vector.y = this.mRect.yMax;
  121. }
  122. else if (this.side == UIAnchor.Side.Left || this.side == UIAnchor.Side.Center || this.side == UIAnchor.Side.Right)
  123. {
  124. vector.y = y;
  125. }
  126. else
  127. {
  128. vector.y = this.mRect.yMin;
  129. }
  130. }
  131. float width = this.mRect.width;
  132. float height = this.mRect.height;
  133. vector.x += this.pixelOffset.x + this.relativeOffset.x * width;
  134. vector.y += this.pixelOffset.y + this.relativeOffset.y * height;
  135. if (flag)
  136. {
  137. if (this.uiCamera.orthographic)
  138. {
  139. vector.x = Mathf.Round(vector.x);
  140. vector.y = Mathf.Round(vector.y);
  141. }
  142. vector.z = this.uiCamera.WorldToScreenPoint(this.mTrans.position).z;
  143. vector = this.uiCamera.ScreenToWorldPoint(vector);
  144. }
  145. else
  146. {
  147. vector.x = Mathf.Round(vector.x);
  148. vector.y = Mathf.Round(vector.y);
  149. if (uipanel != null)
  150. {
  151. vector = uipanel.cachedTransform.TransformPoint(vector);
  152. }
  153. else if (this.container != null)
  154. {
  155. Transform parent2 = this.container.transform.parent;
  156. if (parent2 != null)
  157. {
  158. vector = parent2.TransformPoint(vector);
  159. }
  160. }
  161. vector.z = this.mTrans.position.z;
  162. }
  163. if (this.mTrans.position != vector)
  164. {
  165. this.mTrans.position = vector;
  166. }
  167. if (this.runOnlyOnce && Application.isPlaying)
  168. {
  169. base.enabled = false;
  170. }
  171. }
  172. public Camera uiCamera;
  173. public GameObject container;
  174. public UIAnchor.Side side = UIAnchor.Side.Center;
  175. public bool runOnlyOnce = true;
  176. public Vector2 relativeOffset = Vector2.zero;
  177. public Vector2 pixelOffset = Vector2.zero;
  178. [HideInInspector]
  179. [SerializeField]
  180. private UIWidget widgetContainer;
  181. private Transform mTrans;
  182. private Animation mAnim;
  183. private Rect mRect = default(Rect);
  184. private UIRoot mRoot;
  185. private bool mStarted;
  186. public enum Side
  187. {
  188. BottomLeft,
  189. Left,
  190. TopLeft,
  191. Top,
  192. TopRight,
  193. Right,
  194. BottomRight,
  195. Bottom,
  196. Center
  197. }
  198. }