How to make elements scrollable with only the scrollbar?
Currently it is possible to use both the scrollbar to scroll the element and to click and drag the element itself. However, the latter is not desired.
Thank you!
How to make elements scrollable with only the scrollbar?
Currently it is possible to use both the scrollbar to scroll the element and to click and drag the element itself. However, the latter is not desired.
Thank you!
For a ScrollRect? You could subclass it and override its behaviour.
Yes, I have a gameobject âMapListâ. This has the Mask Image, Mask and ScrollRect components. This object has 2 children. One is the âGridâ where I am instantiating map ui elements. And the second on is the âScrollbarâ. The grid has the Image, Content Size Fitter and Vertical Layout Group components. Viewport of ScrollRect is the âScrollbarâ gameobject and the Content of ScrollRect is the âGridâ gameobject.
Okay⌠I always make my ScrollRect designs like so:
ScrollRect
-(child) viewport
-(child of viewport) content portion (this is where Iâd put grids)
-(child of scrollrect) - scroll bars
I think thatâs right. When you add âscrollviewâ to the scene, the scrollbars and that layout should all be proper anyways.
There is code that deals with dragging, the scrollwheel on the mouse, and the scrollbars, I"m pretty sure.
So, if you subclassed ScrollRect you could override the behaviour for things you wanted to change or ignore.
Have you tried it?
I read other threads and I found that if I set the Horizontal/Vertical boxes to false in Scroll Rect then it should work. Problem is that if I disable the vertical (not using horizontal) flag, the scrollbar is automatically disabled. Is this a bug or an intended behaviour?
Not sure, but it sounds reasonable that if you disable both scroll options the bar isnât there.
No word on whether you tried my previous suggestion? ![]()
I tried but this stops the scrollbar from working. Other people are saying that if you disable the Horizontal/Vertical boxes then it works for them. For me it just disables the scrollbar.
So, you made a subclass & modified the overrode the behaviour of dragging and that didnât work for you?
I did not try creating a subclass. Isnât there really a simpler and less time consuming method for making the content not scrollable by dragging the content itself but rather just the scrollbar?
Hm. I was about to say ânoâ However, if you disable every âraycast targetâ option on every image in the scroll view and content and viewport (just tested this)⌠then sure, you can accomplish that.
Then, however, you canât get any response if you ever want to click/select anything in there.
This is all I had to do to avoid dragging.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ScrollDerived : ScrollRect {
public override void OnDrag(PointerEventData eventData)
{
// base.OnDrag(eventData);
print("Trying drag...but nothing happened.. Woah!? :)"); // definitely delete this when you get to using it :)
}
}
Thank you!
Disabling the raycast should work, yes, however with the disadvantage you described.
Iâll try the code your provided and let know how it works out.
Cool. np
Just commenting because this is the first thing that comes up in a search, and maybe it could be helpful to someone else.
After unsuccessfully trying quite a lot of different ways of achieving this, the simplest, although by brute force, solution that Iâve found is to simply create another image and place it towards the bottom of your hierarchy. Place it inside the canvas, but definitely below/past everything that has anything to do with scrolling, the scroll area, the content, etcâŚ
In the editor window, place the image directly on top of the area containing the content that you want to be able to scroll from the scrollbar, but not be able to be scrolled within the area itself. Make sure that it covers it up. You do not want to be able to see your content. Press play, and make sure that you canât scroll in the area anymore. Then, go back into the editor and turn the opacity all the way down until it is transparent.
Youâve just applied a protective, non-scrollable, clear coat
!!