Need A Dynamic Button, UI Drag, Hold, Up, Down

Hi, I created a thread, here…

…I am posting here, since traffic in the UI Forum is quite slow. My issue is that I want to create a UI button that monitors mouse drag, mouse up, mouse down ect. Is this possible, or should I just create my own?

I understand I can just monitor all input types in Update, but I was hoping form something more dynamic.

I tried to use…

void onMouseDrag(){
}

…but there is no effect. I was surprised since this function exists, under the UI Button scripts.

So, what I am wondering is how to best, make a button, that allows me to utilize these things? Is a UI button the best call or should I just create my own?

OnMouseDrag() will trigger every frame that the mouse is held down on the button the script is attached to. It sounds like you’re trying to use it to monitor the dragging of every UI element simultaneously, but that won’t work this way. What you could do is attach a script to every button you want to monitor with a OnMouseDrag() script that immediately calls the OnMouseDrag() script of a master controller you’ve set up, then it’ll function much the same way that you want.

Naw man.
For some reason, it does not seem to be working.

I have a script, player manager, it holds the OnMouseDown() etc functions. No matter what, if I attach the script to the UI btn, or to some other game object and assign it, it does not fire.

I can assign a script to the btn and a particular function to fire if OnClick, but I thought these functions where inherently part of the UI Button class. So, if the functions existed, on a script this btn references that they would work.

I can create my own monitoring system easily, but was hopeful I could just use these functions for precision and shear ease.

??

Capitalization problems? Make sure it’s named “OnMouseDrag” and not “onMouseDrag”. If that doesn’t work / isn’t the problem, then add in an empty OnMouseDown function to see if maybe the the Drag function requires it to be there.

these are the existing funcitons…

   public void OnMouseDrag(){
print ("a");
}
public void OnMouseDown(){
print ("b");
}
public void OnMouseUp(){
print ("c");
}

Of course I can assign anyone to work at onClick the btn, but none work independently
??

ps. I am using…

using UnityEngine.EventSystems;
using UnityEngine.UI;

Try removing the “public” from them. I would experiment with putting them on the Background object instead of the Button object too, just for kicks.

No Luck.

I really didn’t spend a lot of time with the legacy GUI system (I was using nGUI at the time), and any time I’ve needed OnDrag functionality since, I’ve used the UnityEvents system as it wasn’t limited to UI components (which felt like overcomplicating this problem). When I checked the documentation, several times since this started, I assumed that GUIElements was a base class or interface for some of the interactive UI controls or something. Turns out that it isn’t, so these functions simply don’t normally work on UnityEngine.UI-attached scripts. shrugs

Live and learn, I suppose.

1 Like

The thing the OnMouse… Functions are public functions of the UI Button class. Confused.

You can apparently add an extra component called “Event Trigger”, which has “drag”, “start drag”, and “end drag” options, which you can then point at the OnDrag function (I assume you could also reference it and assign the delegate in a script yourself if you like). Personally, I think implementing the interfaces as shown in that last link would be less work in the long run though.

To make things easier to maintain, you could have your manager bring up all button components of children and then add the EventTrigger component there in the script (also assigning the delegate there in the script), so that you don’t have to go through the effort of putting the component on everything and adding targets for them all in the inspector.

I’m sorry there wasn’t a simpler answer like I thought there was.

Hm.
Thanks. I actually was following a tutorial in exactly this earlier this afternoon. I copied the script identical to what he wrote, but it did not work. Perhaps I did not add it to the object, but was using a manager.

Cheers!!

Your OnMouseXXX events won’t work with UI elements. You need to implement the interfaces from the EventSystem namespace.

Here is a video tutorial I did showing dragging.

You also need to make sure you have the appropriate EventSystem and raycasters in your scene.