/// <summary> /// Tweenコルーチン. /// </summary> IEnumerator _CoScrollTweening(Tweening.TweenMethod tweenType, float time, float startValue, float endValue) { // Tween中はScrollRect自体の動作(inertia/movementType)を制限します. handler.scrollRect.velocity = Vector2.zero; _inertia = handler.scrollRect.inertia; _movementType = handler.scrollRect.movementType; handler.scrollRect.inertia = false; handler.scrollRect.movementType = ScrollRect.MovementType.Unrestricted; bool positive = 0 < (endValue - startValue); if (tweenType == Tweening.TweenMethod.immediate || time <= 0) { handler.OnChangeTweenPosition(endValue, positive); yield return(null); } else { // Tweenを実行します. float unscaleTimer = 0; while (unscaleTimer < time) { handler.OnChangeTweenPosition(Tweening.GetTweenValue(tweenType, startValue, endValue, unscaleTimer / time), positive); unscaleTimer += Time.unscaledDeltaTime; yield return(null); } } // Tweenを停止します. handler.OnChangeTweenPosition(endValue, positive); _StopScrollTween(); yield break; }
protected virtual void Awake() { ((UIBehaviour)this).Awake(); if (Object.op_Inequality((Object)this.get_verticalScrollbar(), (Object)null)) { this.get_verticalScrollbar().set_value(1f); } this.defalt_movement_type = this.get_movementType(); }
public void OnEndDrag(PointerEventData eventData) { _targetPos = FindClosestPos(_container.localPosition.x); ScrollRect.MovementType preType = _scrollView.movementType; _scrollView.movementType = ScrollRect.MovementType.Unrestricted; _rtContainer.DOAnchorPosX(_targetPos, 0.5f).OnComplete(() => { _scrollView.movementType = preType; }); }
public void Init() { if (tempCell == null) { Debug.LogError("tempCell不能为空!"); return; } this.inited = true; this.tempCell.SetActive(false); this.scrollRect = gameObject.AddComponent <ScrollRect>(); this.scrollRect.vertical = false; this.scrollRect.horizontal = true; this.scrollRect.verticalScrollbar = horizontalScrollbar; this.scrollRect.movementType = movementType; this.scrollRect.scrollSensitivity = 30; viewport = new GameObject("viewport", typeof(RectTransform)); viewport.transform.SetParent(transform); this.scrollRect.viewport = viewport.GetComponent <RectTransform>(); content = new GameObject("content", typeof(RectTransform)); content.transform.SetParent(viewport.transform); this.scrollRect.content = content.GetComponent <RectTransform>(); this.scrollRect.viewport.localScale = Vector3.one; this.scrollRect.viewport.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0); this.scrollRect.viewport.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0); this.scrollRect.viewport.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, 0); this.scrollRect.viewport.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 0, 0); this.scrollRect.viewport.anchorMin = Vector2.zero; this.scrollRect.viewport.anchorMax = Vector2.one; this.scrollRect.viewport.anchoredPosition3D = Vector3.zero; this.scrollRect.viewport.eulerAngles = Vector3.zero; this.scrollRect.viewport.gameObject.AddComponent <Mask>().showMaskGraphic = false; Image image = this.scrollRect.viewport.gameObject.AddComponent <Image>(); Rect viewRect = this.scrollRect.viewport.rect; image.sprite = Sprite.Create(new Texture2D(1, 1), new Rect(Vector2.zero, Vector2.one), Vector2.zero); Rect tempRect = tempCell.GetComponent <RectTransform>().rect; this.cellWidth = tempRect.width; this.cellHeight = tempRect.height; this.row = Mathf.FloorToInt(this.scrollRect.viewport.rect.height / this.cellHeight); this.row = Mathf.Max(1, this.row); this.col = Mathf.CeilToInt(this.scrollRect.viewport.rect.width / this.cellWidth); this.scrollRect.content.localScale = Vector3.one; this.scrollRect.content.offsetMax = new Vector2(0, 0); this.scrollRect.content.offsetMin = new Vector2(0, 0); this.scrollRect.content.anchorMin = Vector2.zero; this.scrollRect.content.anchorMax = Vector2.one; this.scrollRect.onValueChanged.AddListener(this.OnValueChange); this.CreateCells(); }
private static void TestCalculateOffset(ScrollRect.MovementType movementType, bool horizontal, bool vertical, int viewX, float viewY, float resX, float resY, Bounds contentBounds) { Bounds viewBounds = new Bounds(new Vector3(viewX, viewY), new Vector3(2, 2)); // content is south east of view Vector2 delta = Vector2.zero; object[] arguments = new object[] { viewBounds, contentBounds, horizontal, vertical, movementType, delta }; MethodInfo method = typeof(ScrollRect).GetMethod("InternalCalculateOffset", BindingFlags.NonPublic | BindingFlags.Static); var result = (Vector2)method.Invoke(null, arguments); Console.WriteLine(result); Assert.AreEqual(new Vector2(resX, resY), result); }
public void OnScrollClampedShouldClampContentAnchoredPosition(ScrollRect.MovementType movementType, float anchoredPosX, float anchoredPosY) { ScrollRect scrollRect = m_PrefabRoot.GetComponentInChildren <ScrollRect>(); Vector2 scrollDelta = new Vector2(50, -50); scrollRect.movementType = movementType; scrollRect.content.anchoredPosition = new Vector2(2.5f, 2.5f); scrollRect.OnScroll(new PointerEventData(m_PrefabRoot.GetComponentInChildren <EventSystem>()) { scrollDelta = scrollDelta }); Assert.AreEqual(new Vector2(anchoredPosX, anchoredPosY), scrollRect.content.anchoredPosition); }
public void Initialize() { if (IsInitialized) { return; } infiniteCell = cellPrefab.GetComponent <InfiniteCell <T> >(); scrollRect = GetComponent <ScrollRect>(); scrollRect.onValueChanged.AddListener(OnValueChanged); for (int i = 0; i < cellPoolSize; i++) { var newCell = Instantiate(infiniteCell, scrollRect.content); newCell.gameObject.SetActive(false); cellPool.Enqueue(newCell); } movementType = scrollRect.movementType; IsInitialized = true; }
internal static Vector2 InternalCalculateOffset(ref Bounds viewBounds, ref Bounds contentBounds, bool horizontal, bool vertical, ScrollRect.MovementType movementType, ref Vector2 delta) { Vector2 offset = Vector2.zero; if (movementType == ScrollRect.MovementType.Unrestricted) { return(offset); } Vector2 min = contentBounds.min; Vector2 max = contentBounds.max; if (horizontal) { min.x += delta.x; max.x += delta.x; if (min.x > viewBounds.min.x) { offset.x = viewBounds.min.x - min.x; } else if (max.x < viewBounds.max.x) { offset.x = viewBounds.max.x - max.x; } } if (vertical) { min.y += delta.y; max.y += delta.y; if (max.y < viewBounds.max.y) { offset.y = viewBounds.max.y - max.y; } else if (min.y > viewBounds.min.y) { offset.y = viewBounds.min.y - min.y; } } return(offset); }
internal static Vector2 InternalCalculateOffset(ref Bounds viewBounds, ref Bounds contentBounds, bool horizontal, bool vertical, ScrollRect.MovementType movementType, ref Vector2 delta) { Vector2 zero = Vector2.zero; Vector2 result; if (movementType == ScrollRect.MovementType.Unrestricted) { result = zero; } else { 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; } } result = zero; } return(result); }
public void Initiate(DynamicScrollRect scrollRect, IList <T> infoList, int startIndex, GameObject objReference, bool createMoreIfNeeded = true, int?forceAmount = null) { ScrollRect = scrollRect; if (ScrollRect == null) { throw new Exception("No scroll rect in gameObject."); } if (objReference == null) { throw new Exception("No Reference GameObject setted."); } this.infoList = infoList; ScrollRect.onValueChanged.AddListener(OnScroll); ScrollRect.onBeginDrag.AddListener(OnBeginDrag); ScrollRect.onEndDrag.AddListener(OnEndDrag); ScrollRect.onStopMoving.AddListener(OnStopMoving); mMovementType = ScrollRect.movementType; ScrollRect.realMovementType = ScrollRect.movementType; ScrollRect.movementType = UnityEngine.UI.ScrollRect.MovementType.Unrestricted; if (ScrollRect.content.GetComponent <VerticalLayoutGroup>() != null) { mVerticalLayoutGroup = ScrollRect.content.GetComponent <VerticalLayoutGroup>(); mVerticalLayoutGroup.spacing = spacing; } if (ScrollRect.content.GetComponent <HorizontalLayoutGroup>() != null) { mHorizontalLayoutGroup = ScrollRect.content.GetComponent <HorizontalLayoutGroup>(); mHorizontalLayoutGroup.spacing = spacing; } if (ScrollRect.content.GetComponent <GridLayoutGroup>() != null) { mGridLayoutGroup = ScrollRect.content.GetComponent <GridLayoutGroup>(); mGridLayoutGroup.spacing = new Vector2(spacing, spacing); } if (ScrollRect.content.GetComponent <ContentSizeFitter>() != null) { mContentSizeFitter = ScrollRect.content.GetComponent <ContentSizeFitter>(); } mIsHorizontal = ScrollRect.horizontal; mIsVertical = ScrollRect.vertical; objectPool.createMoreIfNeeded = createMoreIfNeeded; objectPool.Initialize(forceAmount ?? 0, objReference, ScrollRect.content); CreateList(startIndex); objectPool.ForEach(x => { x.SetRefreshListAction(RefreshPosition); x.OnObjectIsNotCentralized(); }); SetCentralizedObject(); DisableGridComponents(); if (!mIsHorizontal || !mIsVertical) { return; } Debug.LogError("DynamicScroll doesn't support scrolling in both directions, please choose one direction (horizontal or vertical)"); mIsHorizontal = false; }