Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by obsidianz · Apr 05, 2021 at 11:00 AM · uimousehandles

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

206 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges