12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103 |
- 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)
- {
- int index;
- this.m_Velocity[index = i] = this.m_Velocity[index] * Mathf.Pow(this.m_DecelerationRate, unscaledDeltaTime);
- if (Mathf.Abs(this.m_Velocity[i]) < 1f)
- {
- this.m_Velocity[i] = 0f;
- }
- int index2;
- vector2[index2 = i] = vector2[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>
- {
- }
- }
- }
|