- Home /
OnMouseDown and mobile?
Everything that I have looked up on subject of using OnMouseDown to trigger touch events on mobile says you should be using the touch input class and raycasting instead of OnMouseDown to detect touches, even unity says there could be performance issues in the console using OnMouseDown on mobile. I tried testing a game out using both methods, OnMouseDown and the touch input class with raycasting and saw zero change in performance at all on a bottom of the line dual core 512mb Android phone. If I am seeing no performance change are there any disadvantages to using OnMouseDown for mobile?
Answer by valyard · Sep 10, 2015 at 03:37 PM
When building a game for a mobile device Unity examines all scripts and DLLs in the project for ANY mouse events and sets usesOnMouseEvents
variable to true if it finds at least one. This variable is then used in player loop on device to start or skip mouse handling routines. This means that if you don't have any OnMouse* functions in your code these routines will be skipped not wasting CPU cycles.
The thing is that depending on your setup these routines may be costly. When usesOnMouseEvents
is true Unity does the following:
Calls a method from C++ to C# which...
For all cameras in your project checks if mouse cursor is within their rects;
Uses
camera.GetComponent<GUILayer>()
in case there are IMGUI controls on screen and queries it if so;Does a 3D raycast;
Does a 2D raycast;
Performs a dance to figure out what OnMouse* messages to send;
Sends messages using SendMessage (which is not fast);
Do you want this code running in your game every frame just because you find using OnMouseDown easier than working with touches and raycasting manually? Your choice.
Thanks a lot for the explanation, I had no idea if usesOn$$anonymous$$ouseEvents was true all this was going on, I was under the impression that ts would only run code if On$$anonymous$$ouseDown was true. I'm definitely gonna gonna just use touch input and raycast now. thanks again!
Do you know if the this loop is continuously being executed or if it's only when the gameobject/monobehaviour in question is active? I have raycasting implemented in my mobile game, but not currently in the menu, so was thinking of using On$$anonymous$$ouse* only in the menu.
@valyard could you tell us how did you learn all this? I can't find any useful references to usesOn$$anonymous$$ouseEvents using google.
Answer by chomps32 · Sep 11, 2015 at 03:54 AM
OnMouseDown() is like calling a RayCast in every object that has that method in it when you tap the screen. This is a lot of calculations when you have a lot of clickable objects. For instance: I am creating a tile based game that instantiates 64 tiles. If the Tile class had an OnMouseDown() method, the phone playing it would be doing calculations for 64 different rays. To avoid this I created a ClickableObject interface that has a Clicked() method. I then use a single class that casts one ray and if it hits a ClickableObject it calls the Clicked() method on that one object.
This is wrong. It's only one raycast per camera until an object is found. This object then gets On$$anonymous$$ouseDown message.
Answer by Landern · Sep 10, 2015 at 01:20 PM
You will probably be fine, as a matter of fact Input.simulateMouseWithTouches defaults to true. With that said however you should probably target using compiler directives(is this windows or is this a mobile device) and use the proper Input properties as this would allow you to be more future proof(deprecation of the simulation of mouse events with touch) and direct with using the appropriate input for a given device. This may not apply to your implementation and a single up to three touches may suit you just fine.