Unity3d Android Plugin



Through this series, I extensively use external links that I encourage you to read. While paraphrased versions of the relevant content will be included here, there may be times when the additional reading will help.

Learn how to create an Android Java Plugin for Unity3D that makes use of Toast popup messages. This video will demonstrate how to create a native Android Jav. Create a new Unity project Create the android plugin directory (Assets/Plugins/Android) Create a new xml file called AndroidManifest.xml in the Android folder (this is important- anywhere else and Unity won't.

Android

Beginning with Android plugins

Create a Lib Project on Android SDK. Create 1 main Activity Extends UnityPlayerActivity; Create others Activities that you need and add it into Manifest. Create resource, layout Export to JAR and add it into Unity by copy all JAR, resource file to Assets/Plugins/Android folder. A video showing how to create a simple android plugin for Unity3D. In this video you learn how to place Mobclix Ads in a Unity game. More detailed information can be found here.

Currently, Unity provides two ways to call native Android code.

  1. Write native Android code in Java, and call these Java functions using C#
  2. Write C# code to directly call functions that are part of the Android OS

To interact with native code, Unity provides some classes and functions.

  • AndroidJavaObject - This is the base class that Unity provides to interact with native code. Almost any object returned from native code can be stored as and AndroidJavaObject
  • AndroidJavaClass - Inherits from AndroidJavaObject. This is used to reference classes in your native code
  • Get / Set values of an instance of a native object, and the static GetStatic / SetStatic versions
  • Call / CallStatic to call native non-static & static functions

Outline to creating a plugin and terminology

  1. Write native Java code in Android Studio
  2. Export the code in a JAR / AAR file (Steps here for JAR files and AAR files)
  3. Copy the JAR / AAR file into your Unity project at Assets/Plugins/Android
  4. Write code in Unity (C# has always been the way to go here) to call functions in the plugin

Note that the first three steps apply ONLY if you wish to have a native plugin!

From here on out, I'll refer to the JAR / AAR file as the native plugin, and the C# script as the C# wrapper


Choosing between the plugin creation methods

It's immediately obvious that the first way of creating plugins is long drawn, so choosing your route seems moot. However, method 1 is the ONLY way to call custom code. So, how does one choose?

Simply put, does your plugin

  1. Involve custom code - Choose method 1
  2. Only invoke native Android functions? - Choose method 2

Please do NOT try to 'mix' (i.e. a part of the plugin using method 1, and the other using method 2) the two methods! While entirely possible, it's often impractical and painful to manage.

Unity3d

I wrote an Android plugin. Since I have to ask for GPS permissions I exported the AndroidManifest.xml file from exporting the project as and Android project. I then fixed the naming issues so that it will run (the package name and activity name needs to match Unity's internal naming).

Unity3d Android Plugin Tutorial

Now the problem is that the app icon is blank when the app is on my android device. I didn't change that part. It still says android:icon='@drawable/app_icon'

Unity3d Android Plugin

I am assuming that something needs to change in relation to this for it to work when I am not building in the Android SDK. I just have the manifest file in my plugins/android folder.

Unity3d Android Plugin Tutorial

Unity3d Android Plugin

Can someone help me please?