I’ve been attempting to figure out how to fabricate mouse input on the new UI all day, and feel like I’m stuck in the mud. As the UI is new, it seems there is close to zero in the way of documentation on the scripting end of the EventSystem.
Problem:
For testing purposes, what I am trying to do is get a scroll bar on a canvas to react to 2 key presses. I have the the scroll bar getting selected, and even firing it’s OnDrag event callback (as well as other various callbacks), yet no reaction on the UI end… The goal is to have the ‘T’ key select the scrollbar, and then while holding the ‘S’ key, have it scroll downwards. The latter part (dragging the scrollbar) being the what’s not working. If anyone has any knowledge of how to accomplish this, or a resource (youtube video, blog post, garbage pale outside Unity HQ for scraps of know-how, anything…) I would greatly appreciate it.
Apologies in advance for the following mess of code… (been tinkering all day, haven’t had time to clean up)
Some of what I have (explained below):
// TESTING
if( Input.GetKeyDown( KeyCode.T ) )
{
//PointerEventData data = new PointerEventData( EventSystem.current );
//ExecuteEvents.Execute( m_scrollBar, data, ExecuteEvents.beginDragHandler );
PointerEventData data = new PointerEventData( EventSystem.current );
data.selectedObject = m_scrollBar;
//data.pressPosition = new Vector2( Input.mousePosition.x, Input.mousePosition.y );
m_pressPos = new Vector2( Input.mousePosition.x, Input.mousePosition.y );
ExecuteEvents.Execute( m_scrollBar, data, ExecuteEvents.selectHandler );
ExecuteEvents.Execute( m_scrollBar, data, ExecuteEvents.pointerEnterHandler );
ExecuteEvents.Execute( m_scrollBar, data, ExecuteEvents.initializePotentialDrag );
ExecuteEvents.Execute( m_scrollBar, data, ExecuteEvents.pointerDownHandler );
}
if( Input.GetKey( KeyCode.S ) )
{
PointerEventData data = new PointerEventData( EventSystem.current );
//data.position = new Vector2( Input.mousePosition.x, Input.mousePosition.y );
//data.scrollDelta = new Vector2( Input.mouseScrollDelta.x, Input.mouseScrollDelta.y );
data.pointerDrag = m_scrollBar;
data.scrollDelta = new Vector2( 0, -0.1f );
ExecuteEvents.Execute( m_scrollBar, data, ExecuteEvents.dragHandler );
}
Both of the above if statements are in an overridden function of the BaseInputModule function, Process(). The script itself inherits from BaseInputModule, and is a component of the EventSystem game object in my scene. I have a second script on the scrollbar itself that simply prints information to the console to determine that the events are being properly sent, callbacks like OnDrag, etc.
Thanks so much, any help is greatly appreciated!