- Home /
Unity mouse handlers overriding on child components
I made a MonoBehaviour that I use in the background object of every main UI panel. This component implements mouse handlers so I that I can do stuff like move the UI when dragging it or move the panel to the front of the screen when I click on it.
Over this panels I put many different objects like other panels, buttons, sliders, etc. It's important to notice that this subpanels don't implement the component mentioned before. If I click on a different UI element I still want the main panel (and all its child objects) to move to the front, but I don't want the drag and drop to work.
To block the unwanted methods from being called I made a different script that implements those methods with an empty body and attach it to every object that is supposed to block this calls. This does what I want but it's not very good performance wise, as the methods are still being called (the OnDragHandler
is called particularly many times).
My problem now is that if one of the subpanels implements a handler that I still want the main panel to call, it will only be called in the subpanel.
Is there a way to guarantee that the main panel method is called?
Is there a better way of doing the whole thing?
Answer by obsidianz · Apr 05, 2021 at 05:35 PM
I found a way to do this. It's not perfect but it works. Here's what I did:
To stop the unwanted methods from being called in the children objects I implement them with an empty body as said in the question.
I can't find a way to make sure that the other methods are always called, but as the only method I have is the OnPointerDown
I did the following:
First, I tag the main panel's gameObject with a custom tag. I used "MainPanel";
Then I create a script that is listening for mouse clicks in every frame.
When it detects a click, it casts a ray that detects every UI component under the mouse.
I loop through the list (it is ordered by the distance from the screen) and when I find the first object with the tag I call the method and break from the look.
Here's my code:
private void MouseLeft()
{
PointerEventData pointerData = new PointerEventData(EventSystem.current);
// Use Input.mousePosition if you're not using the Unity Input System package
pointerData.position = Mouse.current.position.ReadValue();
pointerData.pointerId = -1;
List<RaycastResult> results = new List<RaycastResult>();
// Save the EventSystem.current in a variable at Start
eventSystem.RaycastAll(pointerData, results);
foreach (RaycastResult r in results)
{
if (!r.gameObject.CompareTag("MainPanel")) continue;
r.gameObject.GetComponent<AppWindow>().BringToFront();
break;
}
}
I will not be marking this as the answer as I'm hopeful that someone else will find a better way of doing this.
Your answer
![](https://koobas.hobune.stream/wayback/20220613031809im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Change Mouse Icon on UI Hover 1 Answer
Hide cursor and stop interaction 1 Answer
Click on things? 0 Answers
How can I disable gamepad input, for my UI? 1 Answer
Prevent raycast to OnMouseOver() or OnMouseEnter() when mouse over UI 1 Answer