#ScrollRect with controller

1 messages · Page 1 of 1 (latest)

autumn gale
#

The scene:
- menu container with the scroll rect
-- menu with layout element
--- all the grid tiles instantiated during runtime

I have it working nicely with scroll and drag as it's default behaviour, but there's absolutely nothing built in for controllers/keyboard it seems?

The grid tiles are all buttons that do get selected with the controller, but it doesn't scroll. Trying to set the y position of the grid and/or container just launches it into space.

Looking online found me code relating to selectables and such but the grid tiles don't have that, they're just interactable buttons.

#

Vertical scroll

#

The tiles are also minorly vertically stretched for some reason and the very bottom of the last row is a little cut off but I got no idea where to begin with those issues and they're minor so they can be looked at after functionality is done 😅

#

(I'm also sorry for so many posts with stupid questions 😭)

eternal bronze
#

yeah, you need to move the scroll rect around yourself

#

i believe my game just uses this:

#
using Pursuit.Code.ExtensionMethods;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Pursuit.UI
{
    public class ScrollRectSnapper : MonoBehaviour, ISelectHandler
    {
        public void OnSelect(BaseEventData eventData)
        {
            // trying to reject the mouse cursor here, since TMP_Dropdown
            // selects things that we hover over!!

            if (eventData.GetType() == typeof(BaseEventData))
                return;

            GetComponentInParent<ScrollRect>().SnapTo(transform as RectTransform);
        }
    }
}
#

where SnapTo is an extension method I wrote

#
using UnityEngine;
using UnityEngine.UI;

namespace Pursuit.Code.ExtensionMethods
{
    public static class ScrollRectExtensionMethods
    {
        public static void SnapTo(this ScrollRect scroller, RectTransform child)
        {
            Canvas.ForceUpdateCanvases();

            var contentPos = (Vector2)scroller.transform.InverseTransformPoint(scroller.content.position);
            var childPos = (Vector2)scroller.transform.InverseTransformPoint(child.position);
            Vector2 endPos = contentPos - childPos;
            // If no horizontal scroll, then don't change contentPos.x
            if (!scroller.horizontal) endPos.x = contentPos.x;
            // If no vertical scroll, then don't change contentPos.y
            if (!scroller.vertical) endPos.y = contentPos.y;
            scroller.content.position = scroller.transform.TransformPoint(endPos);

            Vector2 goalPos = scroller.normalizedPosition;

            goalPos.x = Mathf.Clamp01(goalPos.x);
            goalPos.y = Mathf.Clamp01(goalPos.y);

            scroller.normalizedPosition = goalPos;
        }
    }
}
#

i'm pretty sure I cribbed the latter from a forum post lol

#

the ForceUpdateCanvases ensures that the current layout is valid

#

that may be a bit expensive to run every single time I select something, hm

autumn gale
#

Ohhh this is using the selectable component isn't it

#

All the grid tiles I have are buttons, I don't think any have the selectable component. I might at this point just have to go add it onto the prefabs and hope that doesn't break things

#

Oh! Buttons inherit selectable stuff

autumn gale
#

Hmm no dice, it's just snapping back to the default position while the selection cursor keeps going down. It's highlighting and selecting the correct tiles, just not moving the scrollrect with it properly (or it is and it's just bouncing back)

#

it was conflicting with another function also trying to scroll

#

It woooorrrrksssss