Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
3
Question by theBdrive · Sep 10, 2015 at 01:17 PM · touchtouch controlstouch screen

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?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
10

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:

  1. Calls a method from C++ to C# which...

  2. For all cameras in your project checks if mouse cursor is within their rects;

  3. Uses camera.GetComponent<GUILayer>() in case there are IMGUI controls on screen and queries it if so;

  4. Does a 3D raycast;

  5. Does a 2D raycast;

  6. Performs a dance to figure out what OnMouse* messages to send;

  7. 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.

Comment
Add comment · Show 3 · 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 theBdrive · Sep 10, 2015 at 09:13 PM 0
Share

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!

avatar image jnt · Nov 15, 2017 at 05:10 AM 0
Share

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.

avatar image Vandell · Nov 29, 2018 at 01:30 PM 0
Share

@valyard could you tell us how did you learn all this? I can't find any useful references to usesOn$$anonymous$$ouseEvents using google.

avatar image
0

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.

Comment
Add comment · Show 1 · 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 valyard · Sep 11, 2015 at 11:28 AM 0
Share

This is wrong. It's only one raycast per camera until an object is found. This object then gets On$$anonymous$$ouseDown message.

avatar image
0

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.

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

6 People are following this question.

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

Related Questions

Using right version of .Net-Framework with Unity 5.2.3f1 in context of TUIO protocol and MS PixelSense-Table 0 Answers

Touch pressure not updated when stationnary 0 Answers

TouchInputModule "is no longer required"... 1 Answer

If using a touch screen computer, is it possible to test touch functions without using Unity Remote 0 Answers

Shump Touch Control 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