Sending data from Unity

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:

  1. 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.
  2. text – The actual text message to be sent.
  3. 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.

Tagged , , . Bookmark the permalink.

31 Responses to Sending data from Unity

  1. Saki says:

    How is the AndroidManifest.xml file for this look like?

  2. Lior Tal says:

    AndroidJavaClass derives from AndroidJavaObject (IDisposable).

    I believe that it should be properly disposed after usage (not seen in the example).

  3. alok says:

    Is this boo script
    can you convert it into c# script

  4. Kevin says:

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

  5. Kevin says:

    Because in my project have 2 plugin, so 2 manifest too!
    and I think it’s conflict

    • Ofer Reichman says:

      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?

  6. Kevin says:

    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!

  7. Q8GEEK says:

    Question:
    How can I share an image with that? (The image is loaded in Texture2D)

    • Ofer Reichman says:

      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.

  8. Q8GEEK says:

    So, just save the file as .Java and include it there in the Plugins folder?

    • Ofer Reichman says:

      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.

  9. Zell Bakalli says:

    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 ?

  10. DS says:

    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

    • Ofer Reichman says:

      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)?

  11. Tom Tong says:

    Hello.
    If I want to get picture from Android photo gallery and use it in texture, what is the direction for this issue?

  12. Sachin says:

    Can you please share a sample unity and android app so that I can follow.

Leave a Reply

Your email address will not be published. Required fields are marked *