Search Unity

ScrollView - How to set the scroll position programatically?

Discussion in 'Immediate Mode GUI (IMGUI)' started by dkozar, Nov 30, 2009.

?

Have you ever seen the horizontal scroller?

  1. Nope

    100.0%
  1. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    I've been using the scroll view:

    Code (csharp):
    1. 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:

    Code (csharp):
    1. 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:

    Code (csharp):
    1. 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.

    2) 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
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    BeginScrollView returns the value of the scroll position. So just setting a variable to a number like this:

    Code (csharp):
    1. scrollPosition = 100;
    won't do anything because it has nothing to do with the BeginScrollView function.

    You can use a variable no problems.

    Code (csharp):
    1. var foo = 100;
    2. GUI.BeginScrollView(rect, foo, contentRect, HorizontalStyle, VerticalStyle);
    --Eric
     
  3. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    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.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That was just an example. You'd keep the drag behavior, and set the position explicitly when desired.

    --Eric
     
  5. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Eric, it doesn't work - have you try it?
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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
     
  7. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Can you post a simple example?

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

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    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.

    Code (csharp):
    1. /* Vertical Scrollbar example */
    2.  
    3. var vScrollbarValue : float;
    4.  
    5. function OnGUI () {
    6.    vScrollbarValue = GUI. VerticalScrollbar (Rect (25, 25, 100, 30), vScrollbarValue, 1.0, 10.0, 0.0);
    7. }
    http://unity3d.com/support/documentation/Components/gui-Controls.html

    ps. I've seen the horizontal scrollbar - finally - but I really can't tell what was wrong before :)
     
  9. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    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:-
    Code (csharp):
    1. var scrollPos: Vector2 = <whatever you like>;
    2. 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.
     
  10. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Didn't work for me. Have you *really* try it?
     
  11. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    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.
     
  12. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    I've not the slightest problem with it.

    The following code even works on Unity iPhone 1.5.1

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class UITest : MonoBehaviour {
    5.     private Vector2 scrollPos = new Vector2( 356, 316 );
    6.     private Rect scrollRect = new Rect( 10, 10, 460, 300 );
    7.     private Rect scrollViewRect = new Rect( 0, 0, 800, 600 );
    8.    
    9.     void OnGUI() {
    10.         scrollPos = GUI.BeginScrollView( scrollRect, scrollPos, scrollViewRect );
    11.         Debug.Log( "Scroll Position: " + scrollPos );
    12.             GUI.Button (new Rect (0,0,100,20), "Top-left");
    13.             GUI.Button (new Rect (120,0,100,20), "Top-right");
    14.             GUI.Button (new Rect (400,180,100,20), "Bottom-left");
    15.             GUI.Button (new Rect (700, 580,100,20), "Bottom-Right");
    16.        
    17.         GUI.EndScrollView ();
    18.     }
    19. }
    20.  
     
  13. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    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:
     
  14. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    works as well on 2.6.1 so you just have an outdated unity likely
     
  15. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    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...
     
  16. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    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... :)
     
  17. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    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.