[Unity Practical Combat] UGUI-ScrollRect component list -> Code forces scrolling to the bottom - [Solution to ChatGPT answer verticalNormalizedPosition = 0 does not work]

Unity officially introduces the ScrollRect component

Unity official website introduces the ScrollRect component.
Scroll Rect (Scroll Rect)
When content that takes up a lot of space needs to be displayed in a small area, the scroll rectangle can be used. The scrolling rectangle provides the functionality to scroll this content.

Typically, a scroll rectangle is combined with a mask to create a scroll view. In the resulting view, only the scrollable content within the scroll rectangle is visible. In addition, a scrolling rectangle can be combined with one or two scrollbars (Scrollbars) that can be dragged for horizontal or vertical scrolling.

Scroll rectangle.
Scroll rectangle.

Properties
Properties: Function:
Content This is a reference to the rectangular transform of a UI element that requires scrolling (such as a large image).
Horizontal Enables horizontal scrolling
Vertical Enables vertical scrolling
Movement Type Unrestricted, Elastic, or Clamped. Use Elastic or Clamped to force content to stay within the bounds of the scrolling rectangle. Elastic mode bounces content when it reaches the edge of the scroll rectangle
Elasticity This is the amount of bounce used in elastic mode.
Inertia If Inertia is set, content will continue to move when the pointer is dragged and released. If Inertia is not set, the content will only move when dragging.
Deceleration Rate When Inertia is set, the deceleration rate (Deceleration Rate) determines how fast the content stops moving. A rate of 0 will stop movement immediately. A value of 1 means movement never slows down.
Scroll Sensitivity Sensitivity to wheel and trackpad scroll events.
Viewport A reference to the viewport rectangle transform that is the parent of the content rectangle transform.
Horizontal Scrollbar Reference to the horizontal scroll bar element (optional).
Visibility Whether the scrollbar should automatically hide when not needed and (optionally) also expand the viewport.
Spacing The space between the scroll bar and the viewport.
Vertical Scrollbar Reference to the vertical scroll bar element (optional).
Visibility Whether the scrollbar should automatically hide when not needed and (optionally) also expand the viewport.
Spacing The space between the scroll bar and the viewport.
Event
Properties: Function:
On Value Changed UnityEvent called when the scroll position of the scroll rectangle changes. This event sends the current scroll position as a dynamic parameter of type Vector2.
Details
The important elements in a scroll view include the viewport, the scrolling content, and optionally one or two scroll bars.

The root game object has a scrolling rectangle component.
Viewports have mask components. The viewport can be the root game object or a separate game object that is a child of the root. If using auto-hiding scrollbars, the viewport must be a child. The viewport rectangle transformation needs to be referenced in the Viewport property of the scroll rectangle.
All scrolling content must be children of a single content game object that is a child of the viewport. The content rectangle transformation needs to be referenced in the Content property of the scroll rectangle.
Scrollbars (if used) are children of the root game object. See the Scroll Bars page for more details on scroll bar settings, and see the Scroll Bar Settings section below for information on scroll bar settings for scroll views.
The following image shows a setting where the viewport is a child of the scroll view's root node. This setting is adopted by default when using the GameObject > UI > Scroll View menu option.
Insert image description here

To scroll content, input must be received from within the ScrollRect's bounds rather than from the content itself.

Be careful when using the Unrestricted move type, as you may irretrievably lose control of your content. When using Elastic or Constrained move types, it is best to adjust the content position to ensure that it starts moving within the bounds of the ScrollRect, otherwise unexpected behavior may occur when the RectTransform attempts to restore the content to within its bounds.

Scroll Bar Settings
Optionally link a scroll rectangle to a horizontal and/or vertical scroll bar. These controls are typically placed in hierarchy views as siblings of the viewport, and when present, should be dragged into the Horizontal Scrollbar and Vertical Scrollbar properties of the scroll rectangle, respectively. Note that the Direction property should be set to Left To Right on such horizontal scroll bars and to Bottom To Top on vertical scroll bars.

If you don't need to scroll the content because the scroll bar does not exceed the viewport, you can choose to have the scroll bar hide automatically. Note that auto-hide only happens in play mode. In edit mode, scroll bars are always displayed. This prevents scenes from being marked as "dirty" when they shouldn't, and also helps create content that still has space even when scrollbars are shown.

If the visibility behavior of one or both scroll bars is set to Auto Hide And Expand View, the viewport is automatically expanded when the scroll bars are hidden, taking up the extra space originally used by the scroll bars. If you use this setting, the position and size of the view will be determined by the scroll rectangle, and the width of the horizontal scroll bar and the height of the vertical scroll bar will also be determined by the scroll rectangle. When using this setting, both the viewport and the scrollbars must be children of the scrolling rectangle's root game object.

Tip
Use the content RectTransform's pivot and anchor points to determine the alignment of the content within the scroll view as it scales. If the content should remain aligned to the top, set the anchor point to the top of the parent and set the pivot to the top position.
See the Size UI elements to fit their content page to learn how to make the content rectangle transform automatically resize to fit the content.

1. Asked ChatGPT, the answer is very simple

Insert image description here

using UnityEngine;
using UnityEngine.UI;

public class Test : MonoBehaviour
{
    public ScrollRect scrollRect;

    void Start()
    {
        scrollRect.verticalNormalizedPosition = 0;
    }

}


2. But I found that there is no solution. Sometimes it is right on top, sometimes it is not the bottom.

3. Finally, this problem was solved by manually refreshing Canvas, Grid Layout Group and Content Size Fitter.

using UnityEngine;
using UnityEngine.UI;

public class Test : MonoBehaviour
{
    public ScrollRect scrollRect;

    void Start()
    {
        Canvas.ForceUpdateCanvases();
		scrollRect.content.GetComponent<VerticalLayoutGroup>().CalculateLayoutInputVertical();
		scrollRect.content.GetComponent<ContentSizeFitter>().SetLayoutVertical();
		scrollRect.verticalNormalizedPosition = 0;
    }
}

Guess you like

Origin blog.csdn.net/aaaadong/article/details/129997491