Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 bicomicguy · Mar 18, 2019 at 02:13 AM · scrollviewscrollingautomatic

Scroll rect Autoscroll script almost working

Ok so in my project I'm creating the items menu and I made a scroll rect for it. After researching and finding out that autoscroll wasn't available already in Unity. I found a bunch of different scripts, but only one is almost working. It scrolls like I need it to, but It doesn't scroll all the way to the end of my item buttons/content. Can someone help me figure out what's wrong? Thanks in advance!!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
  
 [RequireComponent( typeof( ScrollRect ) )]
 public class ScrollToSelected : MonoBehaviour {
  
 public float scrollSpeed = 10f;
  
 ScrollRect m_ScrollRect;
 RectTransform m_RectTransform;
 RectTransform m_ContentRectTransform;
 RectTransform m_SelectedRectTransform;
  
 void Awake() {
     m_ScrollRect = GetComponent<ScrollRect>();
     m_RectTransform = GetComponent<RectTransform>();
     m_ContentRectTransform = m_ScrollRect.content;
 }
  
 void Update() {
     UpdateScrollToSelected();
 }
  
 void UpdateScrollToSelected() {
  
 // grab the current selected from the eventsystem
     GameObject selected = EventSystem.current.currentSelectedGameObject;
  
 if ( selected == null ) {
     return;
 }
 if ( selected.transform.parent != m_ContentRectTransform.transform ) {
     return;
 }
  
 m_SelectedRectTransform = selected.GetComponent<RectTransform>();
  
 // math stuff
     Vector3 selectedDifference = m_RectTransform.localPosition - m_SelectedRectTransform.localPosition;
     float contentHeightDifference = ( m_ContentRectTransform.rect.height - m_RectTransform.rect.height );
  
     float selectedPosition = ( m_ContentRectTransform.rect.height - selectedDifference.y );
     float currentScrollRectPosition = m_ScrollRect.normalizedPosition.y * contentHeightDifference;
     float above = currentScrollRectPosition - ( m_SelectedRectTransform.rect.height / 2 ) + m_RectTransform.rect.height;
     float below = currentScrollRectPosition + ( m_SelectedRectTransform.rect.height / 2 );
  
 // check if selected is out of bounds
     if ( selectedPosition > above ) {
         float step = selectedPosition - above;
         float newY = currentScrollRectPosition + step;
         float newNormalizedY = newY / contentHeightDifference;
         m_ScrollRect.normalizedPosition = Vector2.Lerp( m_ScrollRect.normalizedPosition, new Vector2( 0, newNormalizedY ), scrollSpeed * Time.deltaTime );
     } else if ( selectedPosition < below ) {
         float step = selectedPosition - below;
         float newY = currentScrollRectPosition + step;
         float newNormalizedY = newY / contentHeightDifference;
         m_ScrollRect.normalizedPosition = Vector2.Lerp( m_ScrollRect.normalizedPosition, new Vector2( 0, newNormalizedY ), scrollSpeed * Time.deltaTime );
     }
  
 }
  
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by billybob1978 · Mar 18, 2019 at 02:49 AM

What do you mean by autoscroll?

I have a chat and I make it autoscroll to the bottom every time I hit enter by using this.

scrollrect.velocity = new Vector2(0f, 1000f);

Not sure if that helps!

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 bicomicguy · Mar 18, 2019 at 03:54 AM

Like when selecting a button from the viewport and the ui automatically pushes the scrollrect to follow the buttons that are out of bounds/view of the scroll rect. Thanks for the reply, I'll check and see if it works.

Comment
Add comment · Show 2 · 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 billybob1978 · Mar 18, 2019 at 04:53 AM 0
Share

let me know, i'm curous!

avatar image bicomicguy billybob1978 · Mar 18, 2019 at 11:06 PM 0
Share

Unfortunately it didn't work, it made the scroll weird.

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

100 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

Related Questions

Moving a ScrollRect child via script to focus on some point. 0 Answers

Scrollbar touch sensitivity 0 Answers

How do I Make TextMeshPro InputField scroll through code? 0 Answers

[Solved] need help regarding autolayout in scrollrect 1 Answer

ScrollRect not working on mobile at certain resolution 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