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!
- Importing unity generated eclipse project into 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)
- 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!