Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sally_hoeg · Feb 05, 2015 at 12:41 PM · touchscreenscrollrectsnap

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges