uGUIScrollRect.cs 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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. ref Vector2 ptr = ref this.m_Velocity;
  475. int index;
  476. this.m_Velocity[index = i] = ptr[index] * Mathf.Pow(this.m_DecelerationRate, unscaledDeltaTime);
  477. if (Mathf.Abs(this.m_Velocity[i]) < 1f)
  478. {
  479. this.m_Velocity[i] = 0f;
  480. }
  481. ptr = ref vector2;
  482. int index2;
  483. vector2[index2 = i] = ptr[index2] + this.m_Velocity[i] * unscaledDeltaTime;
  484. }
  485. else
  486. {
  487. this.m_Velocity[i] = 0f;
  488. }
  489. }
  490. if (this.m_MovementType == uGUIScrollRect.MovementType.Clamped)
  491. {
  492. vector = this.CalculateOffset(vector2 - this.m_Content.anchoredPosition);
  493. vector2 += vector;
  494. }
  495. if (this.m_Velocity != Vector2.zero || vector.sqrMagnitude >= 0.02f)
  496. {
  497. this.SetContentAnchoredPosition(vector2);
  498. }
  499. }
  500. if (this.m_Dragging && this.m_Inertia)
  501. {
  502. Vector3 b = (this.m_Content.anchoredPosition - this.m_PrevPosition) / unscaledDeltaTime;
  503. this.m_Velocity = Vector3.Lerp(this.m_Velocity, b, unscaledDeltaTime * 10f);
  504. }
  505. if (this.m_ViewBounds != this.m_PrevViewBounds || this.m_ContentBounds != this.m_PrevContentBounds || this.m_Content.anchoredPosition != this.m_PrevPosition)
  506. {
  507. this.UpdateSliders(vector);
  508. this.m_OnValueChanged.Invoke(this.normalizedPosition);
  509. this.UpdatePrevData();
  510. }
  511. }
  512. protected void UpdatePrevData()
  513. {
  514. if (this.m_Content == null)
  515. {
  516. this.m_PrevPosition = Vector2.zero;
  517. }
  518. else
  519. {
  520. this.m_PrevPosition = this.m_Content.anchoredPosition;
  521. }
  522. this.m_PrevViewBounds = this.m_ViewBounds;
  523. this.m_PrevContentBounds = this.m_ContentBounds;
  524. }
  525. private void UpdateSliders(Vector2 offset)
  526. {
  527. if (this.m_HorizontalSlider)
  528. {
  529. this.m_HorizontalSlider.normalizedValue = this.horizontalNormalizedPosition;
  530. }
  531. if (this.m_VerticalSlider)
  532. {
  533. this.m_VerticalSlider.normalizedValue = this.verticalNormalizedPosition;
  534. }
  535. }
  536. public Vector2 normalizedPosition
  537. {
  538. get
  539. {
  540. return new Vector2(this.horizontalNormalizedPosition, this.verticalNormalizedPosition);
  541. }
  542. set
  543. {
  544. this.SetNormalizedPosition(value.x, 0);
  545. this.SetNormalizedPosition(value.y, 1);
  546. }
  547. }
  548. public float horizontalNormalizedPosition
  549. {
  550. get
  551. {
  552. this.UpdateBounds();
  553. if (this.m_ContentBounds.size.x <= this.m_ViewBounds.size.x)
  554. {
  555. return (float)((this.m_ViewBounds.min.x <= this.m_ContentBounds.min.x) ? 0 : 1);
  556. }
  557. return (this.m_ViewBounds.min.x - this.m_ContentBounds.min.x) / (this.m_ContentBounds.size.x - this.m_ViewBounds.size.x);
  558. }
  559. set
  560. {
  561. this.SetNormalizedPosition(value, 0);
  562. }
  563. }
  564. public float verticalNormalizedPosition
  565. {
  566. get
  567. {
  568. this.UpdateBounds();
  569. if (this.m_ContentBounds.size.y <= this.m_ViewBounds.size.y)
  570. {
  571. return (float)((this.m_ViewBounds.min.y <= this.m_ContentBounds.min.y) ? 0 : 1);
  572. }
  573. return (this.m_ViewBounds.min.y - this.m_ContentBounds.min.y) / (this.m_ContentBounds.size.y - this.m_ViewBounds.size.y);
  574. }
  575. set
  576. {
  577. this.SetNormalizedPosition(value, 1);
  578. }
  579. }
  580. private void SetHorizontalNormalizedPosition(float value)
  581. {
  582. this.SetNormalizedPosition(value, 0);
  583. }
  584. private void SetVerticalNormalizedPosition(float value)
  585. {
  586. this.SetNormalizedPosition(value, 1);
  587. }
  588. protected virtual void SetNormalizedPosition(float value, int axis)
  589. {
  590. this.EnsureLayoutHasRebuilt();
  591. this.UpdateBounds();
  592. float num = this.m_ContentBounds.size[axis] - this.m_ViewBounds.size[axis];
  593. float num2 = this.m_ViewBounds.min[axis] - value * num;
  594. float num3 = this.m_Content.localPosition[axis] + num2 - this.m_ContentBounds.min[axis];
  595. Vector3 localPosition = this.m_Content.localPosition;
  596. if (Mathf.Abs(localPosition[axis] - num3) > 0.01f)
  597. {
  598. localPosition[axis] = num3;
  599. this.m_Content.localPosition = localPosition;
  600. this.m_Velocity[axis] = 0f;
  601. this.UpdateBounds();
  602. }
  603. }
  604. private static float RubberDelta(float overStretching, float viewSize)
  605. {
  606. return (1f - 1f / (Mathf.Abs(overStretching) * 0.55f / viewSize + 1f)) * viewSize * Mathf.Sign(overStretching);
  607. }
  608. protected override void OnRectTransformDimensionsChange()
  609. {
  610. this.SetDirty();
  611. }
  612. private bool hScrollingNeeded
  613. {
  614. get
  615. {
  616. return !Application.isPlaying || this.m_ContentBounds.size.x > this.m_ViewBounds.size.x + 1f;
  617. }
  618. }
  619. private bool vScrollingNeeded
  620. {
  621. get
  622. {
  623. return !Application.isPlaying || this.m_ContentBounds.size.y > this.m_ViewBounds.size.y + 1f;
  624. }
  625. }
  626. public virtual void CalculateLayoutInputHorizontal()
  627. {
  628. }
  629. public virtual void CalculateLayoutInputVertical()
  630. {
  631. }
  632. public virtual float minWidth
  633. {
  634. get
  635. {
  636. return -1f;
  637. }
  638. }
  639. public virtual float preferredWidth
  640. {
  641. get
  642. {
  643. return -1f;
  644. }
  645. }
  646. public virtual float flexibleWidth
  647. {
  648. get
  649. {
  650. return -1f;
  651. }
  652. }
  653. public virtual float minHeight
  654. {
  655. get
  656. {
  657. return -1f;
  658. }
  659. }
  660. public virtual float preferredHeight
  661. {
  662. get
  663. {
  664. return -1f;
  665. }
  666. }
  667. public virtual float flexibleHeight
  668. {
  669. get
  670. {
  671. return -1f;
  672. }
  673. }
  674. public virtual int layoutPriority
  675. {
  676. get
  677. {
  678. return -1;
  679. }
  680. }
  681. public virtual void SetLayoutHorizontal()
  682. {
  683. this.m_Tracker.Clear();
  684. if (this.m_HSliderExpand || this.m_VSliderExpand)
  685. {
  686. this.m_Tracker.Add(this, this.viewRect, DrivenTransformProperties.AnchoredPositionX | DrivenTransformProperties.AnchoredPositionY | DrivenTransformProperties.AnchorMinX | DrivenTransformProperties.AnchorMinY | DrivenTransformProperties.AnchorMaxX | DrivenTransformProperties.AnchorMaxY | DrivenTransformProperties.SizeDeltaX | DrivenTransformProperties.SizeDeltaY);
  687. this.viewRect.anchorMin = Vector2.zero;
  688. this.viewRect.anchorMax = Vector2.one;
  689. this.viewRect.sizeDelta = Vector2.zero;
  690. this.viewRect.anchoredPosition = Vector2.zero;
  691. LayoutRebuilder.ForceRebuildLayoutImmediate(this.content);
  692. this.m_ViewBounds = new Bounds(this.viewRect.rect.center, this.viewRect.rect.size);
  693. this.m_ContentBounds = this.GetBounds();
  694. }
  695. if (this.m_VSliderExpand && this.vScrollingNeeded)
  696. {
  697. this.viewRect.sizeDelta = new Vector2(-(this.m_VSliderWidth + this.m_VerticalSliderSpacing), this.viewRect.sizeDelta.y);
  698. LayoutRebuilder.ForceRebuildLayoutImmediate(this.content);
  699. this.m_ViewBounds = new Bounds(this.viewRect.rect.center, this.viewRect.rect.size);
  700. this.m_ContentBounds = this.GetBounds();
  701. }
  702. if (this.m_HSliderExpand && this.hScrollingNeeded)
  703. {
  704. this.viewRect.sizeDelta = new Vector2(this.viewRect.sizeDelta.x, -(this.m_HSliderHeight + this.m_HorizontalSliderSpacing));
  705. this.m_ViewBounds = new Bounds(this.viewRect.rect.center, this.viewRect.rect.size);
  706. this.m_ContentBounds = this.GetBounds();
  707. }
  708. if (this.m_VSliderExpand && this.vScrollingNeeded && this.viewRect.sizeDelta.x == 0f && this.viewRect.sizeDelta.y < 0f)
  709. {
  710. this.viewRect.sizeDelta = new Vector2(-(this.m_VSliderWidth + this.m_VerticalSliderSpacing), this.viewRect.sizeDelta.y);
  711. }
  712. }
  713. public virtual void SetLayoutVertical()
  714. {
  715. this.UpdateSliderLayout();
  716. this.m_ViewBounds = new Bounds(this.viewRect.rect.center, this.viewRect.rect.size);
  717. this.m_ContentBounds = this.GetBounds();
  718. }
  719. private void UpdateSliderVisibility()
  720. {
  721. uGUIScrollRect.UpdateOneSliderVisibility(this.vScrollingNeeded, this.m_Vertical, this.m_VerticalSliderVisibility, this.m_VerticalSlider);
  722. uGUIScrollRect.UpdateOneSliderVisibility(this.hScrollingNeeded, this.m_Horizontal, this.m_HorizontalSliderVisibility, this.m_HorizontalSlider);
  723. }
  724. private static void UpdateOneSliderVisibility(bool xScrollingNeeded, bool xAxisEnabled, uGUIScrollRect.SliderVisibility sliderVisibility, Slider slider)
  725. {
  726. if (slider)
  727. {
  728. if (sliderVisibility == uGUIScrollRect.SliderVisibility.Permanent)
  729. {
  730. if (slider.gameObject.activeSelf != xAxisEnabled)
  731. {
  732. slider.gameObject.SetActive(xAxisEnabled);
  733. }
  734. }
  735. else if (slider.gameObject.activeSelf != xScrollingNeeded)
  736. {
  737. slider.gameObject.SetActive(xScrollingNeeded);
  738. }
  739. }
  740. }
  741. private void UpdateSliderLayout()
  742. {
  743. if (this.m_VSliderExpand && this.m_HorizontalSlider)
  744. {
  745. this.m_Tracker.Add(this, this.m_HorizontalSliderRect, DrivenTransformProperties.AnchoredPositionX | DrivenTransformProperties.AnchorMinX | DrivenTransformProperties.AnchorMaxX | DrivenTransformProperties.SizeDeltaX);
  746. this.m_HorizontalSliderRect.anchorMin = new Vector2(0f, this.m_HorizontalSliderRect.anchorMin.y);
  747. this.m_HorizontalSliderRect.anchorMax = new Vector2(1f, this.m_HorizontalSliderRect.anchorMax.y);
  748. this.m_HorizontalSliderRect.anchoredPosition = new Vector2(0f, this.m_HorizontalSliderRect.anchoredPosition.y);
  749. if (this.vScrollingNeeded)
  750. {
  751. this.m_HorizontalSliderRect.sizeDelta = new Vector2(-(this.m_VSliderWidth + this.m_VerticalSliderSpacing), this.m_HorizontalSliderRect.sizeDelta.y);
  752. }
  753. else
  754. {
  755. this.m_HorizontalSliderRect.sizeDelta = new Vector2(0f, this.m_HorizontalSliderRect.sizeDelta.y);
  756. }
  757. }
  758. if (this.m_HSliderExpand && this.m_VerticalSlider)
  759. {
  760. this.m_Tracker.Add(this, this.m_VerticalSliderRect, DrivenTransformProperties.AnchoredPositionY | DrivenTransformProperties.AnchorMinY | DrivenTransformProperties.AnchorMaxY | DrivenTransformProperties.SizeDeltaY);
  761. this.m_VerticalSliderRect.anchorMin = new Vector2(this.m_VerticalSliderRect.anchorMin.x, 0f);
  762. this.m_VerticalSliderRect.anchorMax = new Vector2(this.m_VerticalSliderRect.anchorMax.x, 1f);
  763. this.m_VerticalSliderRect.anchoredPosition = new Vector2(this.m_VerticalSliderRect.anchoredPosition.x, 0f);
  764. if (this.hScrollingNeeded)
  765. {
  766. this.m_VerticalSliderRect.sizeDelta = new Vector2(this.m_VerticalSliderRect.sizeDelta.x, -(this.m_HSliderHeight + this.m_HorizontalSliderSpacing));
  767. }
  768. else
  769. {
  770. this.m_VerticalSliderRect.sizeDelta = new Vector2(this.m_VerticalSliderRect.sizeDelta.x, 0f);
  771. }
  772. }
  773. }
  774. protected void UpdateBounds()
  775. {
  776. this.m_ViewBounds = new Bounds(this.viewRect.rect.center, this.viewRect.rect.size);
  777. this.m_ContentBounds = this.GetBounds();
  778. if (this.m_Content == null)
  779. {
  780. return;
  781. }
  782. Vector3 size = this.m_ContentBounds.size;
  783. Vector3 center = this.m_ContentBounds.center;
  784. Vector2 pivot = this.m_Content.pivot;
  785. uGUIScrollRect.AdjustBounds(ref this.m_ViewBounds, ref pivot, ref size, ref center);
  786. this.m_ContentBounds.size = size;
  787. this.m_ContentBounds.center = center;
  788. if (this.movementType == uGUIScrollRect.MovementType.Clamped)
  789. {
  790. Vector3 zero = Vector3.zero;
  791. if (this.m_ViewBounds.max.x > this.m_ContentBounds.max.x)
  792. {
  793. zero.x = Math.Min(this.m_ViewBounds.min.x - this.m_ContentBounds.min.x, this.m_ViewBounds.max.x - this.m_ContentBounds.max.x);
  794. }
  795. else if (this.m_ViewBounds.min.x < this.m_ContentBounds.min.x)
  796. {
  797. zero.x = Math.Max(this.m_ViewBounds.min.x - this.m_ContentBounds.min.x, this.m_ViewBounds.max.x - this.m_ContentBounds.max.x);
  798. }
  799. if (this.m_ViewBounds.min.y < this.m_ContentBounds.min.y)
  800. {
  801. zero.y = Math.Max(this.m_ViewBounds.min.y - this.m_ContentBounds.min.y, this.m_ViewBounds.max.y - this.m_ContentBounds.max.y);
  802. }
  803. else if (this.m_ViewBounds.max.y > this.m_ContentBounds.max.y)
  804. {
  805. zero.y = Math.Min(this.m_ViewBounds.min.y - this.m_ContentBounds.min.y, this.m_ViewBounds.max.y - this.m_ContentBounds.max.y);
  806. }
  807. if (zero.sqrMagnitude > 0.01f)
  808. {
  809. this.m_ContentBounds = this.GetBounds();
  810. size = this.m_ContentBounds.size;
  811. center = this.m_ContentBounds.center;
  812. pivot = this.m_Content.pivot;
  813. uGUIScrollRect.AdjustBounds(ref this.m_ViewBounds, ref pivot, ref size, ref center);
  814. this.m_ContentBounds.size = size;
  815. this.m_ContentBounds.center = center;
  816. }
  817. }
  818. }
  819. internal static void AdjustBounds(ref Bounds viewBounds, ref Vector2 contentPivot, ref Vector3 contentSize, ref Vector3 contentPos)
  820. {
  821. Vector3 vector = viewBounds.size - contentSize;
  822. if (vector.x > 0f)
  823. {
  824. contentPos.x -= vector.x * (contentPivot.x - 0.5f);
  825. contentSize.x = viewBounds.size.x;
  826. }
  827. if (vector.y > 0f)
  828. {
  829. contentPos.y -= vector.y * (contentPivot.y - 0.5f);
  830. contentSize.y = viewBounds.size.y;
  831. }
  832. }
  833. private Bounds GetBounds()
  834. {
  835. if (this.m_Content == null)
  836. {
  837. return default(Bounds);
  838. }
  839. this.m_Content.GetWorldCorners(this.m_Corners);
  840. Matrix4x4 worldToLocalMatrix = this.viewRect.worldToLocalMatrix;
  841. return uGUIScrollRect.InternalGetBounds(this.m_Corners, ref worldToLocalMatrix);
  842. }
  843. internal static Bounds InternalGetBounds(Vector3[] corners, ref Matrix4x4 viewWorldToLocalMatrix)
  844. {
  845. Vector3 vector = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  846. Vector3 vector2 = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  847. for (int i = 0; i < 4; i++)
  848. {
  849. Vector3 lhs = viewWorldToLocalMatrix.MultiplyPoint3x4(corners[i]);
  850. vector = Vector3.Min(lhs, vector);
  851. vector2 = Vector3.Max(lhs, vector2);
  852. }
  853. Bounds result = new Bounds(vector, Vector3.zero);
  854. result.Encapsulate(vector2);
  855. return result;
  856. }
  857. private Vector2 CalculateOffset(Vector2 delta)
  858. {
  859. return uGUIScrollRect.InternalCalculateOffset(ref this.m_ViewBounds, ref this.m_ContentBounds, this.m_Horizontal, this.m_Vertical, this.m_MovementType, ref delta);
  860. }
  861. internal static Vector2 InternalCalculateOffset(ref Bounds viewBounds, ref Bounds contentBounds, bool horizontal, bool vertical, uGUIScrollRect.MovementType movementType, ref Vector2 delta)
  862. {
  863. Vector2 zero = Vector2.zero;
  864. if (movementType == uGUIScrollRect.MovementType.Unrestricted)
  865. {
  866. return zero;
  867. }
  868. Vector2 vector = contentBounds.min;
  869. Vector2 vector2 = contentBounds.max;
  870. if (horizontal)
  871. {
  872. vector.x += delta.x;
  873. vector2.x += delta.x;
  874. if (vector.x > viewBounds.min.x)
  875. {
  876. zero.x = viewBounds.min.x - vector.x;
  877. }
  878. else if (vector2.x < viewBounds.max.x)
  879. {
  880. zero.x = viewBounds.max.x - vector2.x;
  881. }
  882. }
  883. if (vertical)
  884. {
  885. vector.y += delta.y;
  886. vector2.y += delta.y;
  887. if (vector2.y < viewBounds.max.y)
  888. {
  889. zero.y = viewBounds.max.y - vector2.y;
  890. }
  891. else if (vector.y > viewBounds.min.y)
  892. {
  893. zero.y = viewBounds.min.y - vector.y;
  894. }
  895. }
  896. return zero;
  897. }
  898. protected void SetDirty()
  899. {
  900. if (!this.IsActive())
  901. {
  902. return;
  903. }
  904. LayoutRebuilder.MarkLayoutForRebuild(this.rectTransform);
  905. }
  906. protected void SetDirtyCaching()
  907. {
  908. if (!this.IsActive())
  909. {
  910. return;
  911. }
  912. CanvasUpdateRegistry.RegisterCanvasElementForLayoutRebuild(this);
  913. LayoutRebuilder.MarkLayoutForRebuild(this.rectTransform);
  914. }
  915. Transform ICanvasElement.get_transform()
  916. {
  917. return base.transform;
  918. }
  919. bool ICanvasElement.IsDestroyed()
  920. {
  921. return base.IsDestroyed();
  922. }
  923. [SerializeField]
  924. private RectTransform m_Content;
  925. [SerializeField]
  926. private bool m_Horizontal = true;
  927. [SerializeField]
  928. private bool m_Vertical = true;
  929. [SerializeField]
  930. private uGUIScrollRect.MovementType m_MovementType = uGUIScrollRect.MovementType.Elastic;
  931. [SerializeField]
  932. private float m_Elasticity = 0.1f;
  933. [SerializeField]
  934. private bool m_Inertia = true;
  935. [SerializeField]
  936. private float m_DecelerationRate = 0.135f;
  937. [SerializeField]
  938. private float m_ScrollSensitivity = 1f;
  939. [SerializeField]
  940. private RectTransform m_Viewport;
  941. [SerializeField]
  942. private Slider m_HorizontalSlider;
  943. [SerializeField]
  944. private Slider m_VerticalSlider;
  945. [SerializeField]
  946. private uGUIScrollRect.SliderVisibility m_HorizontalSliderVisibility;
  947. [SerializeField]
  948. private uGUIScrollRect.SliderVisibility m_VerticalSliderVisibility;
  949. [SerializeField]
  950. private float m_HorizontalSliderSpacing;
  951. [SerializeField]
  952. private float m_VerticalSliderSpacing;
  953. [SerializeField]
  954. private uGUIScrollRect.ScrollRectEvent m_OnValueChanged = new uGUIScrollRect.ScrollRectEvent();
  955. private Vector2 m_PointerStartLocalCursor = Vector2.zero;
  956. protected Vector2 m_ContentStartPosition = Vector2.zero;
  957. private RectTransform m_ViewRect;
  958. protected Bounds m_ContentBounds;
  959. private Bounds m_ViewBounds;
  960. private Vector2 m_Velocity;
  961. private bool m_Dragging;
  962. private Vector2 m_PrevPosition = Vector2.zero;
  963. private Bounds m_PrevContentBounds;
  964. private Bounds m_PrevViewBounds;
  965. [NonSerialized]
  966. private bool m_HasRebuiltLayout;
  967. private bool m_HSliderExpand;
  968. private bool m_VSliderExpand;
  969. private float m_HSliderHeight;
  970. private float m_VSliderWidth;
  971. [NonSerialized]
  972. private RectTransform m_Rect;
  973. private RectTransform m_HorizontalSliderRect;
  974. private RectTransform m_VerticalSliderRect;
  975. private DrivenRectTransformTracker m_Tracker;
  976. private readonly Vector3[] m_Corners = new Vector3[4];
  977. public enum MovementType
  978. {
  979. Unrestricted,
  980. Elastic,
  981. Clamped
  982. }
  983. public enum SliderVisibility
  984. {
  985. Permanent,
  986. AutoHide,
  987. AutoHideAndExpandViewport
  988. }
  989. [Serializable]
  990. public class ScrollRectEvent : UnityEvent<Vector2>
  991. {
  992. }
  993. }
  994. }