public void SetSelectedTrans(Transform trans) { if (trans == null) { return; } _seletedTrans = trans; _scrollState = EScrollState.ESS_Decelerate; }
public void StartScroll() { _scrollState = EScrollState.ESS_Scroll; bRunLateUpdated = true; //resort child sibling foreach (var obj in siblingIndex) { obj.Key.SetSiblingIndex(obj.Value); } }
protected virtual void LateUpdate() { if (!m_Content || !bRunLateUpdated) { return; } EnsureLayoutHasRebuilt(); UpdateBounds(); float deltaTime = Time.smoothDeltaTime; Vector2 offset = CalculateOffset(Vector2.zero); if ((offset != Vector2.zero || m_Velocity != Vector2.zero) && (_scrollState == EScrollState.ESS_Scroll || _scrollState == EScrollState.ESS_Decelerate)) { Vector2 position = m_Content.anchoredPosition; for (int axis = 0; axis < 2; axis++) { // Apply spring physics if movement is elastic and content has an offset from the view. if (m_MovementType == MovementType.Elastic && offset[axis] != 0) { float speed = m_Velocity[axis]; position[axis] = Mathf.SmoothDamp(m_Content.anchoredPosition[axis], m_Content.anchoredPosition[axis] + offset[axis], ref speed, m_Elasticity, Mathf.Infinity, deltaTime); if (_scrollState == EScrollState.ESS_Decelerate) { m_Velocity[axis] = speed; } } // Else move content according to velocity with deceleration applied. else if (m_Inertia) { if (_scrollState == EScrollState.ESS_Decelerate) { m_Velocity[axis] *= Mathf.Pow(m_DecelerationRate, deltaTime); if (Mathf.Abs(m_Velocity[axis]) < 1) { m_Velocity[axis] = 0; } } position[axis] += m_Velocity[axis] * deltaTime; } // If we have neither elaticity or friction, there shouldn't be any velocity. else { if (_scrollState == EScrollState.ESS_Decelerate) { m_Velocity[axis] = 0; } } } if (m_Velocity != Vector2.zero) { if (m_MovementType == MovementType.Clamped) { offset = CalculateOffset(position - m_Content.anchoredPosition); position += offset; } SetContentAnchoredPosition(position); } } //scroll ending animation if (_scrollState == EScrollState.ESS_EndedAnim && m_Content.anchoredPosition.y > 0) { float rest = m_Content.anchoredPosition.y - m_fEndingAnimSpeed; if (rest <= 0) { rest = 0; _scrollState = EScrollState.ESS_MAX; if (m_onStopScroll != null) { bRunLateUpdated = false; m_onStopScroll.Invoke(_curFocuesdTrans); } } Vector2 position = new Vector2(0, rest); SetContentAnchoredPosition(position); } //scroll ending if (_scrollState == EScrollState.ESS_Ended) { PostScroll(); _scrollState = EScrollState.ESS_EndedAnim; } //decelerating if (_scrollState == EScrollState.ESS_Decelerate && m_Velocity.y <= m_fEndSpeed) { _scrollState = EScrollState.ESS_Ended; float totalDis = CalculateMoveDistance(); Vector2 position = m_Content.anchoredPosition; position.y += totalDis; _scrollState = EScrollState.ESS_Ended; m_Velocity = new Vector2(0, 0); SetContentAnchoredPosition(position); } if (m_ViewBounds != m_PrevViewBounds || m_ContentBounds != m_PrevContentBounds || m_Content.anchoredPosition != m_PrevPosition) { // UpdateScrollbars(offset); m_OnValueChanged.Invoke(normalizedPosition); UpdatePrevData(); } }