- Home /
How to force Eventsystem Drag
So, i'm making a virtual joystick for my android game, the joystick anchor its position to where the user touches on the screen, and the whole process of moving the joystick is made through IDragHandler, IBeginDragHandler, IEndDragHandler.
The problem is that the events from Eventsystem
are called before the Touch, so when i touch the screen Eventsystem will compute an empty space and then the touch will move the joystick to the touched position.
How can i force Eventsystem
to execute BeginDrag
after the joystick have being moved to its position?
I know that there's the ExecuteEvents.Execute();
but i can only make PointerUp and Down to work, drag dosen't.
Answer by EDevJogos · Dec 25, 2017 at 08:15 PM
Found out a solution.
First i was trying to use ExecuteEvents.Execute()
passing a new PointerEventData
which dosen't work, you have to use the current PointerEventData
from the EventSystem
you can get it in most EventSystem interfaces like: IPointerClickHandler
public void OnPointerClick(PointerEventData eventData)
{
if(JoystickController.CurrentPointer == null)
{
//Store the EventSystem PointerEventData for future use.
JoystickController.CurrentPointer = eventData;
}
}
Than just store it somewhere, and at the Touch or Mouse click call, you have to set the pointerDrag
object and call the ExecuteEvents
with the event initializePotentialDrag
, like:
CurrentPointer.pointerDrag = joystick.gameObject;
ExecuteEvents.Execute(joystick.gameObject, CurrentPointer, ExecuteEvents.initializePotentialDrag);
Note. You can't call ExecuteEvents
from the same object that will recieve the Event, it won't work.
Hi, I've tried this solution and it doesn't really work for me. In my situation, I got a bunch of game objects implementing the drag interfaces inside a parent container which has a ScrollRect component.
When implementing your solution, and after adding a log in OnBeginDrag
, OnDrag
and OnEndDrag
the only thing I get is one debug line per function called, i.e., it's only being called once, so I can actually see how the object moved in 1 frame.
Note: I had to make a call to initializePotentialDrag
, OnBeginDrag
and OnDrag
(looping this one to test makes the object move) to be able to see some changes, calling only ExecuteEvents.initializePotentialDrag
didn't to anything. Also, I'm actually able to call this from within the same gameobject and works. Finally, I'm enabling this script in runtime so when I'm calling it the button is already pressed in which case the OnBeginDrag is never called? Not really sure.
Any ideas of why this could be happening?
Thanks in advance!
If anyone else reads this thread, this worked very well for me.
For a bit more details:
The thing that seems to bug is only InitializePotentialDrag. So you only need to change this and not all the other drag method (they will work normaly once InitializePotentialDrag has been sent).
I didn't store the PointerEventData but sent it directly and then added this line: pointerData.pointerDrag = targetObject;
void IInitializePotentialDragHandler.OnInitializePotentialDrag (PointerEventData eventData)
{
eventData.pointerDrag = $$anonymous$$yTarget.gameObject;
ExecuteEvents.Execute<IInitializePotentialDragHandler> ($$anonymous$$yTarget.gameObject, eventData, ExecuteEvents.initializePotentialDrag);
}
void IBeginDragHandler.OnBeginDrag (PointerEventData eventData)
{
ExecuteEvents.Execute<IDragHandler> ($$anonymous$$yTarget.gameObject, eventData, ExecuteEvents.dragHandler);
}
Your answer
Follow this Question
Related Questions
Detecting onDrag over UI images 1 Answer
Drag and Swap two object using touch 0 Answers
Touch - drag different objects 1 Answer
Dragging Camera based on Touch 0 Answers
how create flipping book effect like apple ibook with touch ? 0 Answers