To build Unity projects for Android the Android SDK (Software Development Kit) needs to be installed. If you’re using Android Studio the SDK will be already part of that installation.
So far so good, but when Unity asks you to select the Android SDK root folder you’ll have a problem because it is part of the Android Studio package and therefore isn’t accessible to Unity.
Create Project in Android Studio. Create a project with no activity, click next few times and you are ready to code. Add Unity’s Library to your Project.
To work around this let’s create a symbolic link to the SDK’s root folder in our Applications root. Open a Terminal window and execute the following command:
The command
ln
used with the option -s
will create a symbolic link to the SDK location /Applications/Android Studio.app/sdk
as /Applications/Android SDK
.- After open the unity file in android studio it will build gradle. Then the gradle build finish, move the java and res file from your android studio project you want to combine with unity. Because you already copied the manifest file so, in this unity project android studio will follow that manifest file.
- Google is pushing Android Studio as main development IDE for all android application. Android Studio provides better interface for Android Application development. In my earlier post, I explained how to create a plugin using Eclipse. Now I will share how the same thing can be done in Android studio. First create an Android Studio.
Note that a backslash
before a space (e.g. SDK
) is escaping that space to prevent it from being treated as a separator, hence part of that directory.Voilà. Now we can select the Android SDK root folder when Unity asks us:
Unity3D offers a ton of great features for Android projects, but often there are gaps that must be filled with plugins.
Let’s take the example of Toast popups for Android. In your game you might like to show a popup message that explains an error, or states that you are signed in to a server.
Start by creating a new Android library project. If you’re like me, you’ll be using the command line with a text editor instead of an IDE.
In your newly created ToastExample.java file add the following source code:
There are a few things to note in the source code. Because we will be making use of the
AndroidJavaObject
to represent our instance, we need to create the instance and return it. The static function instance()
will create our instance if it does not already exist by calling the class constructor and then returning it for Unity3D to use. Beyond the required instance and constructor methods everything thing else is up to your imagination. In our case we will be creating a Toast popup that requires an activity context. You can either create a separate function like setContext
or require the context as a parameter for showMessage
.To include the plugin in a Unity3D game you must first compile and bundle it into a JAR file. Again if you’re using a command line to build you can add the following to your build.xml file:
From this point you can just run
ant jar
and you will end up with bin/TestPlugin.jarAssuming that you got no errors on the Java side of things, you can proceed to your Unity3D project. In your project create Assets/Plugins/Android if it doesn’t already exist. Unity3D knows to pick up all Android plugins from this location. Copy your TestPlugin.jar to the plugin location.
![Android Studio Unity3d Plugin Android Studio Unity3d Plugin](/uploads/1/1/8/5/118559972/453857329.jpg)
Inside your Assets/Scripts directory create a new C# script called UnityToastExample and include the following code:
Because we have decided to keep things simple and not extend the UnityPlayerActivity in our Android code, we need to get the current activity from the player. This is the only way to get the activity context required by our Toast.
Once we have the activity context we can load our class and create the
ToastExample
object instance. When the object instance has been created we are now able to call any public function from the class. The first thing we do is set the context that we got from our current activity. This is where it can get confusing. Toast popups are UI features so they are required to run on the UI thread. This can be done many different ways whether in the Java code or in the Unity code. In this example it will be done in the Unity3D code.![Android Studio Unity3d Plugin Android Studio Unity3d Plugin](/uploads/1/1/8/5/118559972/305139584.jpeg)
Anything in this particular
AndroidJavaRunnable
will be run on the UI thread.Attach the UnityToastExample.cs script to a
GameObject
and build for Android. If everything went smooth, you should see a Toast popup on your device at launch. Make sure to only run for Android as you’ll get errors for other platforms.Android Studio Plugins Menu
A video version of this article can be seen below.
Android Studio Unity3d Plugin Installer
Nic Raboy
Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.
Android Studio Install Plugin
Please enable JavaScript to view the comments powered by Disqus.