Skip to main content

Creating Android Plugins For Unity

Creating Android Plugins For Unity

When creating a game / app with Unity which targets a specific platform like Android or iOS it would really handy if you could access some  platform specific features such as native pop-up windows ( modal dialogs ), battery status, vibration access, location access, file system access etc.. 

*Note : The following tutorial assumes that you know a bit about Android development and java in general. Even if you don't know android development I hope this tutorial shows you how easy and rewarding it is to make your own Android plugins.
Before we start you should make a folder called 'Plugins' inside the 'Assets' directory and inside that create a 'Android' folder.
This is where all our .aar / .jar files go which is basically a .dll file for windows folks.
We will be making a plugin that displays a Toast dialog which consists of a string we pass
from Unity and also a method that performs some math operation and returns the output to Unity.

Let's Start

  1. Open Android Studio and create a new project.
  2. Set up the minimum & target Sdk versions.
  3. We do not need an activity, So choose no activity.
  4. Now we have to create a new module.

    Then select 'Android Library'.

    Then set the library name.
  5. Now we have to create a new java class.

    And we create a singleton class.
  6. In the 'Helper' class we write the function that we will be calling from Unity side.
    package com.bitshiftprogrammer.androidplugin.testlibrary;
    import android.content.Context;
    import android.util.Log;
    import android.widget.Toast;
    public class Helper {
        private static final Helper ourInstance = new Helper();
        public static Helper getInstance() {
            return ourInstance;
        private Helper()
            Log.v("Unity", "Helper Object Created");
        public int ShowToastMessage(Context ac , String message)
            Toast.makeText(ac, message, Toast.LENGTH_LONG).show();
            return 1;
        public int PerformMath(int x)
            return x+10;
    This class consists of 2 functions both of which are public. Also the constructor of our Helper class prints out 'Helper Object Created' when it is called.
    These 'Log' statements can be seen in the LogCat of our Android studio when we have either a phone or an Android emulator running.
    *Note : int ShowToastMessage(Context , String) :-
    This method returns '1'. This is just there so that we can call this method from Unity ( AndroidJavaClass in Unity doesn't seem to support methods that return 'void' as far as I'm aware. 😐 ).
    We need to pass in a 'Context' as well, This can be given from Unity side by taking the current running activity's context ( We will see this being done soon ).
  7. Now we need to build this and get the .aar that was generated and put that in our Plugins folder in Unity.

    The .aar can be found at Project Location > Your Library > Build > Outputs > aar.
  8. Now the Unity side of things.
    using UnityEngine;
    public class AndroidPluginManager : MonoBehaviour
        const string pluginName = "com.bitshiftprogrammer.androidplugin.testlibrary.Helper";
        static AndroidJavaClass pluginClass;
        static AndroidJavaObject pluginObject;
        static AndroidJavaObject context;
        void Start()
            pluginClass = new AndroidJavaClass(pluginName);
            pluginObject = pluginClass.CallStatic<AndroidJavaObject>("getInstance");
            AndroidJavaObject currentActivity;
            AndroidJavaClass UnityPlayer;
            UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            currentActivity = UnityPlayer.GetStatic("currentActivity");
            context = currentActivity.Call("getApplicationContext");
        public void ShowTimeToast()
            pluginObject.Call<int>("ShowToastMessage", context, ("Time is = " + Time.time));
        public void PeformAddByTen()
            Debug.Log("From Unity : Called perform math : " + pluginObject.Call<int>("PerformMath", 14));
    Here we use the 'AndroidJavaClass' & 'AndroidJavaObject' which are predefined classes to access native Android functionalities.
    First we have to find 'Helper' class that we made, So for that we have to pass in the complete path of the class by including it's package name.
    In this example the package name is : 'com.bitshiftprogrammer.androidplugin.testlibrary' and the class name is 'Helper'.
  9. The 'AndroidJavaClass' object would be created like this:
    pluginClass = new AndroidJavaClass("com.bitshiftprogrammer.androidplugin.testlibrary.Helper").
    After we have found the class, we then need to get an instance of that class ( object ).
    Since this class is a singleton, An object of that class has already been created and we can get it by calling the 'getInstance' static method of the 'Helper' class.
    We can call static methods in a class by using the 'CallStatic<return type>("method name")' of an 'AndroidJavaClass' object.
    This is done like this : pluginClass.CallStatic<AndroidJavaObject>("getInstance")
  10. Now that we have access to the object we can now call the methods. In order to call these methods we use the 'Call<return type>(param object[])' method available for a 'AndroidJavaObject' object. In this method any number of parameters of any type can be passed in.
    So for the 'PerformMath' method we are giving an integer '14' and the method returns '14 + 10' as the output which we receive in Unity.
    And for the 'ShowToastMessage' method we pass in our current context ( not going into what a context is. 😁 ) and the message we want the toast to display.
Toast Example
The final step is just calling these functions in Unity. I have attached them to simple Unity buttons.
Logcat printed statements
You just have to build and run it on your Android phone. To see the Logs that are generated just connect your phone via USB while Android Studio is opened and check the Logcat.

That's it! Hope you learnt something. Support Bitshift Programmer by leaving a like on Bitshift Programmer Facebook Page and be updated as soon as there is a new blog post.
If you have any questions that you might have about shaders or unity development in general don't be shy and leave a message on my facebook page or down in the comments.
For more Unity development tutorials, go : HERE
For Shader development tutorials, go : HERE


  1. Unity 3D is highly preferred for its extended support to 27 platforms. The app developed and deployed can be easily shared between PC, web and mobile platforms. Besides, the agile methodology enables speedy prototyping and constant releases, which in turn speed up the game development in UK.

  2. Thanks for the useful information, give more updates like First time I visit your site really nice, here after a daily visit.
    ecommerce website development company in chennai

  3. νŒŒμ›Œλ³Ό,νŒŒμ›Œλ³Όμ‚¬μ΄νŠΈ,νŒŒμ›Œλ³Όμ •λ³΄,νŒŒμ›Œλ³Όλ‰΄μŠ€,νŒŒμ›Œλ³ΌλΆ„μ„,νŒŒμ›Œλ³Όκ²€μ¦,λ™ν–‰νŒŒμ›Œλ³Ό | νŒŒμ›Œλ³Όν΄λŸ½ νŒŒμ›Œλ³Ό.


Post a comment

Assets Worth Checking Out


Curved Surface Shader [ Unity Implementation ]

Curved Surface Shader This is the shader that we will be having at the end of this tutorial.
 The curved surface shader is capable of achieving really varied visual effects from showing space-time curve due to gravity to a generic curved world shader that is seen in endless runners like Subway Surfers.
The concepts that you learn here can open you up to a new way of looking at shaders and if you didn't think they were the coolest thing ever already, hopefully let this be the turning point.😝.

Both the examples show above use the same exact material is just that different values have been passed to the shader.
Start by creating a new unlit shader in Unity and we will work our way from there.
First we define what the properties are:
_MainTex("Texture", 2D) = "white" {} _BendAmount("Bend Amount", Vector) = (1,1,1,1) _BendOrigin("Bend Origin", Vector) = (0,0,0,0) _BendFallOff("Bend Falloff", float) = 1.0 _BendFallOffStr("Falloff s…

Introduction To Replacement Shaders & Shader Keywords

What is a replacement shader? A replacement shader is a shader that gets applied to every object being rendered.
Since the camera determines what objects end up being shown on screen, The functionality for setting up replacement shaders are in the camera class as well.

A good use case of a replacement shader would be in making effects like SSAO.
Here we need access to the normals and the depth information so a replacement shader that displays only the normals can be rendered ( stored in a render texture ) and then another shader that displays the depth information ( stored in a render texture ) and then the final image is rendered with the SSAO effect by taking the two render textures as input and doing a bunch of calculations.

Another use case would be to visualize the environment differently for various reasons like how they did in City Skylines.

The function that performs shader replacement is:
Camera.SetReplacementShader( Shader shader, string replacementTag ) Takes in a shader as …

Toon Liquid Shader - Unity Shader

Toon Liquid Shader This is how the shader will end up looking :
This shader is pretty neat and somewhat easy to implement as well as to understand. Since we will be adding some basic physics to the toon water as it is moved about we will have to support that in the vertex shader as well.
So let's start by looking at the properties :
Properties { _Colour ("Colour", Color) = (1,1,1,1) _FillAmount ("Fill Amount", Range(-10,10)) = 0.0 [HideInInspector] _WobbleX ("WobbleX", Range(-1,1)) = 0.0 [HideInInspector] _WobbleZ ("WobbleZ", Range(-1,1)) = 0.0 _TopColor ("Top Color", Color) = (1,1,1,1) _FoamColor ("Foam Line Color", Color) = (1,1,1,1) _Rim ("Foam Line Width", Range(0,0.1)) = 0.0 _RimColor ("Rim Color", Color) = (1,1,1,1) _RimPower ("Rim Power", Range(0,10)) = 0.0 } Just the usual stuff that we are used to. The only thing that may stand out is the [HideInInspector] tag, This works j…

Access Reflection Probe Data For Custom Shaders

The Unity shader documentation regarding reflection probes is pretty minimal and not at all comprehensive.
This short tutorial is intended to bring reflection probe functionalities to the forefront your future shader writing endevors which is a fancy way of saying "Look at this cool stuff and go and use it somewhere" 😏
Here we will try just the bare minimum of making a shader that reflects the cubemap data from reflection probe and displays it on the object.

These reflection probes are basically objects that store a complete image of the environment surrounding it into a cubemap which then can be read by shaders to create various effects.
More information on how reflection probes work in Unity can be found here :
Using Reflection Probes In Unity

I am not going over how to set up Reflection Probes here only how to access them inside our custom shaders.
So this is what we will be making:
The reflection probe takes in the cubemap only if it is within it's range otherwise i…

How To Animate A Fish Swimming With Shaders

Animate Fish Swimming With Shaders We are going to make swimming animation by using only shader code.
By the time we are done, it's going to look like this.
You will probably need the fish model used in this tutorial, that can be found HERE. Can use your own model but the shader code might have to be modified accordingly because of the orientation of the model that you might be using ( issues with whether the X axis & Z axis is flipped ).
The shader used way out performs a similar scene with skeletal animations applied on the fish models.
On a previous benchmark I did comparing the shader animation with the skeletal animation there was a difference of 28 FPS( on average ) with 50 fish.
The shader we are going to make is really powerful and flexible and don't think that it's limited to making fishes swimπŸ˜€.

So this mesh oriented like this when imported into unity and this is important to understand because this means that the model's vertices have to be moved along the X-…