• 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
1
Question by snowconesolid · Dec 24, 2012 at 06:50 PM · androidonmousedownremotemonobehavior

Why does OnMouse work with android remote?

I am very confused with something at the moment.

I know functions like OnMouseDown or OnMouseUp do not work on android or iphone builds.

I am making an android app, and with the unity-android remote I tested all of the OnMouse functions and they all work. They work the same but instead actually read in touches.

but when I build to android and test it on my phone it no longer works.

So I dont understand why it works with the android remote but not in an actual android build. can someone shed some light please?

why does it not just work the same way in the final build as it did with the remote?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Dec 24, 2012 at 11:33 PM

The android remote app is just a remote control for the unity editor. You actually play the game in the editor, so all works as usual in the editor. The unity runtime for android and i OS doesn't do the automatic ray cast like standalone builds or in the editor. You have to do the ray casting yourself on mobile devices.

I'm currently on my nexus tablet so writing isn't that easy :) but here's a script that you can attach to the main camera and all the OnMouseXXX functions should also work in an android / iOS build

 // MouseEventsGenerator.cs
 using UnityEngine;
 
 public class MouseEventsGenerator : MonoBehaviour
 {
     #if !UNITY_EDITOR
     Collider current = null;
     Collider drag = null;
     
     void Update()
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if(Physics.Raycast(ray, out hit))
         {
             if(current == null)
             {
                 current = hit.collider;
                 current.SendMessage("OnMouseEnter");
             }
             else
             {
                 current.SendMessage("OnMouseOver");
                 if(Input.GetMouseButtonDown(0))
                 {
                     current.SendMessage("OnMouseDown");
                     drag = current;
                 }
                 if(Input.GetMouseButtonUp(0))
                 {
                     current.SendMessage("OnMouseUp");
                     if(current == drag)
                     {
                         current.SendMessage("OnMouseUpAsButton");
                         drag = null;
                     }
                 }
             }
         }
         else
         {
             if(current != null)
             {
                 current.SendMessage("OnMouseExit");
                 current = null;
             }
         }
         if(drag != null)
         {
             drag.SendMessage("OnMouseDrag");
         }
     }
     #endif
 }


I haven't tested this since I wrote that from scratch on my tablet(took me ages :D ). Basically it should do the same thing that unity does under the hood for standalone builds but on mobile devices.

Note: I added preprocessor tags so it only affects build versions and not the editor. Don't use this script on a platform that already has those functions called automatically.

edit

Note: this will only work with colliers. GUIElements will not cause any of those messages. They have to be tested manually. I guess most don't use them anymore but if so, use the HitTest function of each GUIElement.

Comment
Add comment · Show 4 · 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 Bunny83 · Dec 24, 2012 at 11:38 PM 0
Share

Damn, I'm glad chrome didn't fuck up my post... I think this took at least half an hour :D. Tablets are clearly not made for program$$anonymous$$g, but it's funny it's possible.

$$anonymous$$erry Christmas :)

avatar image snowconesolid · Dec 25, 2012 at 02:48 AM 0
Share

This is a beautiful script! Its just what I needed. $$anonymous$$uch props for typing this all up on a tablet.

I think this should go on the wiki. I know there is a script that gets On$$anonymous$$ouseDown working already on the wiki but this one is 100 times better since it allows all On$$anonymous$$ouse functions.

Just needed to change Input.Get$$anonymous$$ouseUp(0) and Input.Get$$anonymous$$ouseDown(0) to Input.Get$$anonymous$$ouseButtonUp(0) and Input.Get$$anonymous$$ouseButtonDown(0)

in this part of the script

             if(Input.Get$$anonymous$$ouseDown(0))
             {
                 current.Send$$anonymous$$essage("On$$anonymous$$ouseDown");
                 drag = current;
             }
             if(Input.Get$$anonymous$$ouseUp(0))
             {
                 current.Send$$anonymous$$essage("On$$anonymous$$ouseUp");
                 if(current == drag)
                 {
                     current.Send$$anonymous$$essage("On$$anonymous$$ouseUpAsButton");
                     drag = null;
                 }


other than that it runs excellent. Thank you Bunny83.

Happy holidays.

avatar image Bunny83 · Dec 25, 2012 at 11:12 AM 0
Share

Yes, I'm sorry I got that wrong :)

I will fix my answer

avatar image tanveer19 · Apr 23, 2014 at 08:37 PM 0
Share

Thank you so much Bunny83. Yours answer helped me a lot save my day :) Excellent script to play with. You should get an award for that really :) Thank you.

avatar image
0

Answer by LawyerOfGod · Jan 01, 2014 at 04:17 PM

Strange... i have tested my game with OnMouse functions and all of them work fine on Android Builds... (i mean, those functions work on Unity Remote and on the app Build)...

in fact, i´m confused on what should i use... but both seem to be the same..

have you tried with a Debug.Log event if OnMouse is working fine on your app build ?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

How can i make my game look better on unity remote 4? 2 Answers

Remote 5 android Mac not working (after much research) 5 Answers

Unity Remote 5 android doesnt work.,Unity Remot 5 android doesnt work 2 Answers

transform.localScale very slow on device (contrary to Android Remote) 2 Answers

Problems with Android Build after updates 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