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 /
  • Help Room /
avatar image
0
Question by Avaciin · Jul 14, 2016 at 08:53 PM · unity 5uiscrollview

Control ScrollView with arrow keys?

I have been searching everywhere to find out how to scroll through a ScrollView with the arrow keys and I'm pretty close to getting it to work. However there's a weird issue that when it scrolls, it cuts off half the next object in the list. I'll post images to show how this is behaving.

alt text

How it looks when it first starts

alt text

Then it starts misbehaving when scrolling. i.e it's not spacing correctly.

Here's the code I've been trying:

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using System.Collections;
 
 public class InventoryWindowController : MonoBehaviour {
 
     public RectTransform scrollRectTransform;
     public RectTransform contentPanel;
     public RectTransform selectedRectTransform;
     public GameObject curSelected;
     public GameObject lastSelected;
 
     public float selectedPositionY;
     public float scrollViewMinY;
     public float scrollViewMaxY;
     public float newY;
 
     void Start () {
 
     }
 
     void Update () {
         curSelected = EventSystem.current.currentSelectedGameObject;
 
         selectedRectTransform = curSelected.GetComponent<RectTransform>();
 
         // The position of the selected UI element is the absolute anchor position,
         // ie. the local position within the scroll rect + its height if we're
         // scrolling down. If we're scrolling up it's just the absolute anchor position.
         selectedPositionY = Mathf.Abs(selectedRectTransform.anchoredPosition.y) + selectedRectTransform.rect.height;
         // The upper bound of the scroll view is the anchor position of the content we're scrolling.
         scrollViewMinY = contentPanel.anchoredPosition.y;
         // The lower bound is the anchor position + the height of the scroll rect.
         scrollViewMaxY = contentPanel.anchoredPosition.y + scrollRectTransform.rect.height;
 
         // If the selected position is below the current lower bound of the scroll view we scroll down.
         if (selectedPositionY > scrollViewMaxY) {
             newY = selectedPositionY - scrollRectTransform.rect.height;
             contentPanel.anchoredPosition = new Vector2(contentPanel.anchoredPosition.x, newY);
         }
         // If the selected position is above the current upper bound of the scroll view we scroll up.
         else if (Mathf.Abs(selectedRectTransform.anchoredPosition.y) < scrollViewMinY) {
             contentPanel.anchoredPosition = new Vector2(contentPanel.anchoredPosition.x, Mathf.Abs(selectedRectTransform.anchoredPosition.y));
         }
 
         lastSelected = curSelected;
     }
 }
 
onscroll.png (23.4 kB)
starting-image.png (21.7 kB)
Comment
Add comment · Show 1
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 Merrick20 · Feb 22, 2017 at 03:02 PM 0
Share

Thank you for this!! I've been going around this for a long time. Did you fin a solution to this? Have you thought about making a tutorial?

Again thank you!!!

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by MiraiTunga · Nov 08, 2017 at 01:20 PM

You can use this.

https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/src/88c43891c0bf44f136e3021ad6c89d704dfebc83/Scripts/Utilities/UIScrollToSelection.cs?at=master&fileviewer=file-view-default

:)

Comment
Add comment · Show 1 · 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 radiantboy · Mar 31 at 05:09 PM 0
Share

AWESOME, works well thanks!!!!

avatar image
0

Answer by MichaelNielsenDev · Jul 14, 2016 at 09:56 PM

Try making the currently selected object in the scroll list the bottom viewable selection when scrolling down, and the top viewable selection when scrolling up. This will make the list scroll when you want it to and will prevent seeing half an object being cut off.

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 Avaciin · Jul 14, 2016 at 09:58 PM 0
Share

Where would i set that?

avatar image MichaelNielsenDev Avaciin · Jul 14, 2016 at 10:52 PM 0
Share

You'd use curSelected

It looks like there are five gameobjects onscreen in that list at any given time. Therefore, start with curSelected at the top gameobject, then as soon as you start to scroll down, make it five objects down the list. When first scrolling up, make it five objects up the list. Otherwise, just have it go up and down normally.

avatar image
0

Answer by LocksmithArmy · Sep 02, 2020 at 03:33 AM

Here is a simple method to scroll via code.

You must track the highlighted item you are scrolling through though.

         public void SetScrollbar(Scrollbar scrollbar, int target, int qty)
         {
             scrollbar.value = 1 - ((float)target / (float)(qty - 1));
         }

I use '1 - ' because my 0 index is at the top... yours may not be, depending on how you loaded your list.

scrollbar is the actual scrollbar component, target is the currently selected item, and qty is the total number of items in the list.

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

116 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UI Scroll View - Scrollbar only working on one half 1 Answer

i need help with uGUI Scrollview!!! 0 Answers

Why does the Unity Scroll View cannot be scrolled horizontally using the finger instead of using the scroll bar 0 Answers

Scroll rect - Repositioning elements causes scroll velocity to sky rocket ?! 1 Answer

How to change a score 1 Answer


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