- Home /
Can I access Java code from Unity?
I have an existing computational library that does high level AI reasoning in Java. Can I connect my Java library for use in a Unity game? If so, what is the best way to do so?
I have read the documentation on plugins (http://unity3d.com/support/documentation/Manual/Plugins.html). It is very clear that this can be used to connect to external code in c++. However, the phrase "Practically any language or development environment that can create DLL files can be used to create plugins" got my attention.
If I could compile my Java code into a dll, then I could use it with Unity? Has anyone tried this?
After some research I found IKVM.NET (http://www.ikvm.net/), whose stated purpose is:
" an implementation of Java for Mono and the Microsoft .NET Framework. It includes the following components:
A Java Virtual Machine implemented in .NET A .NET implementation of the Java class libraries Tools that enable Java and .NET interoperability " Specifically, it discusses using Java APIs in .net applications (http://www.ikvm.net/devguide/net2java.html) and indicates it can "Convert a Java API to .NET CIL using ikvmc. This produces a .NET dll"
So if I produced a dll using IKVM.NET, could I then access my Java library from C# unity code? Would I need to use the plugin procedure described in the documentation for connecting to c++ code? My understanding is that the plugin procedure is only necessary for accessing unmanaged dlls. But I believe that the dll produced by IKVM.NET would be a managed dll, so it should be able to be placed directly in my assets directory? Any thoughts on if this is correct and/or possible?
I am very interested in hearing from anyone with advice on how to connect Unity to a Java library.
Thanks,
Angela
Answer by awoods · Apr 23, 2010 at 11:03 PM
For those following along. The answer is yes - it works! Here is what I did:
Created a simple java library that looks like this:
public class ExampleLibrary {
private int n = 0;
public ExampleLibrary() { }
public String HelloWorld() { String s = "Helloworld: " + n; n++; return s; }
}
Compile it into a jar file (by any way you like, I used Eclipse).
Use ikvmc to convert the jar file to a .net .dll using the instructions here: http://sourceforge.net/apps/mediawiki/ikvm/index.php?title=Tutorial
Place my .dll, along with the ikvm dlls into my Unity project's Assets folder.
Added a GUIText object to my camera. Then added the following simple .cs script to my camera:
using UnityEngine;
public class JavaHelloWorldScript : MonoBehaviour {
ExampleLibrary exampleLibrary = new ExampleLibrary();
void Start() {
}
void OnGUI () {
string text = exampleLibrary.HelloWorld(); GUILayout.Label(text);
}
void Update() { }
}
When I run, this places a string in the upper left corner of my screen that says Helloworld:X, where x is a continuously incrementing number.
So looks like I am good to go with bringing in a real Java library for use.
I have hit a snag. This works fine while I am inside the Unity3D editor. However, if I build a deployable version, it no longer works. I have tried deploying to PC, $$anonymous$$ac & Web. All have the same result, no errors, and the deployed version runs, but the "Helloworld:X" counter does not display. Any suggestions? Any places I should look for error messages? Any way to add some debug instrumentation that might help identify the problem? I need to deter$$anonymous$$e if it is possible to deploy in this way, or if it is something that is not going to be possible. Any suggestions would be appreciated.
Hi there,
We tested this method and after a few hiccups got it working in editor and on a Nexus One running Android.
We didn't try it on a PC, $$anonymous$$ac or the web.
-Nicholas Bellerophon, Game Designer, Jirbo Inc.
Hello. Could you please post some more detailed information how you got it to run? I followed the above tutorial but now I get this error in Unity: Assets/$$anonymous$$G1 Assets/Scripts/UI/$$anonymous$$yJavaPlugin.cs(5,9): error CS0246: The type or namespace name `ExampleLibrary' could not be found. Are you missing a using directive or an assembly reference?
All the DLL files (my own and all the DLL files from I$$anonymous$$V$$anonymous$$ are in the "Standard Assets" folder of Unity and the .cs script I am using is in the /Scripts/UI/ folder in Unity. Thank you.
Do I need to copy ALL of the I$$anonymous$$V$$anonymous$$ DLL files to the Assets folder? This would be 29 files in my case. Is this correct? Can I also put them in a sub folder within the Assets folder in Unity?
Does it work using Unity basic? or do I need the Pro version? Thanks
To anyone who tries this in the future:
Angela is right, it works perfectly fine in the Editor, you just need to bring in all 27 I$$anonymous$$V$$anonymous$$ dll's. But for me, when I went to build the game, it complained that it couldn't find some missing .net dll's. To fix this, go to:
File->Build Settings->Player Settings->Other Settings and change "Api compatibility level" from "2.0 Subset" to "2.0".
Note: This may only work on Windows (not too sure)
Note 2: This will not work for a WebPlayer
Answer by Lucas Meijer 1 · Apr 17, 2010 at 09:38 AM
IF LKVM.NET is able to create a .net java VM, that is able to run your javacode, chances are very good it will run within Unity. (I have no idea how fast that java vm would be though).
You would not need to use the "plugin" system, as you are correct that that is only for unmanaged dll's, and the output of LKVM.NET would be a managed dll.
Usually with as exotic routes as these, the only way to realy find out if it works, is to try it.
Thanks for the input. I am going to give it a try as soon as I get a chance.
Answer by nader_me · Nov 28, 2011 at 05:36 AM
Hello... I am new to unity 3d, in fact this is my first trial to this software. I want to build a virtual world using unity 3d, and also add a reasoning to the character in the virtual world. As a result of some search I found some agent reasoning programming languages (such as jade, jason, jack,...) which are build over java programming language.
Does anybody try to integrate agent reasoning language with unity 3D character? what is the best agent reasoning language to use with unity 3d?
I also tried to follow the above steps to integrate dll file, I create folder called (Plugins)and paste the dll file ExampleLibrary.dll along with 27 dll files of ikvmc , but I get error (Assets/JavaHelloWorldScript.cs(8,9): error CS0246: The type or namespace name `ExampleLibrary' could not be found. Are you missing a using directive or an assembly reference?)
does anybody know why I get this error?
Thanks in advance
Your answer
Follow this Question
Related Questions
Problem reading sensor plugin, event won't trigger 2 Answers
Now that Unity 4.1.2 broke the Android plugin examples, what do you use to learn them? 1 Answer
Andriod export problem 0 Answers
AndroidJavaException: java.lang.SecurityException: Requires VIBRATE permission 0 Answers
[IKVM] Unity goes crazy after the first compilation 1 Answer