How to determine if a slider is present in a scrollview (GUILayout)

EDIT: attempting to state my question more clearly

In my game the mouse scrollwheel zooms the camera in and out.

I have a GUILayout.Label on the screen that is inside a scrollview.

The code looks something like this:

 GUILayout.BeginArea(someRectangle);
 scrollVector = GUILayout.BeginScrollView(scrollVector);
 GUILayout.Label(textOfArbitraryLength);
 GUILayout.EndScrollView();
 GUILayout.EndArea();

If textOfArbitraryLength is long enough (such that it is taller than someRectangle and requires a vertical slider) I would like for mouse scrollwheel input to control the scrolling of the text.

If textOfArbitraryLength is small enough (such that it fits inside of someRectangle without scrolling) I would like for mouse scrollwheel input to control the zooming of the camera.

Is it possible to determine if there is a slider present in the scrollview?

I want to be able to determine when the Camera Control script should ignore scrollwheel input.

Sorry if I did a bad job explaining what I was looking for... And thanks in advance for any help.


original post:

Is it possible to determine whether or not a slider is being used inside a scrollview when using GUILayout?

I have a label inside a scrollview and would like to ignore input from the mouse scrollwheel when the mouse is over the label AND the content is scrollable. In other words, I don't want undesired things to happen in the scene when the user is trying to scroll over the text in the label.

If the content of the label fits within the scrollview (ie there is no slider) I would like to process mouse input as usual. Most of the time this is the case...

I can almost accomplish this by checking to see if the scrollPosition vector's y-value is 0, but not quite. If the user scrolls up too much, there will be undesired effects once the slider hits the top and the scroll wheel keeps moving.

Thanks in advance for any help...

Just don't assign the returned value to your scrollposition in that case...

I’ll assume JS:

var scrollPosition : Vector2;

function OnGUI()
{
    var newposition = GUILayout.BeginScrollView(scrollPosition);
    if ("It's allowed to be scrolled") {
        scrollPosition = newposition;
    }
    // [...]
    GUILayout.EndScrollView();
}

Just to complete the example. Here in C#:

Vector2 scrollPosition;

void OnGUI()
{
    Vector2 newposition = GUILayout.BeginScrollView(scrollPosition);
    if ("It's allowed to be scrolled") {
        scrollPosition = newposition;
    }
    // [...]
    GUILayout.EndScrollView();
}


edit

As you said you want the opposite. You want to know when the users mouse cursor is over a scrollview. With GUILayoutUtility.GetLastRect you can query the last layouted rectangle. But that only returns the right Rect in the repaint event, not during the layouting step.

var mouseOverScrollView = false;
function OnGUI()
{
    // [...]
    GUILayout.EndScrollView();
    if (Event.current.type == EventType.Repaint)
    {
        mouseOverScrollView = GUILayoutUtility.GetLastRect().Contains(Input.mousePosition);
    }
}

Another way could be using the tooltip functionality, but unfortunately a scrollview doesn't have a tooltip...


edit

I guess you use wordwrap. I’ve not tested this, but i guess it should work.

var canScrollText  = false;

function OnGUI()
{
    GUILayout.BeginArea(someRectangle);
    scrollVector = GUILayout.BeginScrollView(scrollVector);
    GUILayout.Label(textOfArbitraryLength);
    GUILayout.EndScrollView();
    GUILayout.EndArea();
    if (Event.current.type == EventType.Repaint)
    {
        var labelHeight = GUI.skin.label.CalcHeight(GUIContent(textOfArbitraryLength),someRectangle.width);
        canScrollText  = (labelHeight > someRectangle.height);
    }
}

I think it should be possible to do that for arbitrary content by using GetLastRect on a grouping control inside the scrollview to determine its size. But again, not tested.

Good luck.