Unity Android Plugin Tutorial Series part3- Let’s make a toast

It’ s been a while since I wrote my last tutorial, but here we go. In this tutorial we will make a toast. If you don’ t know that a toast is, you can take a look at it from android developer’s website here. It’s pretty basic stuff but can be very useful to notify user about simple stuff like login failure or connection lost etc…

*Edit: Before we begin our tutorial, this is the third part of my android tutorials for  overriding unityplayer activity which includes some Java part, so if you don’t wish to do that there is a unity package solution for that you can download it from this repository. (You can grab the unity package from /Assets folder)

Let’ s begin, like I said before you can do much more than logging to DDMS by overriding “UnityPlayerActivity”. Making a toast for your android game is really simple we will just add a simple “makeToast” method to our MainActivity class on Java side and also within Unity3D. Go ahead and download project files from github. I tried to explain the details shortly with Readme file inside repository. Repository has two projects; One is Unity3d project and the other one is Eclipse projects(our plugin). Just clone the repository and take a look at the scripts. Besides making toast I added facebook and twitter share methods and also an alert view method, just take a look at the Test.cs script inside Unity project. Be sure to check Androidmanifest.xml (we are overriding unity’s current activity!).

I assume you are already familiar with Eclipse and Android development. If not you can read the basics from google’s own documents from here.

  • Import UnityProject into Unity3d and export it as Google Android Project!(we will use it with eclipse).
  • Import UnityEclipsePlugin from our repository into Eclipse and check android build path for class.jar(if path is not set correctly remove it and re-add it as external jar, You can find it inside the repository).(see image2).
  • Import unity generated eclipse project into Eclipse and open Android tab from it’s properties. From libraries section add UnityEclipsePlugin as Library.(image3).
  • (Not necessary for this tutorial)Make your own refactoring like changing package names.
  • Run the sample, thats it!

*I have also added GameActivity.jar into Unity Project, so you can now build and run from Unity editor for testing! If you are following the steps above you can delete GameActivity.jar file you don’t need it with Eclipse project.

PS: After importing unity eclipse project eclipse will give you a warning inside Androidmanifest.xml (see image below). You need to delete that line!

Screen Shot 2014-08-11 at 20.24.26 (2)
Debuggable error after importing unity project
  • Importing unity generated eclipse project into Eclipse.
Screen Shot 2014-08-11 at 20.16.44 (2)
Adding external jar(image2)
  • Import UnityEclipsePlugin from our repository into Eclipse and check android build path for class.jar(if path is not set correctly remove it and re-add it as external jar, You can find it inside the repository)
Screen Shot 2014-08-11 at 20.21.32 (2)
Adding library(image3)
  • Import unity generated eclipse project into Eclipse and open Android tab from it’s properties. From libraries section add UnityEclipsePlugin as Library. And run your project thats it.

As you can see, on java side makeToast method runs on a UIThread, it’ s necessary because toast or webView like elements runs on mainUI thread so we need a runnable to be able to see them. If you look at the C# code we are just calling makeToast method from our “AndroidJavaObject”. You may have noticed our AndroidJavaClass is using  “com.unity3d.player.UnityPlayer” and “AndroidJavaObject” is using “currentActivity”, that’s because of we have overrided our java class with “UnityPlayerActivity” and make it our launcher class.

You can download source files from here  from github . It ‘s really basic stuff so on the next tutorial I’ m planning to show how  to write a plugin for Google play game services and export and use Google Android project from Unity3D. Cheers!

If you like this article please support my work on Patreon, thank you!

Advertisement

6 thoughts on “Unity Android Plugin Tutorial Series part3- Let’s make a toast

Add yours

  1. Have you tried building .jar files as plugin for Unity in Android Studio. I haven’t concrete answers yet.

    Best regards,
    Swaroop

  2. Hey I am able to communicate with my android studio project which has a simple java class with some Getter functions inside it but I am still unable to start an activity which is also inside the android studio project.
    Presently, I am not providing any manifest file now from the android studio project. Do you have any idea how to write and provide the manifest file in unity so that the default unity manifest and the one provided gets merged?

    P.S : If I am providing the generated manifest file from the android studio. My app either doesn’t open or crashes. Not providing the same doesn’t create any issue. I feel something is wrong the way I should change the manifest file from android studio

  3. Ok as far as I understand for now you want to start an activity from Unity and to do so you created a library module from Android Studio and exported it as jar and trying to build your app from Unity.
    -First of all you can’t use .aar files unless you have Unity5(I don’t have but they say aar support is added).
    – If you want to open an activity from Unity since you added it inside your .jar need to declare it inside your manifest. If you are already using a custom manifest for your project just add it inside your application tag.
    – Otherwise you need to create a custom manifest for your project. First you need copy the Android Manifest that Unity generates when you compile your game, it’s on folder: YourProjectname/Temp/StagingArea/AndroidManifest.xml, copy from there to Assets/Plugins/Android folder inside Unity and add your activity of your library: <activity android:name="yourLibraryPackagename.YourActivity" into that manifest.

    Since your android studio project is a library it’s manifest is irrelevant. I’ll post answer to stackoverflow too.

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: