• 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
Question by ConfusedExpert · Feb 24, 2015 at 11:02 AM · inputperformanceeventevent-handling

OnPointerDown vs OnMouseDown

Hello,

I'm trying to understand how the new EventSystem works and how I can use it to make my projects better. Is there any difference between using the OnPointerDown method (implementing the appropriate interface) and using the OnMouseDown? Maybe some performance stuff?

Also, If I have a 20 copies of an object, which share the same script with the same OnPointerDown/OnMouseOver method, is there a way to keep their functionality but not having to attach every single one of them said script? Should I be trying to do something like this? I'm interested in performance issues that may arise.

Thank you,
Asolmanx

Comment
jethrogillgren
webjaros

People who like this

2 Show 0
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

  • Sort: 
avatar image

Answer by sniper43 · Feb 25, 2015 at 10:19 AM

If you read the API, you would see that OnPointerDown inherits from the UI.Selectable class and OnMouseDown inheris from MonoBehavior.

So the answer to that is that you use OnPointerDown on UI objects, while using OnMouseDown for in scene objects.

For your second question: You can create an empty parent object, then child as many objects as you like. Assuming those objects have the same component, you can then modify those components.

The easiest way to get children in said parent that have for example have a struct or class Variables attached would be (C# example)

 class SomethingOnParent
 {
 
  void Update()
  {
    foreach(Variable var in gameObject.GetComponentsInChildren<Variable>())
    {
      var.Handle();
    }
  }
 
 }

where the variable has this in it's script

 class Variable
 {
  public void Handle()
  {
    transform.position += transform.forward;
  }
 }


The above wirtten script will move every child object forward for 1 unit every frame.

You can use

 foreach(Transform allTransforms in transform)

to get all chilren transforms (but this returns the parent's transform as well, so handle that appropriately).

If you have any questions about coding, feel free to ask.

Comment
Kiwasi
SwingWren
webjaros
canklot

People who like this

2 Show 2 · 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
avatar image ConfusedExpert · Feb 25, 2015 at 04:33 PM 1
Share

What I meant was that, by implementing the IPointerDownHandler interface, and using a Physics raycaster and the event system, you can write a "OnPointerDown(PointerEventData data)" method that seems to work just like the OnMouseDown. I was wondering if there are any differences between those two.

avatar image sniper43 · Feb 26, 2015 at 09:02 AM 1
Share

I've already told you. They belong ot different libraries. That means they have different handling. If you don't need the overhead of UI.Selectable, don't use OnPointerDown. UI.Selectable inherits from MonoBehaviour at some point, so anyhting OnMouseDown can do, so can OnPointerDown. But OnPointerDown "assumes" that the object it's attached to is a UI.Selectable object, so you can use appropriate methods for that.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

EventType.MouseDrag not working in WebPlayer 0 Answers

checking for keyboard input with exceptions 0 Answers

replace Input.GetKey for mobile input 1 Answer

[Editor] Get Scroll Wheel while dragging 0 Answers

Emulate RightArrow GUI Event 0 Answers


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