Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by udaanparvaz · Aug 23, 2012 at 09:11 AM · android

Two way Communication between Unity and Android

I am gonna start off with a bit of a history. I and my friend have developed an Ios app that uses Xcode Native UI as a front end and unity player as the window where the magic happens. To communicate between Xcode and Unity we used the convenient hack of PlayerPrefs on Unity side and NSUserDefaults on Xcode. We only passed some integers and a string in between unity and Xcode which is quite fast at processor level(that's what I think).Plus we used a text file to write something at Xcode end and read it at Unity end.

Now same structure is needed to be implemented on android but I think that PlayerPref hack is only available for Ios.Is there a way of two way communication between android and Unity.In our Ios app the Unity sends a sting at a per frame basis through the PlayerPref which is observed at Xcode end. The time interval of message transfer is very narrow so I am not considering the android plugin route as it will require polling at both sides which will be too heavy.

Please share some ideas and point me towards right direction as I am a newbie and don't have an idea of all the tools out there...

Thanks for the Help!

Edit - Does the Plugin Support Provide a real two way communication? I mean can I send a string from unity to .jar...?coz somewhere on a blog-post I read you can't do it this way only the other way around that is .jar to unity.

Comment
Add comment · Show 10
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 SolidSnake · Aug 23, 2012 at 09:48 AM 0
Share

I know you said you dont want to build plugins.. but really i dont see why not?

avatar image udaanparvaz · Aug 23, 2012 at 09:54 AM 0
Share

$$anonymous$$y main requirement is to send a string to java at a per frame basis. That string basically includes the name of the image the camera of the phone is detecting. So this has to be per frame. Plus my UI will be built at Android side. Is there a way of sending a message to Android from unity.(I can deal with variables going from android to Unity functions). Something similar to the hack of "PlayerPrefs" and "NSUserDefaults in IOS" are same.

avatar image SolidSnake · Aug 23, 2012 at 09:58 AM 0
Share

you can send a message from Unity to Android with a plugin dev... its two way communication. have a look at the doc: http://docs.unity3d.com/Documentation/$$anonymous$$anual/PluginsForAndroid.html

avatar image SolidSnake · Aug 23, 2012 at 10:01 AM 0
Share

not sure how it will impact the performance.. but if you are sending small amount of Data per frame it should be ok i guess

avatar image SolidSnake · Aug 23, 2012 at 10:06 AM 0
Share

otherwise have a look at the SharedPreferences in Android.. Unity might be doing the same thing with it as it does with NSUserDefaults

Show more comments

2 Replies

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

Answer by SolidSnake · Aug 24, 2012 at 09:39 AM

Do you mean the plugin route or the SharedPreferences route? In the plugin route yes you can send string from Unity to Android.. just try the examples provided by Unity

==EDIT== iOS example for plugin:

//inside .mm file

 @implementation MyObjCClass
 //.... 
 -(void)displayText:(NSString*)text
 {
    NSLog(@"%@",text);
 }
 @end
 
 static MyObjCClass* myObject = nil;
 extern "C"{
   void SendText(const char* myStringFromUnity)
   {
      if(myObject == nil)
          myObject = [[MyObjCClass alloc] init];
 
      [myObject displayText:[NSString stringWithUTF8String:myStringFromUnity]];
   }
 }

//inside unity file

 [DllImport ("__Internal")]// this will be the name of your library instead in Android
 public static extern void SendText(string myString);

//then you can call the function

 sendText("my string from Unity");


Similarly you should be able to do the same with Android.. just follow the examples at the bottom of the documentation. (either with NDK alone or the one with Java)

Comment
Add comment · Show 7 · 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 udaanparvaz · Aug 24, 2012 at 10:19 AM 0
Share

I was talking about plugin route. As I don't know about plugins so I was browsing this(http://randomactsofdev.wordpress.com/2011/08/19/accessing-the-android-compass-through-unity-3d/) tutorial for android plugin to get the values of gps to measure speed and show it on unity side. One guy asked this precise question of two way communication . Can you give an example for sending message from unity to .jar . As for the other way around I've seen the Unity Send$$anonymous$$essage and GetStatic("methodname"). These two what I think serve the same purpose("of setting a value to unity and getting a value from android side respectively").What I need is setting a value at android side. Please correct me if I am wrong. Plus I can't get my head around the script reference at Unity(coz am too much of a newbie here :( ).

avatar image SolidSnake · Aug 24, 2012 at 10:29 AM 0
Share

I done it with iOS.. the same apply to android. UnitySend$$anonymous$$essage sends value from Android to Unity.. but you can create bridged functions from Unity to Android as well

I will edit my answer with iOS example of how to do it.. with android it should be the same logic.. the main difference with android is u need to do two bridges if you want to call Java API.. first bridge between Unity and Android ND$$anonymous$$, the second is ND$$anonymous$$ wrapper for the JAVA function.. the Unity examples cover both

avatar image udaanparvaz · Aug 24, 2012 at 10:32 AM 0
Share

And is there a way for "Unity to android"? Thanks for your input :)

avatar image udaanparvaz · Aug 24, 2012 at 10:53 AM 0
Share

Thank you so much for this...the fog is now lifted :). I'll get back after implementing this for Android.

avatar image SolidSnake · Aug 24, 2012 at 10:55 AM 0
Share

:-) good luck

Show more comments
avatar image
0

Answer by SolidSnake · Sep 27, 2012 at 09:31 AM

Do you mean the plugin route or the SharedPreferences route? In the plugin route yes you can send string from Unity to Android.. just try the examples provided by Unity

==EDIT== iOS example for plugin:

//inside .mm file

 @implementation MyObjCClass
 //.... 
 -(void)displayText:(NSString*)text
 {
    NSLog(@"%@",text);
 }
 @end
 
 static MyObjCClass* myObject = nil;
 extern "C"{
   void SendText(const char* myStringFromUnity)
   {
      if(myObject == nil)
          myObject = [[MyObjCClass alloc] init];
 
      [myObject displayText:[NSString stringWithUTF8String:myStringFromUnity]];
   }
 }

//inside unity file

 [DllImport ("__Internal")]// this will be the name of your library instead in Android
 public static extern void SendText(string myString);

//then you can call the function

 sendText("my string from Unity");


Similarly you should be able to do the same with Android.. just follow the examples at the bottom of the documentation. (either with NDK alone or the one with Java)

Comment
Add comment · 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 jarenas · Jul 01, 2013 at 07:51 PM 0
Share

hello,

And what about if I would like to comunicate an Android Service is continually running and Unity?

Regards, Jordi

avatar image SolidSnake · Jul 15, 2013 at 10:52 AM 0
Share

@jarenas in principle it should be similar.. didn't try it myself though. bare in $$anonymous$$d Unity part of the app itself cannot run as service but you can receive the intent broadcast from the service ins$$anonymous$$d.

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

9 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

Andoroid Pictures 0 Answers

Does Unity have scroll menu for smartphone? 2 Answers

Upgrading to Unity 3.4 question 1 Answer

How to return from Unity Player to previous running Android Activity? 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