Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 TheShadyColombian · Mar 02, 2017 at 03:59 PM · uiscrollviewsnapscrollbar

Smooth scroll snap?

I currently have my menu working almost perfectly except I want to enable smooth scroll snapping to each element in the menu. I currently have a script that lerps the scrollbar value to the nearest snap point when the user isn't scrolling but although this is quite clean, it makes the user have to drag a distance bigger than is comfortable, and I just want a swipe to take them to the next one (it's not necessary for a big swipe to take them multiple items down, but that would be nice)

Below is the script I'm using right now, and a demo I found that is exactly what I want (but it's CSS so not really transferable):

What I want: http://codepen.io/sdras/pen/43c9d13b23bc34a85bb3a5e2ea985958

This script is attached to both the scrollbar to control the value, and the ScrollRect to get access to whether it is scrolling or not, and a static value to sync the values between instances:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class UI_SmoothScrollSnap : MonoBehaviour, IBeginDragHandler, IEndDragHandler {
 
     public float SnapSpeed = 10;
     public float ScrollVelocity;

     [ShowInInspector] //I added this outside of my IDE so not sure if it's the right attribute
     private bool isDragging;
     public int Snaps;
     public static bool Dragging;
 
     private Scrollbar scroll;
     private ScrollRect sRect;
 
     // Use this for initialization
     void Start () {
 
         scroll = FindObjectOfType<Scrollbar> ();
 
         sRect = FindObjectOfType<ScrollRect> ();
         
     }
     
     // Update is called once per frame
     void Update () {
 
         isDragging = Dragging;
 
         if (!scroll)
             return;
 
         //ScrollVelocity = sRect.inertia.y;
 
         if (!Dragging)
             scroll.value = Mathf.Lerp (scroll.value, Mathf.Round (scroll.value * (Snaps - 1)) / (Snaps - 1), SnapSpeed * Time.deltaTime);
         
     }
 
     public void OnBeginDrag (PointerEventData eventData) {
         Dragging = true;
     }
 
     public void OnEndDrag (PointerEventData eventData) {
         Dragging = false;
     }
 
 }
 


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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by ExtinctSpecie · Mar 05, 2017 at 03:30 PM

check this out it may help you

https://www.youtube.com/watch?v=KJlIlWHlfMo

https://bitbucket.org/ddreaper/unity-ui-extensions

Comment
Add comment · Show 5 · Share
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
avatar image TheShadyColombian · Mar 06, 2017 at 02:08 PM 1
Share

This looks perfect! Give me a bit of time to test it before accepting the answer.

avatar image RigbyShows · Jul 28, 2018 at 02:01 PM 0
Share

This has to be a joke. 20 pages of script for just scroll snapping. This is the worst code I have ever seen in my life.

avatar image TheShadyColombian RigbyShows · Jul 29, 2018 at 06:33 PM 1
Share

Yes, 20 pages of commented, well documented code. Which is not only clean but also perfectly solves my problem.

If you're gonna come here and judge people's answers, then at least provide a better alternative yourself

avatar image RigbyShows · Jul 30, 2018 at 07:56 AM 0
Share

https://www.youtube.com/watch?v=9B7ahj1kaYs

Here is your reply. Just 1 page of code for a simple thing that shouldn't be taking more than 20 $$anonymous$$utes to do or understand.

avatar image TheShadyColombian RigbyShows · Jul 30, 2018 at 02:54 PM 1
Share

"shouldn't be taking more than 20 $$anonymous$$utes to do or understand" yet you link a 20 $$anonymous$$ute video (which is only part 1) plus no link to a repo or source code. I'll stick with the correct answer which is the one given by ExtinctSpecie

avatar image
0

Answer by RigbyShows · Jul 30, 2018 at 07:56 AM

https://www.youtube.com/watch?v=9B7ahj1kaYs

Here is your reply. Just 1 page of code for a simple thing that shouldn't be taking more than 20 minutes to do or understand.

Comment
Add comment · Share
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
avatar image
0

Answer by Vishwa001 · Sep 29, 2017 at 10:26 AM

Scroll Snap Unity for Horizontal scroll https://github.com/BushidoInteractive/ScrollSnapUnity is esay to use unity package and also manageable with your needs.

Comment
Add comment · Share
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

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

105 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scroll Rect Min/Max Size 0 Answers

ScrollRect scrollbar drag being overridden and stuttering to top 0 Answers

Where can I edit UI.ScrollView? 0 Answers

ScrollBar on ScrollRect not scrolling to the bottom when setting value through code 0 Answers

How do I Make TextMeshPro InputField scroll through code? 0 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