Simple Android plugin for Unity3D – Part 1

Writing plugins for android is not that hard as I used to think would be. Yes, there are certain steps that need to be followed. But overall, it’s a simple process.

This post covers how to setup android project to create a plugin for Unity3D, If you have already completed the steps in this post then you can continue reading next post – Simple Android plugin for Unity3D , Part 2.

So here are steps to write a simple plugin which will invoke a toast from android when called from C#.

Step 1: Setting up android studio project.

  • Open android studio and create a project ‘SimpleAndroidPlugin‘ with empty activity and click finish.

 

 

  • Goto File> New > New Module and add android library – ‘Simple Plugin‘.

 

 

  • Finally you android project will look something like this

 

 

  • ‘com.plugin.android.simpleplugin’ is the package where all action will happen

 

  • Create a java class name – ‘PluginClass’, in simpleplugin package by Right click> New> Java Class on the package. And paste the code as below in that class

 

  • Now about the code
    private static PluginClass instance;
    
    public PluginClass() {
        this.instance = this;
    }
    
    public static PluginClass instance() {
        if(instance == null) {
            instance = new PluginClass();
        }
        return instance;
    }

    PluginClass, as you can see is a singleton class and instance() method returns the instance of the class when called.

    public void setContext(Context context) {
        this.context = context;
    }

    setContext() is used to pass the context from the Unity3D so that it can invoke toast on that context

    public void showMessage(String message) {
        Toast.makeText(this.context, message, Toast.LENGTH_SHORT).show();
    }

    ShowMessage is called to show the toast from unity3D after all the initialization is done.

  • Here we have complete the android setup for calling toast from unity3D, all hard work is done now we have to build this project in order to create a jar and then use it in your android project.

 

  • Go to Build> Make Module ‘Simple Plugin’, after gradle is done building the jar right click on the ‘SimplePlugin‘ module in the project window and click ‘Show in Explorer‘.

 

  • Go to  SimpleAndroidPlugin\simpleplugin\build\intermediates\bundles\debug , here will find the classes.jar which we are going to use in Unity3d project to invoke toast.

 

Download this project or clone it from GitHub

 

In this post, we have seen how to create an android library i.e. classes.jar which can be used to show toast.

Please read the part 2 of this post – how to uses the lib that we have created in this post in unity3D android project.

 

If you are curious and want to know about this project, then leave a comment or email me at nipun18david@gmail.com

If you are interested to know more about the work I have done than do the same as above 😀

Please follow and like us:

One thought on “Simple Android plugin for Unity3D – Part 1

Leave a Reply