uGUIScrollRect.cs 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. using System;
  2. using UnityEngine.Events;
  3. using UnityEngine.EventSystems;
  4. namespace UnityEngine.UI
  5. {
  6. [SelectionBase]
  7. [ExecuteInEditMode]
  8. [DisallowMultipleComponent]
  9. [RequireComponent(typeof(RectTransform))]
  10. public class uGUIScrollRect : UIBehaviour, IInitializePotentialDragHandler, IBeginDragHandler, IEndDragHandler, IDragHandler, IScrollHandler, ICanvasElement, ILayoutElement, ILayoutGroup, IEventSystemHandler, ILayoutController
  11. {
  12. protected uGUIScrollRect()
  13. {
  14. }
  15. public RectTransform content
  16. {
  17. get
  18. {
  19. return this.m_Content;
  20. }
  21. set
  22. {
  23. this.m_Content = value;
  24. }
  25. }
  26. public bool horizontal
  27. {
  28. get
  29. {
  30. return this.m_Horizontal;
  31. }
  32. set
  33. {
  34. this.m_Horizontal = value;
  35. }
  36. }
  37. public bool vertical
  38. {
  39. get
  40. {
  41. return this.m_Vertical;
  42. }
  43. set
  44. {
  45. this.m_Vertical = value;
  46. }
  47. }
  48. public uGUIScrollRect.MovementType movementType
  49. {
  50. get
  51. {
  52. return this.m_MovementType;
  53. }
  54. set
  55. {
  56. this.m_MovementType = value;
  57. }
  58. }
  59. public float elasticity
  60. {
  61. get
  62. {
  63. return this.m_Elasticity;
  64. }
  65. set
  66. {
  67. this.m_Elasticity = value;
  68. }
  69. }
  70. public bool inertia
  71. {
  72. get
  73. {
  74. return this.m_Inertia;
  75. }
  76. set
  77. {
  78. this.m_Inertia = value;
  79. }
  80. }
  81. public float decelerationRate
  82. {
  83. get
  84. {
  85. return this.m_DecelerationRate;
  86. }
  87. set
  88. {
  89. this.m_DecelerationRate = value;
  90. }
  91. }
  92. public float scrollSensitivity
  93. {
  94. get
  95. {
  96. return this.m_ScrollSensitivity;
  97. }
  98. set
  99. {
  100. this.m_ScrollSensitivity = value;
  101. }
  102. }
  103. public RectTransform viewport
  104. {
  105. get
  106. {
  107. return this.m_Viewport;
  108. }
  109. set
  110. {
  111. this.m_Viewport = value;
  112. this.SetDirtyCaching();
  113. }
  114. }
  115. public Slider horizontalSlider
  116. {
  117. get
  118. {
  119. return this.m_HorizontalSlider;
  120. }
  121. set
  122. {
  123. if (this.m_HorizontalSlider)
  124. {
  125. this.m_HorizontalSlider.onValueChanged.RemoveListener(new UnityAction<float>(this.SetHorizontalNormalizedPosition));
  126. }
  127. this.m_HorizontalSlider = value;
  128. if (this.m_HorizontalSlider)
  129. {
  130. this.m_HorizontalSlider.onValueChanged.AddListener(new UnityAction<float>(this.SetHorizontalNormalizedPosition));
  131. }
  132. this.SetDirtyCaching();
  133. }
  134. }
  135. public Slider verticalSlider
  136. {
  137. get
  138. {
  139. return this.m_VerticalSlider;
  140. }
  141. set
  142. {
  143. if (this.m_VerticalSlider)
  144. {
  145. this.m_VerticalSlider.onValueChanged.RemoveListener(new UnityAction<float>(this.SetVerticalNormalizedPosition));
  146. }
  147. this.m_VerticalSlider = value;
  148. if (this.m_VerticalSlider)
  149. {
  150. this.m_VerticalSlider.onValueChanged.AddListener(new UnityAction<float>(this.SetVerticalNormalizedPosition));
  151. }
  152. this.SetDirtyCaching();
  153. }
  154. }
  155. public uGUIScrollRect.SliderVisibility horizontalSliderVisibility
  156. {
  157. get
  158. {
  159. return this.m_HorizontalSliderVisibility;
  160. }
  161. set
  162. {
  163. this.m_HorizontalSliderVisibility = value;
  164. this.SetDirtyCaching();
  165. }
  166. }
  167. public uGUIScrollRect.SliderVisibility verticalSliderVisibility
  168. {
  169. get
  170. {
  171. return this.m_VerticalSliderVisibility;
  172. }
  173. set
  174. {
  175. this.m_VerticalSliderVisibility = value;
  176. this.SetDirtyCaching();
  177. }
  178. }
  179. public float horizontalSliderSpacing
  180. {
  181. get
  182. {
  183. return this.m_HorizontalSliderSpacing;
  184. }
  185. set
  186. {
  187. this.m_HorizontalSliderSpacing = value;
  188. this.SetDirty();
  189. }
  190. }
  191. public float verticalSliderSpacing
  192. {
  193. get
  194. {
  195. return this.m_VerticalSliderSpacing;
  196. }
  197. set
  198. {
  199. this.m_VerticalSliderSpacing = value;
  200. this.SetDirty();
  201. }
  202. }
  203. public uGUIScrollRect.ScrollRectEvent onValueChanged
  204. {
  205. get
  206. {
  207. return this.m_OnValueChanged;
  208. }
  209. set
  210. {
  211. this.m_OnValueChanged = value;
  212. }
  213. }
  214. protected RectTransform viewRect
  215. {
  216. get
  217. {
  218. if (this.m_ViewRect == null)
  219. {
  220. this.m_ViewRect = this.m_Viewport;
  221. }
  222. if (this.m_ViewRect == null)
  223. {
  224. this.m_ViewRect = (RectTransform)base.transform;
  225. }
  226. return this.m_ViewRect;
  227. }
  228. }
  229. public Vector2 velocity
  230. {
  231. get
  232. {
  233. return this.m_Velocity;
  234. }
  235. set
  236. {
  237. this.m_Velocity = value;
  238. }
  239. }
  240. private RectTransform rectTransform
  241. {
  242. get
  243. {
  244. if (this.m_Rect == null)
  245. {
  246. this.m_Rect = base.GetComponent<RectTransform>();
  247. }
  248. return this.m_Rect;
  249. }
  250. }
  251. public virtual void Rebuild(CanvasUpdate executing)
  252. {
  253. if (executing == CanvasUpdate.Prelayout)
  254. {
  255. this.UpdateCachedData();
  256. }
  257. if (executing == CanvasUpdate.PostLayout)
  258. {
  259. this.UpdateBounds();
  260. this.UpdateSliders(Vector2.zero);
  261. this.UpdatePrevData();
  262. this.m_HasRebuiltLayout = true;
  263. }
  264. }
  265. public virtual void LayoutComplete()
  266. {
  267. }
  268. public virtual void GraphicUpdateComplete()
  269. {
  270. }
  271. private void UpdateCachedData()
  272. {
  273. Transform transform = base.transform;
  274. this.m_HorizontalSliderRect = ((!(this.m_HorizontalSlider == null)) ? (this.m_HorizontalSlider.transform as RectTransform) : null);
  275. this.m_VerticalSliderRect = ((!(this.m_VerticalSlider == null)) ? (this.m_VerticalSlider.transform as RectTransform) : null);
  276. bool flag = this.viewRect.parent == transform;
  277. bool flag2 = !this.m_HorizontalSliderRect || this.m_HorizontalSliderRect.parent == transform;
  278. bool flag3 = !this.m_VerticalSliderRect || this.m_VerticalSliderRect.parent == transform;
  279. bool flag4 = flag && flag2 && flag3;
  280. this.m_HSliderExpand = (flag4 && this.m_HorizontalSliderRect && this.horizontalSliderVisibility == uGUIScrollRect.SliderVisibility.AutoHideAndExpandViewport);
  281. this.m_VSliderExpand = (flag4 && this.m_VerticalSliderRect && this.verticalSliderVisibility == uGUIScrollRect.SliderVisibility.AutoHideAndExpandViewport);
  282. this.m_HSliderHeight = ((!(this.m_HorizontalSliderRect == null)) ? this.m_HorizontalSliderRect.rect.height : 0f);
  283. this.m_VSliderWidth = ((!(this.m_VerticalSliderRect == null)) ? this.m_VerticalSliderRect.rect.width : 0f);
  284. }
  285. protected override void OnEnable()
  286. {
  287. base.OnEnable();
  288. if (this.m_HorizontalSlider)
  289. {
  290. this.m_HorizontalSlider.onValueChanged.AddListener(new UnityAction<float>(this.SetHorizontalNormalizedPosition));
  291. }
  292. if (this.m_VerticalSlider)
  293. {
  294. this.m_VerticalSlider.onValueChanged.AddListener(new UnityAction<float>(this.SetVerticalNormalizedPosition));
  295. }
  296. CanvasUpdateRegistry.RegisterCanvasElementForLayoutRebuild(this);
  297. }
  298. protected override void OnDisable()
  299. {
  300. CanvasUpdateRegistry.UnRegisterCanvasElementForRebuild(this);
  301. if (this.m_HorizontalSlider)
  302. {
  303. this.m_HorizontalSlider.onValueChanged.RemoveListener(new UnityAction<float>(this.SetHorizontalNormalizedPosition));
  304. }
  305. if (this.m_VerticalSlider)
  306. {
  307. this.m_VerticalSlider.onValueChanged.RemoveListener(new UnityAction<float>(this.SetVerticalNormalizedPosition));
  308. }
  309. this.m_HasRebuiltLayout = false;
  310. this.m_Tracker.Clear();
  311. this.m_Velocity = Vector2.zero;
  312. LayoutRebuilder.MarkLayoutForRebuild(this.rectTransform);
  313. base.OnDisable();
  314. }
  315. public override bool IsActive()
  316. {
  317. return base.IsActive() && this.m_Content != null;
  318. }
  319. private void EnsureLayoutHasRebuilt()
  320. {
  321. if (!this.m_HasRebuiltLayout && !CanvasUpdateRegistry.IsRebuildingLayout())
  322. {
  323. Canvas.ForceUpdateCanvases();
  324. }
  325. }
  326. public virtual void StopMovement()
  327. {
  328. this.m_Velocity = Vector2.zero;
  329. }
  330. public virtual void OnScroll(PointerEventData data)
  331. {
  332. if (!this.IsActive())
  333. {
  334. return;
  335. }
  336. this.EnsureLayoutHasRebuilt();
  337. this.UpdateBounds();
  338. Vector2 scrollDelta = data.scrollDelta;
  339. scrollDelta.y *= -1f;
  340. if (this.vertical && !this.horizontal)
  341. {
  342. if (Mathf.Abs(scrollDelta.x) > Mathf.Abs(scrollDelta.y))
  343. {
  344. scrollDelta.y = scrollDelta.x;
  345. }
  346. scrollDelta.x = 0f;
  347. }
  348. if (this.horizontal && !this.vertical)
  349. {
  350. if (Mathf.Abs(scrollDelta.y) > Mathf.Abs(scrollDelta.x))
  351. {
  352. scrollDelta.x = scrollDelta.y;
  353. }
  354. scrollDelta.y = 0f;
  355. }
  356. Vector2 vector = this.m_Content.anchoredPosition;
  357. this.m_Velocity += scrollDelta * this.m_ScrollSensitivity;
  358. if (this.m_MovementType == uGUIScrollRect.MovementType.Clamped)
  359. {
  360. vector += this.CalculateOffset(vector - this.m_Content.anchoredPosition);
  361. }
  362. this.SetContentAnchoredPosition(vector);
  363. this.UpdateBounds();
  364. }
  365. public virtual void OnInitializePotentialDrag(PointerEventData eventData)
  366. {
  367. if (eventData.button != PointerEventData.InputButton.Left)
  368. {
  369. return;
  370. }
  371. this.m_Velocity = Vector2.zero;
  372. }
  373. public virtual void OnBeginDrag(PointerEventData eventData)
  374. {
  375. if (eventData.button != PointerEventData.InputButton.Left)
  376. {
  377. return;
  378. }
  379. if (!this.IsActive())
  380. {
  381. return;
  382. }
  383. this.UpdateBounds();
  384. this.m_PointerStartLocalCursor = Vector2.zero;
  385. RectTransformUtility.ScreenPointToLocalPointInRectangle(this.viewRect, eventData.position, eventData.pressEventCamera, out this.m_PointerStartLocalCursor);
  386. this.m_ContentStartPosition = this.m_Content.anchoredPosition;
  387. this.m_Dragging = true;
  388. }
  389. public virtual void OnEndDrag(PointerEventData eventData)
  390. {
  391. if (eventData.button != PointerEventData.InputButton.Left)
  392. {
  393. return;
  394. }
  395. this.m_Dragging = false;
  396. }
  397. public virtual void OnDrag(PointerEventData eventData)
  398. {
  399. if (eventData.button != PointerEventData.InputButton.Left)
  400. {
  401. return;
  402. }
  403. if (!this.IsActive())
  404. {
  405. return;
  406. }
  407. Vector2 a;
  408. if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(this.viewRect, eventData.position, eventData.pressEventCamera, out a))
  409. {
  410. return;
  411. }
  412. this.UpdateBounds();
  413. Vector2 b = a - this.m_PointerStartLocalCursor;
  414. Vector2 vector = this.m_ContentStartPosition + b;
  415. Vector2 b2 = this.CalculateOffset(vector - this.m_Content.anchoredPosition);
  416. vector += b2;
  417. if (this.m_MovementType == uGUIScrollRect.MovementType.Elastic)
  418. {
  419. if (b2.x != 0f)
  420. {
  421. vector.x -= uGUIScrollRect.RubberDelta(b2.x, this.m_ViewBounds.size.x);
  422. }
  423. if (b2.y != 0f)
  424. {
  425. vector.y -= uGUIScrollRect.RubberDelta(b2.y, this.m_ViewBounds.size.y);
  426. }
  427. }
  428. this.SetContentAnchoredPosition(vector);
  429. }
  430. protected virtual void SetContentAnchoredPosition(Vector2 position)
  431. {
  432. if (!this.m_Horizontal)
  433. {
  434. position.x = this.m_Content.anchoredPosition.x;
  435. }
  436. if (!this.m_Vertical)
  437. {
  438. position.y = this.m_Content.anchoredPosition.y;
  439. }
  440. if (position != this.m_Content.anchoredPosition)
  441. {
  442. this.m_Content.anchoredPosition = position;
  443. this.UpdateBounds();
  444. }
  445. }
  446. protected virtual void LateUpdate()
  447. {
  448. if (!this.m_Content)
  449. {
  450. return;
  451. }
  452. this.EnsureLayoutHasRebuilt();
  453. this.UpdateSliderVisibility();
  454. this.UpdateBounds();
  455. float unscaledDeltaTime = Time.unscaledDeltaTime;
  456. Vector2 vector = this.CalculateOffset(Vector2.zero);
  457. if (!this.m_Dragging && (vector != Vector2.zero || this.m_Velocity != Vector2.zero))
  458. {
  459. Vector2 vector2 = this.m_Content.anchoredPosition;
  460. for (int i = 0; i < 2; i++)
  461. {
  462. if (this.m_MovementType == uGUIScrollRect.MovementType.Elastic && vector[i] != 0f)
  463. {
  464. float num = this.m_Velocity[i];
  465. vector2[i] = Mathf.SmoothDamp(this.m_Content.anchoredPosition[i], this.m_Content.anchoredPosition[i] + vector[i], ref num, this.m_Elasticity, float.PositiveInfinity, unscaledDeltaTime);
  466. if (Mathf.Abs(num) < 1f)
  467. {
  468. num = 0f;
  469. }
  470. this.m_Velocity[i] = num;
  471. }
  472. else if (this.m_Inertia)
  473. {
  474. int index;
  475. this.m_Velocity[index = i] = this.m_Velocity[index] * Mathf.Pow(this.m_DecelerationRate, unscaledDeltaTime);
  476. if (Mathf.Abs(this.m_Velocity[i]) < 1f)
  477. {
  478. this.m_Velocity[i] = 0f;
  479. }
  480. int index2;
  481. vector2[index2 = i] = vector2[index2] + this.m_Velocity[i] * unscaledDeltaTime;
  482. }
  483. else
  484. {
  485. this.m_Velocity[i] = 0f;
  486. }
  487. }
  488. if (this.m_MovementType == uGUIScrollRect.MovementType.Clamped)
  489. {
  490. vector = this.CalculateOffset(vector2 - this.m_Content.anchoredPosition);
  491. vector2 += vector;
  492. }
  493. if (this.m_Velocity != Vector2.zero || vector.sqrMagnitude >= 0.02f)
  494. {
  495. this.SetContentAnchoredPosition(vector2);
  496. }
  497. }
  498. if (this.m_Dragging && this.m_Inertia)
  499. {
  500. Vector3 b = (this.m_Content.anchoredPosition - this.m_PrevPosition) / unscaledDeltaTime;
  501. this.m_Velocity = Vector3.Lerp(this.m_Velocity, b, unscaledDeltaTime * 10f);
  502. }
  503. if (this.m_ViewBounds != this.m_PrevViewBounds || this.m_ContentBounds != this.m_PrevContentBounds || this.m_Content.anchoredPosition != this.m_PrevPosition)
  504. {
  505. this.UpdateSliders(vector);
  506. this.m_OnValueChanged.Invoke(this.normalizedPosition);
  507. this.UpdatePrevData();
  508. }
  509. }
  510. protected void UpdatePrevData()
  511. {
  512. if (this.m_Content == null)
  513. {
  514. this.m_PrevPosition = Vector2.zero;
  515. }
  516. else
  517. {
  518. this.m_PrevPosition = this.m_Content.anchoredPosition;
  519. }
  520. this.m_PrevViewBounds = this.m_ViewBounds;
  521. this.m_PrevContentBounds = this.m_ContentBounds;
  522. }
  523. private void UpdateSliders(Vector2 offset)
  524. {
  525. if (this.m_HorizontalSlider)
  526. {
  527. this.m_HorizontalSlider.normalizedValue = this.horizontalNormalizedPosition;
  528. }
  529. if (this.m_VerticalSlider)
  530. {
  531. this.m_VerticalSlider.normalizedValue = this.verticalNormalizedPosition;
  532. }
  533. }
  534. public Vector2 normalizedPosition
  535. {
  536. get
  537. {
  538. return new Vector2(this.horizontalNormalizedPosition, this.verticalNormalizedPosition);
  539. }
  540. set
  541. {
  542. this.SetNormalizedPosition(value.x, 0);
  543. this.SetNormalizedPosition(value.y, 1);
  544. }
  545. }
  546. public float horizontalNormalizedPosition
  547. {
  548. get
  549. {
  550. this.UpdateBounds();
  551. if (this.m_ContentBounds.size.x <= this.m_ViewBounds.size.x)
  552. {
  553. return (float)((this.m_ViewBounds.min.x <= this.m_ContentBounds.min.x) ? 0 : 1);
  554. }
  555. return (this.m_ViewBounds.min.x - this.m_ContentBounds.min.x) / (this.m_ContentBounds.size.x - this.m_ViewBounds.size.x);
  556. }
  557. set
  558. {
  559. this.SetNormalizedPosition(value, 0);
  560. }
  561. }
  562. public float verticalNormalizedPosition
  563. {
  564. get
  565. {
  566. this.UpdateBounds();
  567. if (this.m_ContentBounds.size.y <= this.m_ViewBounds.size.y)
  568. {
  569. return (float)((this.m_ViewBounds.min.y <= this.m_ContentBounds.min.y) ? 0 : 1);
  570. }
  571. return (this.m_ViewBounds.min.y - this.m_ContentBounds.min.y) / (this.m_ContentBounds.size.y - this.m_ViewBounds.size.y);
  572. }
  573. set
  574. {
  575. this.SetNormalizedPosition(value, 1);
  576. }
  577. }
  578. private void SetHorizontalNormalizedPosition(float value)
  579. {
  580. this.SetNormalizedPosition(value, 0);
  581. }
  582. private void SetVerticalNormalizedPosition(float value)
  583. {
  584. this.SetNormalizedPosition(value, 1);
  585. }
  586. protected virtual void SetNormalizedPosition(float value, int axis)
  587. {
  588. this.EnsureLayoutHasRebuilt();
  589. this.UpdateBounds();
  590. float num = this.m_ContentBounds.size[axis] - this.m_ViewBounds.size[axis];
  591. float num2 = this.m_ViewBounds.min[axis] - value * num;
  592. float num3 = this.m_Content.localPosition[axis] + num2 - this.m_ContentBounds.min[axis];
  593. Vector3 localPosition = this.m_Content.localPosition;
  594. if (Mathf.Abs(localPosition[axis] - num3) > 0.01f)
  595. {
  596. localPosition[axis] = num3;
  597. this.m_Content.localPosition = localPosition;
  598. this.m_Velocity[axis] = 0f;
  599. this.UpdateBounds();
  600. }
  601. }
  602. private static float RubberDelta(float overStretching, float viewSize)
  603. {
  604. return (1f - 1f / (Mathf.Abs(overStretching) * 0.55f / viewSize + 1f)) * viewSize * Mathf.Sign(overStretching);
  605. }
  606. protected override void OnRectTransformDimensionsChange()
  607. {
  608. this.SetDirty();
  609. }
  610. private bool hScrollingNeeded
  611. {
  612. get
  613. {
  614. return !Application.isPlaying || this.m_ContentBounds.size.x > this.m_ViewBounds.size.x + 1f;
  615. }
  616. }
  617. private bool vScrollingNeeded
  618. {
  619. get
  620. {
  621. return !Application.isPlaying || this.m_ContentBounds.size.y > this.m_ViewBounds.size.y + 1f;
  622. }
  623. }
  624. public virtual void CalculateLayoutInputHorizontal()
  625. {
  626. }
  627. public virtual void CalculateLayoutInputVertical()
  628. {
  629. }
  630. public virtual float minWidth
  631. {
  632. get
  633. {
  634. return -1f;
  635. }
  636. }
  637. public virtual float preferredWidth
  638. {
  639. get
  640. {
  641. return -1f;
  642. }
  643. }
  644. public virtual float flexibleWidth
  645. {
  646. get
  647. {
  648. return -1f;
  649. }
  650. }
  651. public virtual float minHeight
  652. {
  653. get
  654. {
  655. return -1f;
  656. }
  657. }
  658. public virtual float preferredHeight
  659. {
  660. get
  661. {
  662. return -1f;
  663. }
  664. }
  665. public virtual float flexibleHeight
  666. {
  667. get
  668. {
  669. return -1f;
  670. }
  671. }
  672. public virtual int layoutPriority
  673. {
  674. get
  675. {
  676. return -1;
  677. }
  678. }
  679. public virtual void SetLayoutHorizontal()
  680. {
  681. this.m_Tracker.Clear();
  682. if (this.m_HSliderExpand || this.m_VSliderExpand)
  683. {
  684. this.m_Tracker.Add(this, this.viewRect, DrivenTransformProperties.AnchoredPositionX | DrivenTransformProperties.AnchoredPositionY | DrivenTransformProperties.AnchorMinX | DrivenTransformProperties.AnchorMinY | DrivenTransformProperties.AnchorMaxX | DrivenTransformProperties.AnchorMaxY | DrivenTransformProperties.SizeDeltaX | DrivenTransformProperties.SizeDeltaY);
  685. this.viewRect.anchorMin = Vector2.zero;
  686. this.viewRect.anchorMax = Vector2.one;
  687. this.viewRect.sizeDelta = Vector2.zero;
  688. this.viewRect.anchoredPosition = Vector2.zero;
  689. LayoutRebuilder.ForceRebuildLayoutImmediate(this.content);
  690. this.m_ViewBounds = new Bounds(this.viewRect.rect.center, this.viewRect.rect.size);
  691. this.m_ContentBounds = this.GetBounds();
  692. }
  693. if (this.m_VSliderExpand && this.vScrollingNeeded)
  694. {
  695. this.viewRect.sizeDelta = new Vector2(-(this.m_VSliderWidth + this.m_VerticalSliderSpacing), this.viewRect.sizeDelta.y);
  696. LayoutRebuilder.ForceRebuildLayoutImmediate(this.content);
  697. this.m_ViewBounds = new Bounds(this.viewRect.rect.center, this.viewRect.rect.size);
  698. this.m_ContentBounds = this.GetBounds();
  699. }
  700. if (this.m_HSliderExpand && this.hScrollingNeeded)
  701. {
  702. this.viewRect.sizeDelta = new Vector2(this.viewRect.sizeDelta.x, -(this.m_HSliderHeight + this.m_HorizontalSliderSpacing));
  703. this.m_ViewBounds = new Bounds(this.viewRect.rect.center, this.viewRect.rect.size);
  704. this.m_ContentBounds = this.GetBounds();
  705. }
  706. if (this.m_VSliderExpand && this.vScrollingNeeded && this.viewRect.sizeDelta.x == 0f && this.viewRect.sizeDelta.y < 0f)
  707. {
  708. this.viewRect.sizeDelta = new Vector2(-(this.m_VSliderWidth + this.m_VerticalSliderSpacing), this.viewRect.sizeDelta.y);
  709. }
  710. }
  711. public virtual void SetLayoutVertical()
  712. {
  713. this.UpdateSliderLayout();
  714. this.m_ViewBounds = new Bounds(this.viewRect.rect.center, this.viewRect.rect.size);
  715. this.m_ContentBounds = this.GetBounds();
  716. }
  717. private void UpdateSliderVisibility()
  718. {
  719. uGUIScrollRect.UpdateOneSliderVisibility(this.vScrollingNeeded, this.m_Vertical, this.m_VerticalSliderVisibility, this.m_VerticalSlider);
  720. uGUIScrollRect.UpdateOneSliderVisibility(this.hScrollingNeeded, this.m_Horizontal, this.m_HorizontalSliderVisibility, this.m_HorizontalSlider);
  721. }
  722. private static void UpdateOneSliderVisibility(bool xScrollingNeeded, bool xAxisEnabled, uGUIScrollRect.SliderVisibility sliderVisibility, Slider slider)
  723. {
  724. if (slider)
  725. {
  726. if (sliderVisibility == uGUIScrollRect.SliderVisibility.Permanent)
  727. {
  728. if (slider.gameObject.activeSelf != xAxisEnabled)
  729. {
  730. slider.gameObject.SetActive(xAxisEnabled);
  731. }
  732. }
  733. else if (slider.gameObject.activeSelf != xScrollingNeeded)
  734. {
  735. slider.gameObject.SetActive(xScrollingNeeded);
  736. }
  737. }
  738. }
  739. private void UpdateSliderLayout()
  740. {
  741. if (this.m_VSliderExpand && this.m_HorizontalSlider)
  742. {
  743. this.m_Tracker.Add(this, this.m_HorizontalSliderRect, DrivenTransformProperties.AnchoredPositionX | DrivenTransformProperties.AnchorMinX | DrivenTransformProperties.AnchorMaxX | DrivenTransformProperties.SizeDeltaX);
  744. this.m_HorizontalSliderRect.anchorMin = new Vector2(0f, this.m_HorizontalSliderRect.anchorMin.y);
  745. this.m_HorizontalSliderRect.anchorMax = new Vector2(1f, this.m_HorizontalSliderRect.anchorMax.y);
  746. this.m_HorizontalSliderRect.anchoredPosition = new Vector2(0f, this.m_HorizontalSliderRect.anchoredPosition.y);
  747. if (this.vScrollingNeeded)
  748. {
  749. this.m_HorizontalSliderRect.sizeDelta = new Vector2(-(this.m_VSliderWidth + this.m_VerticalSliderSpacing), this.m_HorizontalSliderRect.sizeDelta.y);
  750. }
  751. else
  752. {
  753. this.m_HorizontalSliderRect.sizeDelta = new Vector2(0f, this.m_HorizontalSliderRect.sizeDelta.y);
  754. }
  755. }
  756. if (this.m_HSliderExpand && this.m_VerticalSlider)
  757. {
  758. this.m_Tracker.Add(this, this.m_VerticalSliderRect, DrivenTransformProperties.AnchoredPositionY | DrivenTransformProperties.AnchorMinY | DrivenTransformProperties.AnchorMaxY | DrivenTransformProperties.SizeDeltaY);
  759. this.m_VerticalSliderRect.anchorMin = new Vector2(this.m_VerticalSliderRect.anchorMin.x, 0f);
  760. this.m_VerticalSliderRect.anchorMax = new Vector2(this.m_VerticalSliderRect.anchorMax.x, 1f);
  761. this.m_VerticalSliderRect.anchoredPosition = new Vector2(this.m_VerticalSliderRect.anchoredPosition.x, 0f);
  762. if (this.hScrollingNeeded)
  763. {
  764. this.m_VerticalSliderRect.sizeDelta = new Vector2(this.m_VerticalSliderRect.sizeDelta.x, -(this.m_HSliderHeight + this.m_HorizontalSliderSpacing));
  765. }
  766. else
  767. {
  768. this.m_VerticalSliderRect.sizeDelta = new Vector2(this.m_VerticalSliderRect.sizeDelta.x, 0f);
  769. }
  770. }
  771. }
  772. protected void UpdateBounds()
  773. {
  774. this.m_ViewBounds = new Bounds(this.viewRect.rect.center, this.viewRect.rect.size);
  775. this.m_ContentBounds = this.GetBounds();
  776. if (this.m_Content == null)
  777. {
  778. return;
  779. }
  780. Vector3 size = this.m_ContentBounds.size;
  781. Vector3 center = this.m_ContentBounds.center;
  782. Vector2 pivot = this.m_Content.pivot;
  783. uGUIScrollRect.AdjustBounds(ref this.m_ViewBounds, ref pivot, ref size, ref center);
  784. this.m_ContentBounds.size = size;
  785. this.m_ContentBounds.center = center;
  786. if (this.movementType == uGUIScrollRect.MovementType.Clamped)
  787. {
  788. Vector3 zero = Vector3.zero;
  789. if (this.m_ViewBounds.max.x > this.m_ContentBounds.max.x)
  790. {
  791. zero.x = Math.Min(this.m_ViewBounds.min.x - this.m_ContentBounds.min.x, this.m_ViewBounds.max.x - this.m_ContentBounds.max.x);
  792. }
  793. else if (this.m_ViewBounds.min.x < this.m_ContentBounds.min.x)
  794. {
  795. zero.x = Math.Max(this.m_ViewBounds.min.x - this.m_ContentBounds.min.x, this.m_ViewBounds.max.x - this.m_ContentBounds.max.x);
  796. }
  797. if (this.m_ViewBounds.min.y < this.m_ContentBounds.min.y)
  798. {
  799. zero.y = Math.Max(this.m_ViewBounds.min.y - this.m_ContentBounds.min.y, this.m_ViewBounds.max.y - this.m_ContentBounds.max.y);
  800. }
  801. else if (this.m_ViewBounds.max.y > this.m_ContentBounds.max.y)
  802. {
  803. zero.y = Math.Min(this.m_ViewBounds.min.y - this.m_ContentBounds.min.y, this.m_ViewBounds.max.y - this.m_ContentBounds.max.y);
  804. }
  805. if (zero.sqrMagnitude > 0.01f)
  806. {
  807. this.m_ContentBounds = this.GetBounds();
  808. size = this.m_ContentBounds.size;
  809. center = this.m_ContentBounds.center;
  810. pivot = this.m_Content.pivot;
  811. uGUIScrollRect.AdjustBounds(ref this.m_ViewBounds, ref pivot, ref size, ref center);
  812. this.m_ContentBounds.size = size;
  813. this.m_ContentBounds.center = center;
  814. }
  815. }
  816. }
  817. internal static void AdjustBounds(ref Bounds viewBounds, ref Vector2 contentPivot, ref Vector3 contentSize, ref Vector3 contentPos)
  818. {
  819. Vector3 vector = viewBounds.size - contentSize;
  820. if (vector.x > 0f)
  821. {
  822. contentPos.x -= vector.x * (contentPivot.x - 0.5f);
  823. contentSize.x = viewBounds.size.x;
  824. }
  825. if (vector.y > 0f)
  826. {
  827. contentPos.y -= vector.y * (contentPivot.y - 0.5f);
  828. contentSize.y = viewBounds.size.y;
  829. }
  830. }
  831. private Bounds GetBounds()
  832. {
  833. if (this.m_Content == null)
  834. {
  835. return default(Bounds);
  836. }
  837. this.m_Content.GetWorldCorners(this.m_Corners);
  838. Matrix4x4 worldToLocalMatrix = this.viewRect.worldToLocalMatrix;
  839. return uGUIScrollRect.InternalGetBounds(this.m_Corners, ref worldToLocalMatrix);
  840. }
  841. internal static Bounds InternalGetBounds(Vector3[] corners, ref Matrix4x4 viewWorldToLocalMatrix)
  842. {
  843. Vector3 vector = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  844. Vector3 vector2 = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  845. for (int i = 0; i < 4; i++)
  846. {
  847. Vector3 lhs = viewWorldToLocalMatrix.MultiplyPoint3x4(corners[i]);
  848. vector = Vector3.Min(lhs, vector);
  849. vector2 = Vector3.Max(lhs, vector2);
  850. }
  851. Bounds result = new Bounds(vector, Vector3.zero);
  852. result.Encapsulate(vector2);
  853. return result;
  854. }
  855. private Vector2 CalculateOffset(Vector2 delta)
  856. {
  857. return uGUIScrollRect.InternalCalculateOffset(ref this.m_ViewBounds, ref this.m_ContentBounds, this.m_Horizontal, this.m_Vertical, this.m_MovementType, ref delta);
  858. }
  859. internal static Vector2 InternalCalculateOffset(ref Bounds viewBounds, ref Bounds contentBounds, bool horizontal, bool vertical, uGUIScrollRect.MovementType movementType, ref Vector2 delta)
  860. {
  861. Vector2 zero = Vector2.zero;
  862. if (movementType == uGUIScrollRect.MovementType.Unrestricted)
  863. {
  864. return zero;
  865. }
  866. Vector2 vector = contentBounds.min;
  867. Vector2 vector2 = contentBounds.max;
  868. if (horizontal)
  869. {
  870. vector.x += delta.x;
  871. vector2.x += delta.x;
  872. if (vector.x > viewBounds.min.x)
  873. {
  874. zero.x = viewBounds.min.x - vector.x;
  875. }
  876. else if (vector2.x < viewBounds.max.x)
  877. {
  878. zero.x = viewBounds.max.x - vector2.x;
  879. }
  880. }
  881. if (vertical)
  882. {
  883. vector.y += delta.y;
  884. vector2.y += delta.y;
  885. if (vector2.y < viewBounds.max.y)
  886. {
  887. zero.y = viewBounds.max.y - vector2.y;
  888. }
  889. else if (vector.y > viewBounds.min.y)
  890. {
  891. zero.y = viewBounds.min.y - vector.y;
  892. }
  893. }
  894. return zero;
  895. }
  896. protected void SetDirty()
  897. {
  898. if (!this.IsActive())
  899. {
  900. return;
  901. }
  902. LayoutRebuilder.MarkLayoutForRebuild(this.rectTransform);
  903. }
  904. protected void SetDirtyCaching()
  905. {
  906. if (!this.IsActive())
  907. {
  908. return;
  909. }
  910. CanvasUpdateRegistry.RegisterCanvasElementForLayoutRebuild(this);
  911. LayoutRebuilder.MarkLayoutForRebuild(this.rectTransform);
  912. }
  913. Transform ICanvasElement.get_transform()
  914. {
  915. return base.transform;
  916. }
  917. bool ICanvasElement.IsDestroyed()
  918. {
  919. return base.IsDestroyed();
  920. }
  921. [SerializeField]
  922. private RectTransform m_Content;
  923. [SerializeField]
  924. private bool m_Horizontal = true;
  925. [SerializeField]
  926. private bool m_Vertical = true;
  927. [SerializeField]
  928. private uGUIScrollRect.MovementType m_MovementType = uGUIScrollRect.MovementType.Elastic;
  929. [SerializeField]
  930. private float m_Elasticity = 0.1f;
  931. [SerializeField]
  932. private bool m_Inertia = true;
  933. [SerializeField]
  934. private float m_DecelerationRate = 0.135f;
  935. [SerializeField]
  936. private float m_ScrollSensitivity = 1f;
  937. [SerializeField]
  938. private RectTransform m_Viewport;
  939. [SerializeField]
  940. private Slider m_HorizontalSlider;
  941. [SerializeField]
  942. private Slider m_VerticalSlider;
  943. [SerializeField]
  944. private uGUIScrollRect.SliderVisibility m_HorizontalSliderVisibility;
  945. [SerializeField]
  946. private uGUIScrollRect.SliderVisibility m_VerticalSliderVisibility;
  947. [SerializeField]
  948. private float m_HorizontalSliderSpacing;
  949. [SerializeField]
  950. private float m_VerticalSliderSpacing;
  951. [SerializeField]
  952. private uGUIScrollRect.ScrollRectEvent m_OnValueChanged = new uGUIScrollRect.ScrollRectEvent();
  953. private Vector2 m_PointerStartLocalCursor = Vector2.zero;
  954. protected Vector2 m_ContentStartPosition = Vector2.zero;
  955. private RectTransform m_ViewRect;
  956. protected Bounds m_ContentBounds;
  957. private Bounds m_ViewBounds;
  958. private Vector2 m_Velocity;
  959. private bool m_Dragging;
  960. private Vector2 m_PrevPosition = Vector2.zero;
  961. private Bounds m_PrevContentBounds;
  962. private Bounds m_PrevViewBounds;
  963. [NonSerialized]
  964. private bool m_HasRebuiltLayout;
  965. private bool m_HSliderExpand;
  966. private bool m_VSliderExpand;
  967. private float m_HSliderHeight;
  968. private float m_VSliderWidth;
  969. [NonSerialized]
  970. private RectTransform m_Rect;
  971. private RectTransform m_HorizontalSliderRect;
  972. private RectTransform m_VerticalSliderRect;
  973. private DrivenRectTransformTracker m_Tracker;
  974. private readonly Vector3[] m_Corners = new Vector3[4];
  975. public enum MovementType
  976. {
  977. Unrestricted,
  978. Elastic,
  979. Clamped
  980. }
  981. public enum SliderVisibility
  982. {
  983. Permanent,
  984. AutoHide,
  985. AutoHideAndExpandViewport
  986. }
  987. [Serializable]
  988. public class ScrollRectEvent : UnityEvent<Vector2>
  989. {
  990. }
  991. }
  992. }