Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity3d integration with android

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?

like image 214
leshka Avatar asked Oct 13 '11 05:10

leshka


People also ask

Is Unity compatible with Android?

Unity's strong partnerships with the leading mobile platforms – including custom tools for iOS and Android – ensure you have access to cutting-edge solutions that power your success.

Can we integrate Unity with Android studio?

First, go to create a new unity 3D project; if you add any package click here. Go to file >> Build Settings. Choose the Android Platform. If you try to run your game from another platform you can follow the same procedure then click the build and run button.

Can I use Unity ads in Android app?

Using Android StudioAdd a new module and import the unity-ads. aar file. Name the module "unity-ads", for example. Right-click on the module in the project view, then select Open Module Settings > app, and add "unity-ads" module as a dependency.


1 Answers

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); 
like image 154
spatulamania Avatar answered Oct 05 '22 23:10

spatulamania