LogoLogo
  • Home
  • Projects
  • About
  • Contact

Raw Java Assets in Unity Project

Devon O. · July 15, 2018 · Unity3D · 0 comments
16

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).

TextAlert.java
Java
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.

TextAlert.cs
C#
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.

UI.cs
C#
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.

  Facebook   Pinterest   Twitter   Google+
androidjavaplugins
  • FIS Athlone && Warbler Update
    April 05, 2009 · 0 comments
    2186
    2
    Read more
  • FlashDevelop Update
    February 15, 2008 · 0 comments
    1664
    2
    Read more
  • Magnify – a jQuery Plugin
    December 11, 2011 · 5 comments
    2549
    9
    Read more

Leave a Comment! Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Devon O. Wolfgang

AIR | Unity3D | AR/VR

Unity Certified Developer

Technical Reviewer of “The Essential Guide to Flash CS4 AIR Development” and “Starling Game Development Essentials”

Reviewer of “The Starling Handbook”

Unity Engineer at Touch Press.

Categories
  • Actionscript (95)
  • AIR (16)
  • Flash (99)
  • Games (7)
  • Liberty (13)
  • Life (53)
  • Shaders (20)
  • Unity3D (21)
Recent Comments
  • MainDepth on Unity Ripple or Shock Wave Effect
  • Devon O. on Unity Ripple or Shock Wave Effect
  • Feral_Pug on Unity Ripple or Shock Wave Effect
  • bavvireal on Unity3D Endless Runner Part I – Curved Worlds
  • Danielius Vargonas on Custom Post Processing with the LWRP
Archives
  • December 2020 (1)
  • December 2019 (1)
  • September 2019 (1)
  • February 2019 (2)
  • December 2018 (1)
  • July 2018 (1)
  • June 2018 (1)
  • May 2018 (2)
  • January 2018 (1)
  • December 2017 (2)
  • October 2017 (1)
  • September 2017 (2)
  • January 2017 (1)
  • July 2016 (1)
  • December 2015 (2)
  • March 2015 (1)
  • September 2014 (1)
  • January 2014 (1)
  • August 2013 (1)
  • July 2013 (1)
  • May 2013 (1)
  • March 2013 (2)
  • December 2012 (1)
  • November 2012 (1)
  • September 2012 (3)
  • June 2012 (2)
  • May 2012 (1)
  • April 2012 (1)
  • December 2011 (2)
  • October 2011 (3)
  • September 2011 (1)
  • August 2011 (1)
  • July 2011 (1)
  • May 2011 (2)
  • April 2011 (2)
  • March 2011 (1)
  • February 2011 (1)
  • January 2011 (2)
  • December 2010 (3)
  • October 2010 (5)
  • September 2010 (1)
  • July 2010 (2)
  • May 2010 (5)
  • April 2010 (2)
  • March 2010 (7)
  • February 2010 (5)
  • January 2010 (5)
  • December 2009 (3)
  • November 2009 (1)
  • October 2009 (5)
  • September 2009 (5)
  • August 2009 (1)
  • July 2009 (1)
  • June 2009 (2)
  • May 2009 (6)
  • April 2009 (4)
  • March 2009 (2)
  • February 2009 (4)
  • January 2009 (1)
  • December 2008 (5)
  • November 2008 (2)
  • September 2008 (1)
  • August 2008 (6)
  • July 2008 (6)
  • June 2008 (9)
  • May 2008 (4)
  • April 2008 (3)
  • March 2008 (4)
  • February 2008 (9)
  • January 2008 (7)
  • December 2007 (6)
Copyright © 2021 Devon O. Wolfgang