1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105 |
- using System;
- using UnityEngine.Events;
- using UnityEngine.EventSystems;
- namespace UnityEngine.UI
- {
- [SelectionBase]
- [ExecuteInEditMode]
- [DisallowMultipleComponent]
- [RequireComponent(typeof(RectTransform))]
- public class uGUIScrollRect : UIBehaviour, IInitializePotentialDragHandler, IBeginDragHandler, IEndDragHandler, IDragHandler, IScrollHandler, ICanvasElement, ILayoutElement, ILayoutGroup, IEventSystemHandler, ILayoutController
- {
- protected uGUIScrollRect()
- {
- }
- public RectTransform content
- {
- get
- {
- return this.m_Content;
- }
- set
- {
- this.m_Content = value;
- }
- }
- public bool horizontal
- {
- get
- {
- return this.m_Horizontal;
- }
- set
- {
- this.m_Horizontal = value;
- }
- }
- public bool vertical
- {
- get
- {
- return this.m_Vertical;
- }
- set
- {
- this.m_Vertical = value;
- }
- }
- public uGUIScrollRect.MovementType movementType
- {
- get
- {
- return this.m_MovementType;
- }
- set
- {
- this.m_MovementType = value;
- }
- }
- public float elasticity
- {
- get
- {
- return this.m_Elasticity;
- }
- set
- {
- this.m_Elasticity = value;
- }
- }
- public bool inertia
- {
- get
- {
- return this.m_Inertia;
- }
- set
- {
- this.m_Inertia = value;
- }
- }
- public float decelerationRate
- {
- get
- {
- return this.m_DecelerationRate;
- }
- set
- {
- this.m_DecelerationRate = value;
- }
- }
- public float scrollSensitivity
- {
- get
- {
- return this.m_ScrollSensitivity;
- }
- set
- {
- this.m_ScrollSensitivity = value;
- }
- }
- public RectTransform viewport
- {
- get
- {
- return this.m_Viewport;
- }
- set
- {
- this.m_Viewport = value;
- this.SetDirtyCaching();
- }
- }
- public Slider horizontalSlider
- {
- get
- {
- return this.m_HorizontalSlider;
- }
- set
- {
- if (this.m_HorizontalSlider)
- {
- this.m_HorizontalSlider.onValueChanged.RemoveListener(new UnityAction<float>(this.SetHorizontalNormalizedPosition));
- }
- this.m_HorizontalSlider = value;
- if (this.m_HorizontalSlider)
- {
- this.m_HorizontalSlider.onValueChanged.AddListener(new UnityAction<float>(this.SetHorizontalNormalizedPosition));
- }
- this.SetDirtyCaching();
- }
- }
- public Slider verticalSlider
- {
- get
- {
- return this.m_VerticalSlider;
- }
- set
- {
- if (this.m_VerticalSlider)
- {
- this.m_VerticalSlider.onValueChanged.RemoveListener(new UnityAction<float>(this.SetVerticalNormalizedPosition));
- }
- this.m_VerticalSlider = value;
- if (this.m_VerticalSlider)
- {
- this.m_VerticalSlider.onValueChanged.AddListener(new UnityAction<float>(this.SetVerticalNormalizedPosition));
- }
- this.SetDirtyCaching();
- }
- }
- public uGUIScrollRect.SliderVisibility horizontalSliderVisibility
- {
- get
- {
- return this.m_HorizontalSliderVisibility;
- }
- set
- {
- this.m_HorizontalSliderVisibility = value;
- this.SetDirtyCaching();
- }
- }
- public uGUIScrollRect.SliderVisibility verticalSliderVisibility
- {
- get
- {
- return this.m_VerticalSliderVisibility;
- }
- set
- {
- this.m_VerticalSliderVisibility = value;
- this.SetDirtyCaching();
- }
- }
- public float horizontalSliderSpacing
- {
- get
- {
- return this.m_HorizontalSliderSpacing;
- }
- set
- {
- this.m_HorizontalSliderSpacing = value;
- this.SetDirty();
- }
- }
- public float verticalSliderSpacing
- {
- get
- {
- return this.m_VerticalSliderSpacing;
- }
- set
- {
- this.m_VerticalSliderSpacing = value;
- this.SetDirty();
- }
- }
- public uGUIScrollRect.ScrollRectEvent onValueChanged
- {
- get
- {
- return this.m_OnValueChanged;
- }
- set
- {
- this.m_OnValueChanged = value;
- }
- }
- protected RectTransform viewRect
- {
- get
- {
- if (this.m_ViewRect == null)
- {
- this.m_ViewRect = this.m_Viewport;
- }
- if (this.m_ViewRect == null)
- {
- this.m_ViewRect = (RectTransform)base.transform;
- }
- return this.m_ViewRect;
- }
- }
- public Vector2 velocity
- {
- get
- {
- return this.m_Velocity;
- }
- set
- {
- this.m_Velocity = value;
- }
- }
- private RectTransform rectTransform
- {
- get
- {
- if (this.m_Rect == null)
- {
- this.m_Rect = base.GetComponent<RectTransform>();
- }
- return this.m_Rect;
- }
- }
- public virtual void Rebuild(CanvasUpdate executing)
- {
- if (executing == CanvasUpdate.Prelayout)
- {
- this.UpdateCachedData();
- }
- if (executing == CanvasUpdate.PostLayout)
- {
- this.UpdateBounds();
- this.UpdateSliders(Vector2.zero);
- this.UpdatePrevData();
- this.m_HasRebuiltLayout = true;
- }
- }
- public virtual void LayoutComplete()
- {
- }
- public virtual void GraphicUpdateComplete()
- {
- }
- private void UpdateCachedData()
- {
- Transform transform = base.transform;
- this.m_HorizontalSliderRect = ((!(this.m_HorizontalSlider == null)) ? (this.m_HorizontalSlider.transform as RectTransform) : null);
- this.m_VerticalSliderRect = ((!(this.m_VerticalSlider == null)) ? (this.m_VerticalSlider.transform as RectTransform) : null);
- bool flag = this.viewRect.parent == transform;
- bool flag2 = !this.m_HorizontalSliderRect || this.m_HorizontalSliderRect.parent == transform;
- bool flag3 = !this.m_VerticalSliderRect || this.m_VerticalSliderRect.parent == transform;
- bool flag4 = flag && flag2 && flag3;
- this.m_HSliderExpand = (flag4 && this.m_HorizontalSliderRect && this.horizontalSliderVisibility == uGUIScrollRect.SliderVisibility.AutoHideAndExpandViewport);
- this.m_VSliderExpand = (flag4 && this.m_VerticalSliderRect && this.verticalSliderVisibility == uGUIScrollRect.SliderVisibility.AutoHideAndExpandViewport);
- this.m_HSliderHeight = ((!(this.m_HorizontalSliderRect == null)) ? this.m_HorizontalSliderRect.rect.height : 0f);
- this.m_VSliderWidth = ((!(this.m_VerticalSliderRect == null)) ? this.m_VerticalSliderRect.rect.width : 0f);
- }
- protected override void OnEnable()
- {
- base.OnEnable();
- if (this.m_HorizontalSlider)
- {
- this.m_HorizontalSlider.onValueChanged.AddListener(new UnityAction<float>(this.SetHorizontalNormalizedPosition));
- }
- if (this.m_VerticalSlider)
- {
- this.m_VerticalSlider.onValueChanged.AddListener(new UnityAction<float>(this.SetVerticalNormalizedPosition));
- }
- CanvasUpdateRegistry.RegisterCanvasElementForLayoutRebuild(this);
- }
- protected override void OnDisable()
- {
- CanvasUpdateRegistry.UnRegisterCanvasElementForRebuild(this);
- if (this.m_HorizontalSlider)
- {
- this.m_HorizontalSlider.onValueChanged.RemoveListener(new UnityAction<float>(this.SetHorizontalNormalizedPosition));
- }
- if (this.m_VerticalSlider)
- {
- this.m_VerticalSlider.onValueChanged.RemoveListener(new UnityAction<float>(this.SetVerticalNormalizedPosition));
- }
- this.m_HasRebuiltLayout = false;
- this.m_Tracker.Clear();
- this.m_Velocity = Vector2.zero;
- LayoutRebuilder.MarkLayoutForRebuild(this.rectTransform);
- base.OnDisable();
- }
- public override bool IsActive()
- {
- return base.IsActive() && this.m_Content != null;
- }
- private void EnsureLayoutHasRebuilt()
- {
- if (!this.m_HasRebuiltLayout && !CanvasUpdateRegistry.IsRebuildingLayout())
- {
- Canvas.ForceUpdateCanvases();
- }
- }
- public virtual void StopMovement()
- {
- this.m_Velocity = Vector2.zero;
- }
- public virtual void OnScroll(PointerEventData data)
- {
- if (!this.IsActive())
- {
- return;
- }
- this.EnsureLayoutHasRebuilt();
- this.UpdateBounds();
- Vector2 scrollDelta = data.scrollDelta;
- scrollDelta.y *= -1f;
- if (this.vertical && !this.horizontal)
- {
- if (Mathf.Abs(scrollDelta.x) > Mathf.Abs(scrollDelta.y))
- {
- scrollDelta.y = scrollDelta.x;
- }
- scrollDelta.x = 0f;
- }
- if (this.horizontal && !this.vertical)
- {
- if (Mathf.Abs(scrollDelta.y) > Mathf.Abs(scrollDelta.x))
- {
- scrollDelta.x = scrollDelta.y;
- }
- scrollDelta.y = 0f;
- }
- Vector2 vector = this.m_Content.anchoredPosition;
- this.m_Velocity += scrollDelta * this.m_ScrollSensitivity;
- if (this.m_MovementType == uGUIScrollRect.MovementType.Clamped)
- {
- vector += this.CalculateOffset(vector - this.m_Content.anchoredPosition);
- }
- this.SetContentAnchoredPosition(vector);
- this.UpdateBounds();
- }
- public virtual void OnInitializePotentialDrag(PointerEventData eventData)
- {
- if (eventData.button != PointerEventData.InputButton.Left)
- {
- return;
- }
- this.m_Velocity = Vector2.zero;
- }
- public virtual void OnBeginDrag(PointerEventData eventData)
- {
- if (eventData.button != PointerEventData.InputButton.Left)
- {
- return;
- }
- if (!this.IsActive())
- {
- return;
- }
- this.UpdateBounds();
- this.m_PointerStartLocalCursor = Vector2.zero;
- RectTransformUtility.ScreenPointToLocalPointInRectangle(this.viewRect, eventData.position, eventData.pressEventCamera, out this.m_PointerStartLocalCursor);
- this.m_ContentStartPosition = this.m_Content.anchoredPosition;
- this.m_Dragging = true;
- }
- public virtual void OnEndDrag(PointerEventData eventData)
- {
- if (eventData.button != PointerEventData.InputButton.Left)
- {
- return;
- }
- this.m_Dragging = false;
- }
- public virtual void OnDrag(PointerEventData eventData)
- {
- if (eventData.button != PointerEventData.InputButton.Left)
- {
- return;
- }
- if (!this.IsActive())
- {
- return;
- }
- Vector2 a;
- if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(this.viewRect, eventData.position, eventData.pressEventCamera, out a))
- {
- return;
- }
- this.UpdateBounds();
- Vector2 b = a - this.m_PointerStartLocalCursor;
- Vector2 vector = this.m_ContentStartPosition + b;
- Vector2 b2 = this.CalculateOffset(vector - this.m_Content.anchoredPosition);
- vector += b2;
- if (this.m_MovementType == uGUIScrollRect.MovementType.Elastic)
- {
- if (b2.x != 0f)
- {
- vector.x -= uGUIScrollRect.RubberDelta(b2.x, this.m_ViewBounds.size.x);
- }
- if (b2.y != 0f)
- {
- vector.y -= uGUIScrollRect.RubberDelta(b2.y, this.m_ViewBounds.size.y);
- }
- }
- this.SetContentAnchoredPosition(vector);
- }
- protected virtual void SetContentAnchoredPosition(Vector2 position)
- {
- if (!this.m_Horizontal)
- {
- position.x = this.m_Content.anchoredPosition.x;
- }
- if (!this.m_Vertical)
- {
- position.y = this.m_Content.anchoredPosition.y;
- }
- if (position != this.m_Content.anchoredPosition)
- {
- this.m_Content.anchoredPosition = position;
- this.UpdateBounds();
- }
- }
- protected virtual void LateUpdate()
- {
- if (!this.m_Content)
- {
- return;
- }
- this.EnsureLayoutHasRebuilt();
- this.UpdateSliderVisibility();
- this.UpdateBounds();
- float unscaledDeltaTime = Time.unscaledDeltaTime;
- Vector2 vector = this.CalculateOffset(Vector2.zero);
- if (!this.m_Dragging && (vector != Vector2.zero || this.m_Velocity != Vector2.zero))
- {
- Vector2 vector2 = this.m_Content.anchoredPosition;
- for (int i = 0; i < 2; i++)
- {
- if (this.m_MovementType == uGUIScrollRect.MovementType.Elastic && vector[i] != 0f)
- {
- float num = this.m_Velocity[i];
- vector2[i] = Mathf.SmoothDamp(this.m_Content.anchoredPosition[i], this.m_Content.anchoredPosition[i] + vector[i], ref num, this.m_Elasticity, float.PositiveInfinity, unscaledDeltaTime);
- if (Mathf.Abs(num) < 1f)
- {
- num = 0f;
- }
- this.m_Velocity[i] = num;
- }
- else if (this.m_Inertia)
- {
- ref Vector2 ptr = ref this.m_Velocity;
- int index;
- this.m_Velocity[index = i] = ptr[index] * Mathf.Pow(this.m_DecelerationRate, unscaledDeltaTime);
- if (Mathf.Abs(this.m_Velocity[i]) < 1f)
- {
- this.m_Velocity[i] = 0f;
- }
- ptr = ref vector2;
- int index2;
- vector2[index2 = i] = ptr[index2] + this.m_Velocity[i] * unscaledDeltaTime;
- }
- else
- {
- this.m_Velocity[i] = 0f;
- }
- }
- if (this.m_MovementType == uGUIScrollRect.MovementType.Clamped)
- {
- vector = this.CalculateOffset(vector2 - this.m_Content.anchoredPosition);
- vector2 += vector;
- }
- if (this.m_Velocity != Vector2.zero || vector.sqrMagnitude >= 0.02f)
- {
- this.SetContentAnchoredPosition(vector2);
- }
- }
- if (this.m_Dragging && this.m_Inertia)
- {
- Vector3 b = (this.m_Content.anchoredPosition - this.m_PrevPosition) / unscaledDeltaTime;
- this.m_Velocity = Vector3.Lerp(this.m_Velocity, b, unscaledDeltaTime * 10f);
- }
- if (this.m_ViewBounds != this.m_PrevViewBounds || this.m_ContentBounds != this.m_PrevContentBounds || this.m_Content.anchoredPosition != this.m_PrevPosition)
- {
- this.UpdateSliders(vector);
- this.m_OnValueChanged.Invoke(this.normalizedPosition);
- this.UpdatePrevData();
- }
- }
- protected void UpdatePrevData()
- {
- if (this.m_Content == null)
- {
- this.m_PrevPosition = Vector2.zero;
- }
- else
- {
- this.m_PrevPosition = this.m_Content.anchoredPosition;
- }
- this.m_PrevViewBounds = this.m_ViewBounds;
- this.m_PrevContentBounds = this.m_ContentBounds;
- }
- private void UpdateSliders(Vector2 offset)
- {
- if (this.m_HorizontalSlider)
- {
- this.m_HorizontalSlider.normalizedValue = this.horizontalNormalizedPosition;
- }
- if (this.m_VerticalSlider)
- {
- this.m_VerticalSlider.normalizedValue = this.verticalNormalizedPosition;
- }
- }
- public Vector2 normalizedPosition
- {
- get
- {
- return new Vector2(this.horizontalNormalizedPosition, this.verticalNormalizedPosition);
- }
- set
- {
- this.SetNormalizedPosition(value.x, 0);
- this.SetNormalizedPosition(value.y, 1);
- }
- }
- public float horizontalNormalizedPosition
- {
- get
- {
- this.UpdateBounds();
- if (this.m_ContentBounds.size.x <= this.m_ViewBounds.size.x)
- {
- return (float)((this.m_ViewBounds.min.x <= this.m_ContentBounds.min.x) ? 0 : 1);
- }
- return (this.m_ViewBounds.min.x - this.m_ContentBounds.min.x) / (this.m_ContentBounds.size.x - this.m_ViewBounds.size.x);
- }
- set
- {
- this.SetNormalizedPosition(value, 0);
- }
- }
- public float verticalNormalizedPosition
- {
- get
- {
- this.UpdateBounds();
- if (this.m_ContentBounds.size.y <= this.m_ViewBounds.size.y)
- {
- return (float)((this.m_ViewBounds.min.y <= this.m_ContentBounds.min.y) ? 0 : 1);
- }
- return (this.m_ViewBounds.min.y - this.m_ContentBounds.min.y) / (this.m_ContentBounds.size.y - this.m_ViewBounds.size.y);
- }
- set
- {
- this.SetNormalizedPosition(value, 1);
- }
- }
- private void SetHorizontalNormalizedPosition(float value)
- {
- this.SetNormalizedPosition(value, 0);
- }
- private void SetVerticalNormalizedPosition(float value)
- {
- this.SetNormalizedPosition(value, 1);
- }
- protected virtual void SetNormalizedPosition(float value, int axis)
- {
- this.EnsureLayoutHasRebuilt();
- this.UpdateBounds();
- float num = this.m_ContentBounds.size[axis] - this.m_ViewBounds.size[axis];
- float num2 = this.m_ViewBounds.min[axis] - value * num;
- float num3 = this.m_Content.localPosition[axis] + num2 - this.m_ContentBounds.min[axis];
- Vector3 localPosition = this.m_Content.localPosition;
- if (Mathf.Abs(localPosition[axis] - num3) > 0.01f)
- {
- localPosition[axis] = num3;
- this.m_Content.localPosition = localPosition;
- this.m_Velocity[axis] = 0f;
- this.UpdateBounds();
- }
- }
- private static float RubberDelta(float overStretching, float viewSize)
- {
- return (1f - 1f / (Mathf.Abs(overStretching) * 0.55f / viewSize + 1f)) * viewSize * Mathf.Sign(overStretching);
- }
- protected override void OnRectTransformDimensionsChange()
- {
- this.SetDirty();
- }
- private bool hScrollingNeeded
- {
- get
- {
- return !Application.isPlaying || this.m_ContentBounds.size.x > this.m_ViewBounds.size.x + 1f;
- }
- }
- private bool vScrollingNeeded
- {
- get
- {
- return !Application.isPlaying || this.m_ContentBounds.size.y > this.m_ViewBounds.size.y + 1f;
- }
- }
- public virtual void CalculateLayoutInputHorizontal()
- {
- }
- public virtual void CalculateLayoutInputVertical()
- {
- }
- public virtual float minWidth
- {
- get
- {
- return -1f;
- }
- }
- public virtual float preferredWidth
- {
- get
- {
- return -1f;
- }
- }
- public virtual float flexibleWidth
- {
- get
- {
- return -1f;
- }
- }
- public virtual float minHeight
- {
- get
- {
- return -1f;
- }
- }
- public virtual float preferredHeight
- {
- get
- {
- return -1f;
- }
- }
- public virtual float flexibleHeight
- {
- get
- {
- return -1f;
- }
- }
- public virtual int layoutPriority
- {
- get
- {
- return -1;
- }
- }
- public virtual void SetLayoutHorizontal()
- {
- this.m_Tracker.Clear();
- if (this.m_HSliderExpand || this.m_VSliderExpand)
- {
- this.m_Tracker.Add(this, this.viewRect, DrivenTransformProperties.AnchoredPositionX | DrivenTransformProperties.AnchoredPositionY | DrivenTransformProperties.AnchorMinX | DrivenTransformProperties.AnchorMinY | DrivenTransformProperties.AnchorMaxX | DrivenTransformProperties.AnchorMaxY | DrivenTransformProperties.SizeDeltaX | DrivenTransformProperties.SizeDeltaY);
- this.viewRect.anchorMin = Vector2.zero;
- this.viewRect.anchorMax = Vector2.one;
- this.viewRect.sizeDelta = Vector2.zero;
- this.viewRect.anchoredPosition = Vector2.zero;
- LayoutRebuilder.ForceRebuildLayoutImmediate(this.content);
- this.m_ViewBounds = new Bounds(this.viewRect.rect.center, this.viewRect.rect.size);
- this.m_ContentBounds = this.GetBounds();
- }
- if (this.m_VSliderExpand && this.vScrollingNeeded)
- {
- this.viewRect.sizeDelta = new Vector2(-(this.m_VSliderWidth + this.m_VerticalSliderSpacing), this.viewRect.sizeDelta.y);
- LayoutRebuilder.ForceRebuildLayoutImmediate(this.content);
- this.m_ViewBounds = new Bounds(this.viewRect.rect.center, this.viewRect.rect.size);
- this.m_ContentBounds = this.GetBounds();
- }
- if (this.m_HSliderExpand && this.hScrollingNeeded)
- {
- this.viewRect.sizeDelta = new Vector2(this.viewRect.sizeDelta.x, -(this.m_HSliderHeight + this.m_HorizontalSliderSpacing));
- this.m_ViewBounds = new Bounds(this.viewRect.rect.center, this.viewRect.rect.size);
- this.m_ContentBounds = this.GetBounds();
- }
- if (this.m_VSliderExpand && this.vScrollingNeeded && this.viewRect.sizeDelta.x == 0f && this.viewRect.sizeDelta.y < 0f)
- {
- this.viewRect.sizeDelta = new Vector2(-(this.m_VSliderWidth + this.m_VerticalSliderSpacing), this.viewRect.sizeDelta.y);
- }
- }
- public virtual void SetLayoutVertical()
- {
- this.UpdateSliderLayout();
- this.m_ViewBounds = new Bounds(this.viewRect.rect.center, this.viewRect.rect.size);
- this.m_ContentBounds = this.GetBounds();
- }
- private void UpdateSliderVisibility()
- {
- uGUIScrollRect.UpdateOneSliderVisibility(this.vScrollingNeeded, this.m_Vertical, this.m_VerticalSliderVisibility, this.m_VerticalSlider);
- uGUIScrollRect.UpdateOneSliderVisibility(this.hScrollingNeeded, this.m_Horizontal, this.m_HorizontalSliderVisibility, this.m_HorizontalSlider);
- }
- private static void UpdateOneSliderVisibility(bool xScrollingNeeded, bool xAxisEnabled, uGUIScrollRect.SliderVisibility sliderVisibility, Slider slider)
- {
- if (slider)
- {
- if (sliderVisibility == uGUIScrollRect.SliderVisibility.Permanent)
- {
- if (slider.gameObject.activeSelf != xAxisEnabled)
- {
- slider.gameObject.SetActive(xAxisEnabled);
- }
- }
- else if (slider.gameObject.activeSelf != xScrollingNeeded)
- {
- slider.gameObject.SetActive(xScrollingNeeded);
- }
- }
- }
- private void UpdateSliderLayout()
- {
- if (this.m_VSliderExpand && this.m_HorizontalSlider)
- {
- this.m_Tracker.Add(this, this.m_HorizontalSliderRect, DrivenTransformProperties.AnchoredPositionX | DrivenTransformProperties.AnchorMinX | DrivenTransformProperties.AnchorMaxX | DrivenTransformProperties.SizeDeltaX);
- this.m_HorizontalSliderRect.anchorMin = new Vector2(0f, this.m_HorizontalSliderRect.anchorMin.y);
- this.m_HorizontalSliderRect.anchorMax = new Vector2(1f, this.m_HorizontalSliderRect.anchorMax.y);
- this.m_HorizontalSliderRect.anchoredPosition = new Vector2(0f, this.m_HorizontalSliderRect.anchoredPosition.y);
- if (this.vScrollingNeeded)
- {
- this.m_HorizontalSliderRect.sizeDelta = new Vector2(-(this.m_VSliderWidth + this.m_VerticalSliderSpacing), this.m_HorizontalSliderRect.sizeDelta.y);
- }
- else
- {
- this.m_HorizontalSliderRect.sizeDelta = new Vector2(0f, this.m_HorizontalSliderRect.sizeDelta.y);
- }
- }
- if (this.m_HSliderExpand && this.m_VerticalSlider)
- {
- this.m_Tracker.Add(this, this.m_VerticalSliderRect, DrivenTransformProperties.AnchoredPositionY | DrivenTransformProperties.AnchorMinY | DrivenTransformProperties.AnchorMaxY | DrivenTransformProperties.SizeDeltaY);
- this.m_VerticalSliderRect.anchorMin = new Vector2(this.m_VerticalSliderRect.anchorMin.x, 0f);
- this.m_VerticalSliderRect.anchorMax = new Vector2(this.m_VerticalSliderRect.anchorMax.x, 1f);
- this.m_VerticalSliderRect.anchoredPosition = new Vector2(this.m_VerticalSliderRect.anchoredPosition.x, 0f);
- if (this.hScrollingNeeded)
- {
- this.m_VerticalSliderRect.sizeDelta = new Vector2(this.m_VerticalSliderRect.sizeDelta.x, -(this.m_HSliderHeight + this.m_HorizontalSliderSpacing));
- }
- else
- {
- this.m_VerticalSliderRect.sizeDelta = new Vector2(this.m_VerticalSliderRect.sizeDelta.x, 0f);
- }
- }
- }
- protected void UpdateBounds()
- {
- this.m_ViewBounds = new Bounds(this.viewRect.rect.center, this.viewRect.rect.size);
- this.m_ContentBounds = this.GetBounds();
- if (this.m_Content == null)
- {
- return;
- }
- Vector3 size = this.m_ContentBounds.size;
- Vector3 center = this.m_ContentBounds.center;
- Vector2 pivot = this.m_Content.pivot;
- uGUIScrollRect.AdjustBounds(ref this.m_ViewBounds, ref pivot, ref size, ref center);
- this.m_ContentBounds.size = size;
- this.m_ContentBounds.center = center;
- if (this.movementType == uGUIScrollRect.MovementType.Clamped)
- {
- Vector3 zero = Vector3.zero;
- if (this.m_ViewBounds.max.x > this.m_ContentBounds.max.x)
- {
- zero.x = Math.Min(this.m_ViewBounds.min.x - this.m_ContentBounds.min.x, this.m_ViewBounds.max.x - this.m_ContentBounds.max.x);
- }
- else if (this.m_ViewBounds.min.x < this.m_ContentBounds.min.x)
- {
- zero.x = Math.Max(this.m_ViewBounds.min.x - this.m_ContentBounds.min.x, this.m_ViewBounds.max.x - this.m_ContentBounds.max.x);
- }
- if (this.m_ViewBounds.min.y < this.m_ContentBounds.min.y)
- {
- zero.y = Math.Max(this.m_ViewBounds.min.y - this.m_ContentBounds.min.y, this.m_ViewBounds.max.y - this.m_ContentBounds.max.y);
- }
- else if (this.m_ViewBounds.max.y > this.m_ContentBounds.max.y)
- {
- zero.y = Math.Min(this.m_ViewBounds.min.y - this.m_ContentBounds.min.y, this.m_ViewBounds.max.y - this.m_ContentBounds.max.y);
- }
- if (zero.sqrMagnitude > 0.01f)
- {
- this.m_ContentBounds = this.GetBounds();
- size = this.m_ContentBounds.size;
- center = this.m_ContentBounds.center;
- pivot = this.m_Content.pivot;
- uGUIScrollRect.AdjustBounds(ref this.m_ViewBounds, ref pivot, ref size, ref center);
- this.m_ContentBounds.size = size;
- this.m_ContentBounds.center = center;
- }
- }
- }
- internal static void AdjustBounds(ref Bounds viewBounds, ref Vector2 contentPivot, ref Vector3 contentSize, ref Vector3 contentPos)
- {
- Vector3 vector = viewBounds.size - contentSize;
- if (vector.x > 0f)
- {
- contentPos.x -= vector.x * (contentPivot.x - 0.5f);
- contentSize.x = viewBounds.size.x;
- }
- if (vector.y > 0f)
- {
- contentPos.y -= vector.y * (contentPivot.y - 0.5f);
- contentSize.y = viewBounds.size.y;
- }
- }
- private Bounds GetBounds()
- {
- if (this.m_Content == null)
- {
- return default(Bounds);
- }
- this.m_Content.GetWorldCorners(this.m_Corners);
- Matrix4x4 worldToLocalMatrix = this.viewRect.worldToLocalMatrix;
- return uGUIScrollRect.InternalGetBounds(this.m_Corners, ref worldToLocalMatrix);
- }
- internal static Bounds InternalGetBounds(Vector3[] corners, ref Matrix4x4 viewWorldToLocalMatrix)
- {
- Vector3 vector = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
- Vector3 vector2 = new Vector3(float.MinValue, float.MinValue, float.MinValue);
- for (int i = 0; i < 4; i++)
- {
- Vector3 lhs = viewWorldToLocalMatrix.MultiplyPoint3x4(corners[i]);
- vector = Vector3.Min(lhs, vector);
- vector2 = Vector3.Max(lhs, vector2);
- }
- Bounds result = new Bounds(vector, Vector3.zero);
- result.Encapsulate(vector2);
- return result;
- }
- private Vector2 CalculateOffset(Vector2 delta)
- {
- return uGUIScrollRect.InternalCalculateOffset(ref this.m_ViewBounds, ref this.m_ContentBounds, this.m_Horizontal, this.m_Vertical, this.m_MovementType, ref delta);
- }
- internal static Vector2 InternalCalculateOffset(ref Bounds viewBounds, ref Bounds contentBounds, bool horizontal, bool vertical, uGUIScrollRect.MovementType movementType, ref Vector2 delta)
- {
- Vector2 zero = Vector2.zero;
- if (movementType == uGUIScrollRect.MovementType.Unrestricted)
- {
- return zero;
- }
- Vector2 vector = contentBounds.min;
- Vector2 vector2 = contentBounds.max;
- if (horizontal)
- {
- vector.x += delta.x;
- vector2.x += delta.x;
- if (vector.x > viewBounds.min.x)
- {
- zero.x = viewBounds.min.x - vector.x;
- }
- else if (vector2.x < viewBounds.max.x)
- {
- zero.x = viewBounds.max.x - vector2.x;
- }
- }
- if (vertical)
- {
- vector.y += delta.y;
- vector2.y += delta.y;
- if (vector2.y < viewBounds.max.y)
- {
- zero.y = viewBounds.max.y - vector2.y;
- }
- else if (vector.y > viewBounds.min.y)
- {
- zero.y = viewBounds.min.y - vector.y;
- }
- }
- return zero;
- }
- protected void SetDirty()
- {
- if (!this.IsActive())
- {
- return;
- }
- LayoutRebuilder.MarkLayoutForRebuild(this.rectTransform);
- }
- protected void SetDirtyCaching()
- {
- if (!this.IsActive())
- {
- return;
- }
- CanvasUpdateRegistry.RegisterCanvasElementForLayoutRebuild(this);
- LayoutRebuilder.MarkLayoutForRebuild(this.rectTransform);
- }
- Transform ICanvasElement.get_transform()
- {
- return base.transform;
- }
- bool ICanvasElement.IsDestroyed()
- {
- return base.IsDestroyed();
- }
- [SerializeField]
- private RectTransform m_Content;
- [SerializeField]
- private bool m_Horizontal = true;
- [SerializeField]
- private bool m_Vertical = true;
- [SerializeField]
- private uGUIScrollRect.MovementType m_MovementType = uGUIScrollRect.MovementType.Elastic;
- [SerializeField]
- private float m_Elasticity = 0.1f;
- [SerializeField]
- private bool m_Inertia = true;
- [SerializeField]
- private float m_DecelerationRate = 0.135f;
- [SerializeField]
- private float m_ScrollSensitivity = 1f;
- [SerializeField]
- private RectTransform m_Viewport;
- [SerializeField]
- private Slider m_HorizontalSlider;
- [SerializeField]
- private Slider m_VerticalSlider;
- [SerializeField]
- private uGUIScrollRect.SliderVisibility m_HorizontalSliderVisibility;
- [SerializeField]
- private uGUIScrollRect.SliderVisibility m_VerticalSliderVisibility;
- [SerializeField]
- private float m_HorizontalSliderSpacing;
- [SerializeField]
- private float m_VerticalSliderSpacing;
- [SerializeField]
- private uGUIScrollRect.ScrollRectEvent m_OnValueChanged = new uGUIScrollRect.ScrollRectEvent();
- private Vector2 m_PointerStartLocalCursor = Vector2.zero;
- protected Vector2 m_ContentStartPosition = Vector2.zero;
- private RectTransform m_ViewRect;
- protected Bounds m_ContentBounds;
- private Bounds m_ViewBounds;
- private Vector2 m_Velocity;
- private bool m_Dragging;
- private Vector2 m_PrevPosition = Vector2.zero;
- private Bounds m_PrevContentBounds;
- private Bounds m_PrevViewBounds;
- [NonSerialized]
- private bool m_HasRebuiltLayout;
- private bool m_HSliderExpand;
- private bool m_VSliderExpand;
- private float m_HSliderHeight;
- private float m_VSliderWidth;
- [NonSerialized]
- private RectTransform m_Rect;
- private RectTransform m_HorizontalSliderRect;
- private RectTransform m_VerticalSliderRect;
- private DrivenRectTransformTracker m_Tracker;
- private readonly Vector3[] m_Corners = new Vector3[4];
- public enum MovementType
- {
- Unrestricted,
- Elastic,
- Clamped
- }
- public enum SliderVisibility
- {
- Permanent,
- AutoHide,
- AutoHideAndExpandViewport
- }
- [Serializable]
- public class ScrollRectEvent : UnityEvent<Vector2>
- {
- }
- }
- }
|