- Home /
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
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.
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 On$$anonymous$$ouseDown. I was wondering if there are any differences between those two.
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 $$anonymous$$onoBehaviour at some point, so anyhting On$$anonymous$$ouseDown 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.