I’m pretty new to Unity-Android integration, so I’m filled with pride after every little success. And I bet this one is pretty trivial, especially for Android veterans. But I’m posting it anyway – for the sake of Unity developers. There just aren’t enough examples out there that tie the pieces together.
I wanted my Unity app to have a share function. All it needs to do is send some text as is easily done on Android. All that is missing is a tiny Android plugin.
Android Plugin
I already covered the subject of creating an Android plugin in another post. I will expand on it to keep this post short.
MainActivity.java
Again I subclassed UnityPlayerActivity. This utilizes the main activity as context for starting a new intent. I then added a small method, shareText:
package com.companyname.appname; import android.content.Intent; public class MainActivity extends UnityPlayerActivity { public void shareText(String subject, String text, String chooserTitle) { Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject); sendIntent.putExtra(Intent.EXTRA_TEXT, text); sendIntent.setType("text/plain"); startActivity(Intent.createChooser(sendIntent, chooserTitle)); } }
shareText takes 3 string parameters:
- subject – The desired subject line of a message. Note that not all senders send the subject. Make sure that your text is sufficient by itself.
- text – The actual text message to be sent.
- chooserTitle – The title for the chooser dialog. It is displayed when the user is prompted to choose how to send the data. For example, it can be “Share via”.
The Unity App
Now all that is left is to call the shareText method from within Unity. This Boo method does the trick:
ifdef UNITY_ANDROID: static def ShareText(subject as string, text as string, chooserTitle as string): jc = AndroidJavaClass("com.unity3d.player.UnityPlayer") jo = jc.GetStatic[of AndroidJavaObject]("currentActivity") jo.Call("shareText", subject, text, chooserTitle)
This method simply calls the Java method as documented by Unity.
That’s the whole deal. Enjoy.
How is the AndroidManifest.xml file for this look like?
Pretty similar to the one in my previous post. Though you can probably do without 2nd “intent-filter” section.
AndroidJavaClass derives from AndroidJavaObject (IDisposable).
I believe that it should be properly disposed after usage (not seen in the example).
Wouldn’t Dispose be called automatically by the destructor?
Is this boo script
can you convert it into c# script
I translated it to Boo from Unity’s C# example, which is available here. (also linked from the post above).
I can’t do that!
this is my code in Unity:
void show(string sub, string txt, string chose)
{
Debug.Log(“Show function is calling”)
AndroidJavaClass plugin = new AndroidJavaClass(“com.example.androidplugin.MainActivity”);
AndroidJavaClass unityPlayer = new AndroidJavaClass(“com.unity3d.player.UnityPlayer”);
AndroidJavaObject activity = unityPlayer.GetStatic(“currentActivity”);
plugin.Call(“shareText”, sub, txt, chose);
}
but when I builded, it did work!!!
Sr but it didn’t work!!!
I think you should be calling “activity.Call” instead of “plugin.Call”. (and then you probably don’t need “plugin” anymore)
because I have two plugin, so it I call :
activity.Call I will recevice Exception sharetext not found 🙁
What shoud I do?
how to merge 2 manifest???
Can you have me?
In which plugin can “sharetext” be found? Call only that plugin.
I’m sorry, I don’t know how to merge manifests.
Because in my project have 2 plugin, so 2 manifest too!
and I think it’s conflict
I’m not sure your problem is the 2 plugins. There is a problem in the code that you posted. To help you fix it please answer me this: in which plugin can the function “sharetext” be found?
if I have 1 plugin, I can call “sharetext” function.
but when I have 2 plugins, my Manifest file:
http://codepad.org/eYQtyTED
it have duplicate in two activity: action android:name=”android.intent.action.MAIN”
so I think when I call activity.Call(“shareText”, sub, txt, chose); “shareText” not found!
Sr my English not good!
Try this code:
AndroidJavaClass plugin = new AndroidJavaClass(“com.example.androidplugin.MainActivity”);
AndroidJavaObject activity = plugin.GetStatic(“currentActivity”);
activity.Call(“shareText”, sub, txt, chose);
I don’t know about Android
but when I try:
http://codepad.org/UMz37bYD
it’s ok!
glad you got it working 🙂
Question:
How can I share an image with that? (The image is loaded in Texture2D)
I don’t know – haven’t tried that. Sharing an image from file seems easy. See this answer. Basically you use “EXTRA_STREAM” for putExtra and set the type to “image/jpeg” or “image/png”.
The tricky part is passing data from Texture2D. Perhaps this answer is the key. Passing a content URI can cause the sharing app to call your app to get the data. Then you can get it from Texture2D using EncodeToJPG or EncodeToPNG.
Best of luck.
Another embarrassing question:
I wrote the code I needed in Eclipse… How do I compile it and use it?
I’ve covered this in this post. It covers the Eclipse plugin side, the Unity side and how to add the plugin to the Unity project.
So, just save the file as .Java and include it there in the Plugins folder?
No, it’s more complicated. You need to build the plugin using Eclipse. The output is a JAR file, which you should copy to your Unity project (Assets/Plugins/Android). And you need to create an AndroidManifest.xml file in the same place.
Thnx for this post I found it very useful. I’m having trouble when trying to use some code that is imported from external jar in my java code and all I’m getting is NoClassDefFoundError. I know that I’m doing something wrong when exporting the jar file. Any idea how to fix this ?
I think you merely have to add the external jar to your classpath.
Hey, I want to connect two different Unity apps, and want to share one String from one app to another. May I know how can I do that ? I am new to the unity
1. Are the two Unity apps running on the same Android device?
2. You want to send a string when both are running (at the same time)?
Hello.
If I want to get picture from Android photo gallery and use it in texture, what is the direction for this issue?
hi.
sorry, I don’t know as I haven’t done this.
googled it for you – maybe these links can help:
Load PNG from external folder
Open gallery
Get filepath and filename of selected gallery image
Get file path of gallery image
Can you please share a sample unity and android app so that I can follow.