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.
How it looks when it first starts
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;
}
}
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!!!
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.
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.
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.