Caixotim
Caixotim

Reputation: 85

Unity3D ScrollView scroll range

I'm trying to create a scrollView, but I don't know how to determine the maximum range of the scroll (or even better, the maximum scroll position).

This is what I have tried to do without any positive results:

void Update()
{
  //get the amount of space that my text is taking
  textSize = gs.CalcHeight(new GUIContent(text), (screenWidth/2));

  if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(0).tapCount==1) 
  {
    var touch = Input.touches[0];

   //check if touch is within the scrollView area
   if(touch.position.x >= (screenWidth/2) && touch.position.x <= (screenWidth) && touch.position.y >= 0 && touch.position.y <= (screenHeight))
   {
     if(touch.phase == TouchPhase.Moved)
     {
       scrollPosition[1] += touch.deltaPosition.y;
       if(scrollPosition[1] < 0)
       {
           scrollPosition[1] = 0;
       }

       //I subtracted this cause i read that the scrollbars take 16px
       if(scrollPosition[1] > (textSize-scrollViewRect.yMax-16)) //scrollViewRect is a Rect with the size of my scrollView
       {
          scrollPosition[1] = textSize-scrollViewRect.yMax-16;
       }
     }
   }
  }
}

void OnGUI()
{
 screenWidth = camera.pixelWidth;
 screenHeight = camera.pixelHeight;
 GUILayout.BeginArea(new Rect(screenWidth/2, 0, screenWidth/2, screenHeight));

 GUILayout.BeginScrollView(scrollPosition/*, GUILayout.Width (screenWidth/2), GUILayout.Height (screenHeight/2)*/, "box");

 GUILayout.Label (new GUIContent(text), gs);

 // End the scrollview
 GUILayout.EndScrollView ();
 scrollViewRect = GUILayoutUtility.GetLastRect();
 GUILayout.EndArea();
}

This was done assuming that the scrollView is on the right half of the screen.

Can you guys help me out on this?

Thanks in advance.

Upvotes: 2

Views: 10304

Answers (2)

Panther
Panther

Reputation: 21

I know this is 3 years later but maybe it will help others too...

I found that setting the scrollPosition to a very high number will force the scrollbar to the bottom:

scrollPosition.y = 999.9f;

Then you can read the position like this:

scrollPosition = GUILayout.BeginScrollView(scrollPosition, 
                                           GUILayout.Width(370),  
                                           GUILayout.Height(135)); 

And it will be set to whatever the maximum is.

Hope this helps!

Upvotes: 2

Happy Apple
Happy Apple

Reputation: 746

As shown in the API documentation for GUI.BeginScrollView, you can assign a Vector2 to the declaration of BeginScrollView to track and update where the box has been scrolled to.

I'll give an example:

using UnityEngine;
using System.Collections;

public class ScrollSize : MonoBehaviour {

private int screenBoxX  = Screen.width;
private int screenBoxY  = Screen.height;

private int scrollBoxX  = Screen.width  * 2;
private int scrollBoxY  = Screen.height * 3;

public Vector2 scrollPosition = Vector2.zero;

void Awake()
{
    Debug.Log ("box size is " + scrollBoxX + " " + scrollBoxY); 
}

void OnGUI()     
{
    scrollPosition = GUI.BeginScrollView(new Rect(0, 0, screenBoxX, screenBoxY),
                         scrollPosition, new Rect(0, 0, scrollBoxX, scrollBoxY));

    GUI.EndScrollView();

    // showing 4 decimal places
    Debug.Log ("Box scrolled @ " + scrollPosition.ToString("F4"));
}
}

For the explanation of this example, let's assume that the screen resolution is 800 by 600. Therefore the viewable scrollbox on the screen will be 800 wide and 600 high, with the internal area of the scrollbox being 1600 by 1800.

We create the scrollbox by assigning Gui.BeginScrollView() as the value of Vector2, scrollPosition. As the scrollbox is scrolled horizontally and vertically, the Vector2 will update with the scrolled value.

If the scrollbox is scrolled to the top-leftmost position, the value of scrollPosition will be 0,0.

If the scrollbox is scrolled to the bottom-rightmost position, the value of scrollPosition will be 816, 616, the size of the box on screen plus the thickness of the scroll bars.

Upvotes: 3

Related Questions