ScrollView - How to set the scroll position programatically?

I’ve been using the scroll view:

scrollPosition = GUI.BeginScrollView(rect, scrollPosition, contentRect, HorizontalStyle, VerticalStyle)

I’ve got two problems:

  1. I cannot set the scroll position programatically. For instance, the line before previous line I set the scrollPosition to some value:
scrollPosition = 100;

However, the scroll position isn’t changed.
Interesting, but: if I put not a variable, but number in it (for instance 100), it works:

scrollPosition = GUI.BeginScrollView(rect, 100, contentRect, HorizontalStyle, VerticalStyle)

I need to set this programatically, since I want to scroll down to the end of the list when a new item is added.

Note: Yes, the value of the variable is less than the maximum possible scroll value, this isn’t the problem.

  1. I cannot get the horizontal scrollbar to be visible. Even if I set the parameter alwaysShowHorizontal to true. I’ve never seen the horizontal one.

Danko

BeginScrollView returns the value of the scroll position. So just setting a variable to a number like this:

scrollPosition = 100;

won’t do anything because it has nothing to do with the BeginScrollView function.

You can use a variable no problems.

var foo = 100;
GUI.BeginScrollView(rect, foo, contentRect, HorizontalStyle, VerticalStyle);

–Eric

The problem with your code is that it kills the ‘drag’ behaviour of the scrollview. I want the ability of scrolling it manually (by dragging it) and programmatically at the same time.

That was just an example. You’d keep the drag behavior, and set the position explicitly when desired.

–Eric

Eric, it doesn’t work - have you try it?

Yes, if you look at the file requester in Fractscape, I use GUILayout.BeginScrollView() normally, and also you can use the arrow keys to select items instead of the mouse, in which case I set the position explicitly.

–Eric

Can you post a simple example?

Like a scrollview plus a button outside - and the button click changes a scroll position.

Some of you are asking the solutions on this. I didn’t solve it, but a colleague of mine (that has an experience in Unity) said that if I use ScrollBar instead of ScrollView and implement my own logic of scrolling, that it should work.

/* Vertical Scrollbar example */

var vScrollbarValue : float;

function OnGUI () {
   vScrollbarValue = GUI. VerticalScrollbar (Rect (25, 25, 100, 30), vScrollbarValue, 1.0, 10.0, 0.0);
}

ps. I’ve seen the horizontal scrollbar - finally - but I really can’t tell what was wrong before :slight_smile:

GUI.BeginScrollView takes a Vector2 as a parameter for the horizontal/vertical scroll position and also returns a Vector2 with the updated position after any scrolling carried out by the user. The code will be something like:-

var scrollPos: Vector2 = <whatever you like>;
scrollPos = GUI.BeginScrollView(rect, scrollPos, viewRect);

You can modify the value of scrollPos yourself at any time and this will be reflected in the ScrollView’s position.

Didn’t work for me. Have you really try it?

Same here. It does not work. I’ve been despairingly trying to give the scroll view an offset to jump to the highscore position of my player, but even if I set the position manually it doesn’t work. A working example would be awesome :slight_smile:

Edit: Of couse it works to have a scrollbar that is clickable by the user to scroll up and down, but not have it at the same time giving it an offset programmatically.

I’ve not the slightest problem with it.

The following code even works on Unity iPhone 1.5.1

using UnityEngine;
using System.Collections;

public class UITest : MonoBehaviour {
	private Vector2 scrollPos = new Vector2( 356, 316 );
	private Rect scrollRect = new Rect( 10, 10, 460, 300 );
	private Rect scrollViewRect = new Rect( 0, 0, 800, 600 );
	
	void OnGUI() {
		scrollPos = GUI.BeginScrollView( scrollRect, scrollPos, scrollViewRect );
		Debug.Log( "Scroll Position: " + scrollPos );
			GUI.Button (new Rect (0,0,100,20), "Top-left");
		    GUI.Button (new Rect (120,0,100,20), "Top-right");
		    GUI.Button (new Rect (400,180,100,20), "Bottom-left");
		    GUI.Button (new Rect (700, 580,100,20), "Bottom-Right");
	    
		GUI.EndScrollView ();
	}
}

Thanks for the code, but it doesn’t work for me.

I have PC and Windows Vista and it doesn’t work in Unity IDE.

As least we know that the same code works differently in different runtimes.

:idea:

works as well on 2.6.1 so you just have an outdated unity likely

Now this is weird. I have exactly the same code here in JavaScript and it’s not working. Thanks Dreamora for sharing, I’ll go and investigate what the problem might be. Very, VERY weird…

Doh! The code was working all the time. The issue was that I was setting the scroll position programmatically, but at that point the highscore list wasn’t simply available as it was getting fetched via www. So the code tried to set a scroll position and the scroll view wasn’t that tall at that moment. I now set the scroll position AFTER the highscore table was loaded from the web and it works brilliantly… :slight_smile:

The problem with the second issue “(2) I cannot get the horizontal scrollbar to be visible” was in a skin. The style for scrollbars diden’t referenced any graphics, so that was the problem.