In the second part of this tutorial series, we are going to override Unity. So what is the purpose or benefits of overriding Unity player and what can you accomplish with that?
- You can socialize your game!! You can make Facebook or Twitter logins, or better you can add achievements, multiplayer etc.. with Google Play Game Services, cool stuff!
- Maybe want to use Google Maps?
- Accessing hardware camera, croping images
- Implementing sqlite database, app analytics for your game, push notifications and list goes on like this.
Now lets start with easy stuff. Actually Unity’s documentation is pretty well explains how to do it. You can check the document from here: http://docs.unity3d.com/Documentation/Manual/PluginsForAndroid.html . I’ m also going to cover that document again. Before we begin I have to say again I’m using Eclipse for java development, I recommend you to do so, and also I assume that you have already made your Android sdk setup.
- To override Unity player first you need “classes.jar” that contains Unity classes. You can find that jar inside /Applications(or ProgramFiles)/Unity/PlaybackEngines/AndroidPlayer/bin folder. Copy it somewhere you can find it easily.
- Next open up the Eclipse and create a new android project or we can use our previous project from last tutorial. Name your activity whatever you want I prefer GameActivity usually. After creating your project, right click on your project and open properties from package explorer. Select “Java Build Path” from left and select libraries tab. Click on “Add External Jars” and choose your “classes.jar” file that you have copied.
- Now open your activity class(I named it MainActivity for this tutorial) and instead of extending “Activity class” extend it with “UnityPlayerActivity”, now you can use UnityClasses. After that delete “setContentView(int) from your “onCreate” method you won’t be needing that line. And print something to console.
- You have now successfully overrided Unity player. Like last tutorial select your “MainActivity.java” file and export it as .jar document into your “Plugins/Android” folder.
- One last to be able to run this on Unity Android. You need to also override your AndroidManifest.xml file. If you export as Android Google Project from build settings you can find your manifest inside from the eclipse project or from Unity docs .It should be like this:
- Just don’t forget to change your ActivityName and your package name(see the image above). Save the xml and add it also to your “Plugins/Android” folder inside Unity. It’s that simple… Build and run your application and you will get a log text “Overrided activity” when your app starts.
You can download and take a look at the files from here : https://www.dropbox.com/sh/gguo3pigy132ctq/gpQuDlMrM4
On next tutorials I’ show some useful methods for your android game and how to call them. Cheers…
If you like this article please support my work on Patreon, thank you!
Last screenshot is small to view, and its great keep it up, thanks.
The path for classes.jar now inside the data folder:
C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androidplayer\bin
Sorry, I am new… I received some error in the MainActivity.java..when following this quide
Iayout cannot be resolved or is not a field (R.layout.activity_main)
and I have to import android.os.Bundle; and andrroid.util.Log to fix some errors…but I can’t see them imported in the example…am I doing something wrong?
You need to delete the line, “setContentView(R.layout.activity_main);” to make it work actually.
And on Eclipse you can click Ctrl+Shift+O for auto importing so you don’t need to worry about those.
I want to open android activity on Unity button click, please provide me the tutorial for that.