2

I want to make a C# script for Unity to make the scroll view scroll up when I press "UP" key and scroll down when I press "DOWN" key.

2
  • content.localPosition = new Vector3(content.localPosition.X, content.LocalPosition.Y + 1, content.localPosition.Z);
    – mrogal.ski
    Dec 2, 2016 at 12:07
  • Is this change the position of the "Content"?
    – MBS
    Dec 2, 2016 at 12:10

4 Answers 4

18

Simply use

ScrollRect.horizontalNormalizedPosition // value range (0 to 1)

or

ScrollRect.verticalNormalizedPosition // value range (0 to 1)

try to lerp these values accordingly on button click event handlers for up and down buttons.

Or you can have a look at scripts HorizontalScrollSnap.cs and VerticalScrollSnap.cs at UnityUI-Extentions

1
  • This solution is a bit of a pain to use, though, since you probably want to scroll a constant amount in content space. So you'd have to do some calculations to figure out the normalized amount to add or subtract. It's a bit surprising there isn't some easier or more standard way to mimic what the scroll wheel does.
    – Joe Strout
    Jul 23, 2021 at 16:06
5

The other answers seemed incomplete or overly complex, so here's how I did it. Assuming scroll is the direction you want to scroll, speed is a property that controls your scroll speed in content units/sec, and scrollRect is a reference to the ScrollRect component:

        if (scroll != 0) {
            float contentHeight = scrollRect.content.sizeDelta.y;
            float contentShift = speed * scroll * Time.deltaTime;
            scrollRect.verticalNormalizedPosition += contentShift / contentHeight;
        }

This should shift the right amount for any content size, and correctly causes the elastic rebound at the top and bottom (if your ScrollRect is configured for that).

1
  • This works, but I noticed that the speed was not as fast as it should be. For example I set it to 10 it did not scroll 10 units / sec. I think that's because the scroll viewer does not scroll the full content height, so I removed the viewport height: float contentHeight = scrollRect.content.SizeDelta.Y - scrollRect.viewport.rect.height; I then also renamed the variable to scrollHeight, after that change the scroll speed of 10 scrolled 10 units / sec
    – R1PFake
    Jan 2, 2022 at 18:46
0

For a smooth scroll (using lerp).

[SerializeField]
private ScrollRect _scrollRectComponent;
[SerializeField]
RectTransform _container; 
private IEnumerator LerpToPage(int page)
{
    Vector2 _lerpTo = (Vector2)_scrollRectComponent.transform.InverseTransformPoint(_container.position) - (Vector2)_scrollRectComponent.transform.InverseTransformPoint(target.position);
    bool _lerp = true;
    Canvas.ForceUpdateCanvases();

    while(_lerp)
    {
        float decelerate = Mathf.Min(10f * Time.deltaTime, 1f);
        _container.anchoredPosition = Vector2.Lerp(_scrollRectComponent.transform.InverseTransformPoint(_container.position), _lerpTo, decelerate);
        if (Vector2.SqrMagnitude((Vector2)_scrollRectComponent.transform.InverseTransformPoint(_container.position) - _lerpTo) < 0.25f)
        {
            _container.anchoredPosition = _lerpTo;
            _lerp = false;
        }
        yield return null;
    }
}
-2

Your question is very incomplete. For 2d or 3d? What have you tried?

Heres how id do it, with some assumptions where you left information out. Add this code in your Camera component:

void Update() {
    if (Input.GetKeyDown(Input.KeyCode.W) {
        transform.position = new Vector3(transform.position.x, transform.position.y + 2, transform.position.z);
    }
}

What this does is to access the objects tranform you attach the script to and adjust the Y-value of its position with +2 if you click W.

You can then just add more if-statements and adjust the keys, but the core is there.

2
  • I want to use with VR 3d application. I don't want to change the rotation of the camera. What I made in my app is to add objects to Content branch and after adding a number of objects the old ones start to go up and disappear, and I need to scroll up (using scrollbar vertical) to see them
    – MBS
    Dec 2, 2016 at 12:49
  • Oh, I see. Maybe Umair M's response is more accurate then. Dec 2, 2016 at 12:50

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.