- Home /
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.
I know you said you dont want to build plugins.. but really i dont see why not?
$$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.
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
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
otherwise have a look at the SharedPreferences in Android.. Unity might be doing the same thing with it as it does with NSUserDefaults
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)
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 :( ).
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
And is there a way for "Unity to android"? Thanks for your input :)
Thank you so much for this...the fog is now lifted :). I'll get back after implementing this for Android.
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)
hello,
And what about if I would like to comunicate an Android Service is continually running and Unity?
Regards, Jordi
@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
Follow this Question
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