Can't select button in ScrollArea when moving from ScrollBar to button with mouse button pressed
So I have a row of buttons in a ScrollArea with a ScrollBar beneath it like that:
Everything works fine when I press the left mouse button, move the scroll bar, release the button and move my mouse over one of the buttons.
But when I press the left mouse button on the scrollbar and then move my mouse over a button and release it, the button won't get hovered until I either release and press the button again or hover a different ui object.
I guess this is because Unity keeps the ScrollBar focused when I move my mouse out of the ScrollBars area to keep it moveabe but fails to reset the focus in this case somehow. Any idea on how to fix that?
Answer by BitteWenden · Dec 10, 2016 at 09:45 PM
Found a workaround!
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
using System.Collections.Generic;
public class ScrollbarWithFix : Scrollbar, IEndDragHandler
{
public void OnEndDrag(PointerEventData eventData)
{
GraphicRaycaster graphicsRaycaster = this.GetComponentInParent<GraphicRaycaster>();
PointerEventData pointerEventData = new PointerEventData(null);
pointerEventData.position = Input.mousePosition;
List<RaycastResult> results = new List<RaycastResult>();
graphicsRaycaster.Raycast(pointerEventData, results);
foreach(RaycastResult result in results)
{
Selectable sel = result.gameObject.GetComponent<Selectable>();
if(sel != null)
{
sel.OnPointerEnter(new PointerEventData(EventSystem.current));
}
}
}
}
Your answer
Follow this Question
Related Questions
Image Component is always null even when I assign it myself in the editor 1 Answer
UI is not responsive in any way, shape or form (2020.2.1f1)[SOLVED] 1 Answer
UI slider arbitrarily decides to get stuck and not move 0 Answers
Ui layout elements keep reverting after taken outside of grid element 0 Answers