51

I need to create an android application which consists of parts written on Unity3d (animation and so on) and on AndroidSDK (by androidSDK I mean few activities written in java, manifest and resources)

In fact I already have those parts (At least mocks are ready=)) And I can't figure out how to communicate between them.

I've read a lot of articles about unity3d android integration (mostly about things called "plugins") and failed to understand how this staff works=( The main thing I understood (everyone exept official refs wrote about that) is that documentation about plugins is very poor... =)

The only way to communicate which I can understand is to make an intent (broadcast) from unity3d script (using AndroidJavaClass and AndroidJavaObject) so my activity (written in java) can handle it. But something tells me it's not the best solution...

What I read about the problem is that I need to use JNI (which is java native interface - WHY?!? - why do I need to use native code?)

Do I need to create some middle-level code on java which purpose is to communicate with my java activity, compile it using AndroidNDK and include into my Unity3d project as a plugin? How can I write that middle layer than? Do I need to use UnityPlayer instead of "Activity" as a base class and why?

Lex Li
  • 60,503
  • 9
  • 116
  • 147
leshka
  • 1,764
  • 6
  • 32
  • 42

3 Answers3

39

Here is a tutorial on the basics of running Unity inside of a normal Android app.

There is a great tutorial on running Unity inside of Android Views. Once you get this up and running you can start embedding scenes easily anywhere in your App.

When you need to call into the Java Android app from Unity, you can add this code:

AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

And then you can call any method you want on your activity through the activity AndroidJavaObject. Something like this:

activity.Call("yourFunctionName", parameters);
David
  • 15,894
  • 22
  • 55
  • 66
spatulamania
  • 6,613
  • 2
  • 30
  • 26
  • Hi @spatulamania, have you gone through this tutorial? I'm facing some problems trying to make it, shall I start a new question/chat? – mdelolmo Jan 25 '12 at 09:49
  • @mdelolmo it's been a while but yes, I have gone through the tutorial and everything worked fine. If you have specific questions about things not working you should probably ask a new question. The one piece of advice is to make sure that your standard Android development environment is working fine and to not skip any steps. – spatulamania Jan 25 '12 at 14:34
  • feel welcome to have a look to the [question I just posted](http://stackoverflow.com/questions/9022207/integrating-unity-with-eclipse-how-to-follow-the-official-tutorials-instruc). Any help would be greatly appreciated. – mdelolmo Jan 26 '12 at 17:12
  • Hi spatulamania running Unity inside of a normal Android app. link is not working. I have implemented your code but unfortunately I am unable to access method of Android activity from Unity. So Please share some more ideas or source project. Thanks in advance. – atrivedi Aug 29 '12 at 11:31
  • Take a look here [link](http://stackoverflow.com/questions/10701184/androidjniclass-unable-to-excute-my-non-static-function/13354999#13354999) for example of calls static and non-static activity functions from Unity3d and appropriate test project. – dvpublic Nov 13 '12 at 03:06
8

For fully supporting on Android, you don't need to create some middle-level code on java.

Instead, you should:

  1. Create a Lib Project on Android SDK.
  2. Create 1 main Activity Extends UnityPlayerActivity
  3. Create others Activities that you need and add it into Manifest.
  4. Create resource, layout…
  5. Export to JAR and add it into Unity by copy all JAR, resource file to Assets/Plugins/Android folder.

This method is helpful for writing Push notification, in app billing, etc…

Ref# here: http://docs.unity3d.com/Documentation/Manual/PluginsForAndroid.html (-> Extending the UnityPlayerActivity Java Code)

And in addition, by using below code:

AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
activity.Call("yourFunctionName", parameters);

You can do most of magic things with Android on Unity

Note*: yourFunctionName must be implemented inside your main activity which extend from UnityPlayerActivity (Look at step 2)

Regards,

Daniel X
  • 105
  • 1
  • 7
Frank Nguyen
  • 6,493
  • 3
  • 38
  • 37
  • 1
    Hi! Is there a way to do the same for iOS project with the step-by-step instruction like your? – vir us Aug 24 '14 at 15:37
  • unity player activity got changed recently. you can look at some assets from the asset store that replace the activity as well. – Lassi Kinnunen Mar 01 '18 at 04:37
  • @LassiKinnunen My answer's 4 years ago, so it's probably outdated. I'm not working on Unity anymore, I don't have opportunity to update answer, sorry! – Frank Nguyen Mar 01 '18 at 04:48
  • yep I was just commenting for the sake of someone finding this later, no probs. it's something i haven't had the time to look into myself either but what I do know is that one of my projects relying on this stuff doesn't compile on higher unity than 5.6 for android anymore. – Lassi Kinnunen Mar 01 '18 at 07:16
0

the unity3D in 2020 isn't needed android SDK at all nowadays. you can just click file -> building setting -> switch platform -> build.

and you can create your android app.