Untitled

 avatar
unknown
plain_text
2 months ago
3.5 kB
8
Indexable
        private void UpdateReferences() {
            // update current selected rect transform
            if (CurrentSelectedGameObject != LastCheckedGameObject) {
                CurrentTargetRectTransform = (CurrentSelectedGameObject != null) ?
                    CurrentSelectedGameObject.GetComponent<RectTransform>() :
                    null;

                // unlock automatic scrolling
                if (CurrentSelectedGameObject != null &&
                    CurrentSelectedGameObject.transform.parent == LayoutListGroup.transform) {
                    IsManualScrollingAvailable = false;
                }
            }

            LastCheckedGameObject = CurrentSelectedGameObject;
        }

        private void ScrollRectToLevelSelection() {
            // check main references
            bool referencesAreIncorrect = (TargetScrollRect == null || LayoutListGroup == null || ScrollWindow == null);

            if (referencesAreIncorrect == true || IsManualScrollingAvailable == true) {
                return;
            }

            RectTransform selection = CurrentTargetRectTransform ;

            // check if scrolling is possible
            if (selection == null || selection.transform.parent != LayoutListGroup.transform) {
                return;
            }

            // depending on selected scroll direction move the scroll rect to selection
            switch (ScrollDirection) {
                case ScrollType.VERTICAL:
                    UpdateVerticalScrollPosition(selection);
                    break;
                case ScrollType.HORIZONTAL:
                    UpdateHorizontalScrollPosition(selection);
                    break;
                case ScrollType.BOTH:
                    UpdateVerticalScrollPosition(selection);
                    UpdateHorizontalScrollPosition(selection);
                    break;
            }
        }

        private void UpdateVerticalScrollPosition(RectTransform selection) {
            // move the current scroll rect to correct position
            float selectionPosition = -selection.anchoredPosition.y - (selection.rect.height * (1 - selection.pivot.y));

            float elementHeight = selection.rect.height;
            float maskHeight = ScrollWindow.rect.height;
            float listAnchorPosition = LayoutListGroup.anchoredPosition.y;

            // get the element offset value depending on the cursor move direction
            float offlimitsValue = GetScrollOffset(selectionPosition, listAnchorPosition, elementHeight, maskHeight);

            //calc the norm position based on the target height 
            float normalizedPosition = TargetScrollRect.verticalNormalizedPosition + (offlimitsValue / LayoutListGroup.rect.height);

            if(offlimitsValue < 0) {
                //scrolling up
                normalizedPosition -=(Mathf.Abs(offlimitsValue) / LayoutListGroup.rect.height);
            }
            else if (offlimitsValue > 0) {
                //scrolling down 
                normalizedPosition += (offlimitsValue / LayoutListGroup.rect.height);
            }
            //clamp the norm position 
            normalizedPosition = Mathf.Clamp01(normalizedPosition);

            //smoothly move 
            TargetScrollRect.verticalNormalizedPosition = Mathf.SmoothStep(TargetScrollRect.verticalNormalizedPosition, normalizedPosition, Time.unscaledDeltaTime * scrollSpeed);
        }
Editor is loading...
Leave a Comment