While reading the Unity blog post about the newest features in Unity 2018.2, this little blurb piqued my interest and made me want to immediately test it out:
You now have the ability to add .java (as well as .cpp and .a) source files to Unity project plugin folders. These files will be recognized as Unity plugins and compiled into the APK without requiring the user to build libraries separately in Android Studio. The plugin code remains a part of the Unity project, eliminating the need to create a separate Android Studio project.
Coming from an Adobe AIR background where native code had to be packaged into .ane files, or even previous versions of Unity where plugins needed to be packaged as .aar or .jar files, this seemed like a godsend.
If you’d like to test it yourself, here’s a real small Android project to get you started. First make sure you have a project created in at least Unity 2018.2 and the build platform set to Android. Next drop this raw .java file anywhere into a Plugins directory within your Assets (note: when I say anywhere, I mean anywhere except the obvious Plugins/Android directory. That directory is treated specially by Unity and should be avoided when using raw .java files. I used the directory Plugins/Java, personally).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
/** * Copyright (c) 2018 Devon O. Wolfgang * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.onebyonedesign.unityplugins; import android.app.Activity; import android.util.Log; import android.app.AlertDialog; import android.content.DialogInterface; import android.view.WindowManager; public class TextAlert { private static final String TAG = "TextAlert"; /** Create a new TextAlert */ private TextAlert(){} /** Get Singleton instance of TextAlert */ public static TextAlert getInstance() { return SingletonHelper.INSTANCE; } /** Display passed string in Alert Dialog */ public void show(Activity a, String msg) { Log.i(TAG, "Showing alert ("+msg+")"); AlertDialog d = new AlertDialog .Builder(a) .setMessage(msg) .setPositiveButton("OK", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }) .create(); d.setCancelable(false); d.setCanceledOnTouchOutside(false); d.getWindow() .setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); d.show(); } // Inner class singleton helper private static class SingletonHelper { private static final TextAlert INSTANCE = new TextAlert(); } } |
As you can probably guess, this is just a simple Singleton that will display an Android AlertDialog when the show() method is called. In the Unity editor Inspector window, be sure the selected platform for the plugin (i.e. the raw .java file) is Android.
Next, wherever you typically add your C# scripts, add this static class that will actually make use of the TextAlert.java singleton.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/** * Copyright (c) 2018 Devon O. Wolfgang * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ using UnityEngine; public static class TextAlert { /// <summary> /// Show the passed string in a native alert dialog /// </summary> public static void Show(string msg) { #if UNITY_ANDROID && !UNITY_EDITOR using(var playerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { AndroidJavaObject activity = playerClass.GetStatic<AndroidJavaObject>("currentActivity"); using(var pluginClass = new AndroidJavaClass("com.onebyonedesign.unityplugins.TextAlert")) { pluginClass.CallStatic<AndroidJavaObject>("getInstance") .Call("show", new object[] { activity, msg }); } } #endif Debug.Log("TextAlert.Show("+msg+")"); } } |
Then this littleĀ MonoBehaviour component class can be added to a GameObject in your scene.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
/** * Copyright (c) 2018 Devon O. Wolfgang * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ using UnityEngine; public class UI : MonoBehaviour { /// <summary> /// On button click event handler /// </summary> public void OnButtonClick() { // Called from UI Button TextAlert.Show("Howdy, World."); } } |
Finally, just create a button and wire it up so that it calls the UI.OnButtonClick() handler and, touch wood, you should get a native Android alert dialog similar to the example below when you build and run on an Android device.
May not be the most exciting demo ever, but this new 2018.2 feature should radically cut down on round tripping time for plugin development. Now, to just figure out how to add code completion for java files in Visual Studio and I’m set.
Recent Comments