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.
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.
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
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.
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
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.
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 ();
}
}
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…
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.