Let’s start by creating a simple customary “HelloWorld” that uses AlertDialog and Toast.
Before we start let’s make sure that AndroidBridgeEngine is enabled for both Editor and Android Platforms. Select AndroidBridgeEngine.dll present in Assets/Plugins/AndroidBridge/Scripts:
And in the inspector make sure both Editor and Android platforms are selected, like so:
Now, inside a new Unity project with AndroidBridge lite or pro installed. Go to Window/AndroidBridge.
This should launch AndroidBridge editor window that is used to add Android types that need to be bridged with Unity. For our project these are the five “main” types that we need:
All the dependent types of these main types will be automatically bridged. This is how your AndroidBridge editor window should look like.
Now press the Bridge button and wait for the progress bar to complete while the required types are added to your project. The generated types are added at Assets/Plugins/AndroidBridge/Scripts/Generated.
You can see the generated code follows the familiar java folder package structure for easy accessibility. Plus a lot more types have been bridged and added to these folders than the 5 we originally manually added.
These are all the types that our required types depend upon either for polymorphism and/or method calls to and from Android. AndroidBridge automatically determines these types and adds them.
[Important] Keen among you might have noticed that namespace names starts with small letters like in Java unlike capitalized letters used in C#. This convention is followed in the code as well e.g. method names of the java classes bridged in Unity start with small letters. This helps in two different ways:
- Developers can almost always copy paste the android code in Unity with little to no changes.
- Developers at all times are aware that which method calls correspond to Unity and which to Android. This helps them use and call them appropriately e.g. Avoiding android calls in main Unity thread and/or putting it in Update method etc.
Now let’s write some code and see how to use these types correctly inside Unity.
Create a new C# Script, let’s name it MainScript and drag it to “Main Camera” to attach and launch it once the app launches.
From line 2-7 we add the namespaces/packages required for the code.
[Important] AndroidBridge.Initialize() call must be made on the main Unity thread unlike other Android/Java calls which can be executed on any thread like from lines 19-22 which are executed in a new thread just to emphasize this point.
[Note] AndroidTerminal.ActivityContext returns current activity as object which needs to be casted into an Activity for any subsequent calls into the Activity. Needless to say but in order to do that make sure android.app.Activity is one of the bridged types.
The AlertDialog and Toast that we need to show need to run on the UI thread in Android. So, we call context.runOnUIThread which takes java.lang.Runnable to call back and it’s one of the two interfaces we implement in MainScript.
Now current android activity will call run method in UI thread. Rest of the code is self explanatory but there are still some things to take note of. Once we enter a text in the Dialog it shows a Toast to say hello.
This was easy, wasn’t it? You now have the power of the full Android inside Unity.
Now, let’s look at some of the conventions and important notes that one should keep in mind while developing in AndroidBridge.