- Home /
scrollrect jump to screen
hey guys. i've made a scrollrect in my scene and used a script to snap the pages. this works fine. but now i want to jump to a certain page with a button click. e.g. when i'm on page 3, i want to press the button to jump on page 10. hope you can help me, i'm getting crazy with this.
this is the script i used to snap the pages:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
[RequireComponent(typeof(ScrollRect))]
public class ScrollRectSnap : MonoBehaviour
{
[Tooltip("the container the screens or pages belong to")]
public Transform ScreensContainer;
[Tooltip("how many screens or pages are there within the content")]
public int Screens = 1;
[Tooltip("which screen or page to start on")]
public int StartingScreen = 1;
private List<Vector3> m_Positions;
private ScrollRect m_ScrollRect;
private Vector3 m_LerpTarget;
private bool m_Lerp;
private RectTransform m_ScrollViewRectTrans;
void Start()
{
m_ScrollRect = gameObject.GetComponent<ScrollRect>();
m_ScrollViewRectTrans = gameObject.GetComponent<RectTransform>();
m_ScrollRect.inertia = false;
m_Lerp = false;
m_Positions = new List<Vector3>();
if (Screens > 0)
{
Vector3 startPos = ScreensContainer.localPosition;
Vector3 endPos = ScreensContainer.localPosition + Vector3.left * ((Screens - 1) * m_ScrollViewRectTrans.rect.width);
for (int i = 0; i < Screens; ++i)
{
float horiNormPos = (float) i / (float)(Screens - 1);
// this does not seem to have an effect [Tested on Unity 4.6.0 RC 2]
m_ScrollRect.horizontalNormalizedPosition = horiNormPos;
m_Positions.Add(Vector3.Lerp(startPos, endPos, horiNormPos));
}
}
// this does not seem to have an effect [Tested on Unity 4.6.0 RC 2]
m_ScrollRect.horizontalNormalizedPosition = (float)(StartingScreen - 1) / (float)(Screens - 1);
}
void FixedUpdate()
{
if (m_Lerp)
{
ScreensContainer.localPosition = Vector3.Lerp(ScreensContainer.localPosition, m_LerpTarget, 10 * Time.deltaTime);
if (Vector3.Distance(ScreensContainer.localPosition, m_LerpTarget) < 0.001f)
{
m_Lerp = false;
}
}
}
/// <summary>
/// Bind this to UnityEditor Event trigger Pointer Up
/// </summary>
public void DragEnd()
{
if (m_ScrollRect.horizontal)
{
m_Lerp = true;
m_LerpTarget = FindClosestFrom(ScreensContainer.localPosition, m_Positions);
}
}
/// <summary>
/// Bind this to UnityEditor Event trigger Drag
/// </summary>
public void OnDrag()
{
m_Lerp = false;
}
Vector3 FindClosestFrom(Vector3 start, List<Vector3> positions)
{
Vector3 closest = Vector3.zero;
float distance = Mathf.Infinity;
foreach (Vector3 position in m_Positions)
{
if (Vector3.Distance(start, position) < distance)
{
distance = Vector3.Distance(start, position);
closest = position;
}
}
return closest;
}
}
i also added an event trigger.
i really hope you can help me! thank you.
Comment
Your answer
Follow this Question
Related Questions
Make a Triangle instead of a "Rect" 1 Answer
Scroll Rect Snapping 0 Answers
Possible to return the names of gameobjects inside a rect? 1 Answer
rect.Contains problem? 2 Answers
Buttons less responsive inside scroll rect with touch controls (UGUI) 2 Answers