INVOKING: Scroll View- On Value Changed (Vector 2)
I want to invoke my "On Value Change" via Script at "void Start"... But i don't know how to get current Vector 2 of the Scroll Rect.... PLEASE HELP.... Where is Vector 2 (It's range from 0 to 1, and unfortunately i have no idea where that Vector exist). PLEASE HELP..
Answer by Sylker · Sep 11, 2021 at 03:03 PM
The vector2 will be passed by the ScrollRect and contains values from both scrollbars: horizontal and vertical.
I don't think it's a good idea to use this at Start. You have to create a public void like this:
public void OnScrollValueChange(Vector2 value)
{
// do your stuff here
}
You may want to set this method to the ScrollRect component in the Editor or via script at Start:
public ScrollRect myScroll;
void Start()
{
myScroll.onValueChanged.AddListener(OnScrollValueChange);
}
I like to use the scrollbar directly for it passes the float value instead of Vector2. In that case, your code should look like this:
using UnityEngine;
using UnityEngine.UI;
public class MyScrollClass : MonoBehaviour
{
public Scrollbar scrollbar;
// Start is called before the first frame update
void Start()
{
scrollbar.onValueChanged.AddListener(OnScrollValueChange);
}
public void OnScrollValueChange(float value)
{
if (value == 0)
{
// This is the end of the scroll
}
}
Don't forget to add the scrollbar to your component in Editor.
Note that to top value is 1 and bottom is 0, what means the user will reach the end of the scrollbar at zero.
Hope that helps.