Unity Android Plugin Tutorial- Making a toast (update)

This tutorial is not going to be an update to my previous post actually. I’m going to show a different method, an easier one. On my previous post here, I showed how to make a toast by making a library project on Android and overriding Unity. But I received some comments that it might get a bit confusing. So for those who doesn’t want to override Unity’s current activity and of course doesn’t want to make an android library project here is the easy way.

First go ahead and download my example repository from Github. My repo contains just a Unity project.

The example project contains an example scene with a demonstration with a simple GUI and a script that calls native methods for;

  • Making a toast message.
  • Showing Alert view.
  • Facebook and Twitter sharing and also a share intent.
  • And immersive mode code for the phones that supports it.(with Unity5 it’s been enabled by default)
  • WebView example.
  • And two extra methods about your installed applications(isApplicationInstalled and openApplication).

Just import UnityAndroidExtras package to your own project and you’re good to go. Inside demo folder check /Scripts/Demo.cs script!

This tutorial is simple explanation of the folder called “GoogleAndroidExport_experimental”. There is a package inside the folder you can add it to your own project. It’s an interesting method thats why I’m writing this tutorial:) Anyway you can just build and run the demo scene from /Demo/Scenes/Test_build_run.

First a short explanation for to-do list for this tutorial:

  • Write our plugin script from Unity which calls java methods.
  • Export our project as Google Android Project option selected from build settings.
  • Import the project into eclipse, and navigate to src/yourpackagename/UnityPlayerNativeActivity.java
  • Add java methods that we are calling from Unity(makeToast, shareOnTwitter etc…) inside UnityPlayerNativeActivity.java file.
  • Run as Android Application on your phone.

We are adding our java methods into freshly build google android project thats why build and run option from Unity won’t work! And yes every time you export your project you have to add your methods into UnityPlayerNativeActivity.java unfortunately.

Hack: For this I made a post process build script with some System.IO methods that changes unity exported UnityPlayerNativeActivity.java automatically after your build is done, so that we will never deal with copying and pasting same methods over and over. You can find the java file we are copying inside UnityAndroidExtras/Editor/Files folder. You can make your own changes add your own methods inside it. The only thing you need to do is export it and run it from Eclipse thats all!

Experimental project in detail,

1- Writing Unity Plugin Script

I’m not going to explain this part in details, if you look at my previous tutorials you can easly understand the work here. From this script we are just calling our java methods. You can find this script inside /GoogleAndroidExport_experimental/UnityAndroidExtras/Plugins/ folder.


2- Exporting as Google Android Project

The demo project contains all java codes that we will call, inside /GoogleAndroidExport_experimental/UnityAndroidExtras/Editor/Files folder. You can take a look at UnityNativePlayerActivity.java file there. You can find this java file if you export the project as google android project under src/yourpackagename/ folder. What I did was taking this file and adding the java methods inside this and putting under Assets/Editor/Files folder. Folder name is irrelevant actually it’s what I choose.

Then I just wrote a postprocess script that swaps our java file with Unity that builds after you export the project. You can check out the api documentation from PostProcessBuildAttribute from here.

Note(irrelevant with tutorial): I’m copying also .xml files inside /Editor/Files folder if you have any. You don’t need to mind that it’s just, if you are using Google play services in your project and having your own AndroidManifest.xml file with a integer line of Google play services version inside and try to build it form/Plugins/Android folder, well Unity gets mad so I prefer storing my Manifest.xml file and swapping it. Too lazy to copy and past:)


Only thing you have to do is hit export button and everything will be done automatically. I have added a unitypackage inside the Assets folder, if you want to use it for your project the only thing you have to do is exporting as Android project!

3- Running Your Project From Eclipse

  • Right click on your workspace, hit import.
  • Select Android application and select your Unity exported project and hit ok.
  • After import make sure your imports are all done and you have no error.
  • Right click on your project and run as Android Application, thats it !

Note:Just beware if you are using any plugin that overrides unity’s current activity this shortcut solution might not work for you !


15 thoughts on “Unity Android Plugin Tutorial- Making a toast (update)

Add yours

  1. This is awesome, thank you !

    For info, your demo project works with Eclipse ADT. -I imported your demo project in Unity, built for Google Android and imported in Eclipse ADT and each button did the expected thing.

  2. I was wrong the import in Android Studio works just fine. I was puzzled because there is no layout and also made a mistake during import. Just make sure during the import project in Android Studio to import the correct folder. You’re then good to go in Android Studio.

    1. May i know which folder did you import? cause i face some problem importing it. Thanks in advanced!

      1. You just need /Plugins folder and /Demo folder to use this package. There is a Demo.cs script inside Demo folder, you can check the examples from there

    2. @nevzatarman Your plugin can function very well in Unity and I wanna thank you so much as you’re so awesome! My previous question was actually asking how to open the eclipse file in Android studio.. I got many unknown errors 😦 Hope you can help~~

      1. Hi thanks for the comment, if you can tell me the steps you have done and send me the errors I can help maybe. But there is no extra work to do to open your eclipse project in Android Studio you just need to import and it handles everything automatically.

  3. Yes it works for Android Studio, but the downside of using Android Studio with Unity is since Unity exports Eclipse ADT project you need to re-import the project every time into Android Studio.

  4. Hello nevzatarman,

    Thank you so much for your turorial….
    more then 3days am looking for tutorials to create a toast message in my app which is in unity…..

    Now i have one problem can you please help me …..


      1. thank you very much nevzatarman…….. now i would like to store some time between two presses….i.e initialise a variable to store current time in millis on first back button press and show message to press once again to exit, and if next back button press is within two seconds of first press, then exit app.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Website Powered by WordPress.com.

Up ↑

%d bloggers like this: