Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Ideas needed for debugging using Java code inside Unity

Discussion in 'Editor & General Support' started by awoods, May 20, 2010.

  1. awoods

    awoods

    Joined:
    May 20, 2010
    Posts:
    4
    I originally posted my question on the answers board:

    http://answers.unity3d.com/questions/8008/can-i-access-java-code-from-unity

    However, I believe my problem has progressed to where a discussion would be more useful, as a straight answer is not available. Please see the answers page for an exact description of what I have tried so far (includes code).

    Summarized, I have successfully converted a Java .jar file into a .net .dll using IKVM. I have then successfully accessed the .dll from a Unity3D c# script when running inside the Unity3D development environment. The problem occurs when I build a deployable version. In the deployed version, the connection to the Java code does not work. I don't get any errors, and the deployed application works, but it is missing the on screen counter that is the result of communication with the Java code.

    This is very similar to what happens if you build the example c++ plugin ( http://unity3d.com/support/resources/example-projects/texture-plugins ) for web deployment. Of course accessing c++ code from a web application isn't supposed to work. But what is disconcerting is that a silent failure occurs. The resulting web application is built and will run, but all of the portions that need to communicate with the c++ code do not work. There are no any error messages - I'd love to see a big, giant warning if you try to compile a project that uses a c++ plugin for the web telling you it won't work. Anybody know of any way to turn on such debugging (or where to look for the error messages)?
    I'm wondering if I'm hitting a similar problem, a silent failure for something that is not going to be possible. I'm trying to think of a way that I can prove whether or not this is the case.

    I would appreciate any suggestions on how to go about troubleshooting. Places to look for errors that I might not know about, debugging levels I could turn on, suggested strategies for identifying the problem...any discussion along these lines would be helpful.

    As background, I am an experienced programmer (including commercial game development in other engines), but relatively new to Unity3D. So feel free to lay complex ideas on me=)

    I would like to definitively prove whether it is or is not possible to access Java libraries from a deployed Unity3d application.
     
  2. Lucas Meijer_old

    Lucas Meijer_old

    Joined:
    Apr 5, 2008
    Posts:
    436
    Hey,

    You're defenitely off the beaten path here. Kudos for getting it to work in the editor itself :). You're correct in that we could be a lot better with our error reporting.

    The webplayer indeed does not allow arbitrary c++ code to run, and it very much sounds like your guess is on the money, in that it's the security model preventing your java communication from working. if you can get it to work in the editor, you can also very likely get it to work as a standalone player, but as a webplayer, I don't see how you can make it work, other than moving the stuff you are now doing in java, out of java and into c#.

    Bye, Lucas
     
  3. awoods

    awoods

    Joined:
    May 20, 2010
    Posts:
    4
    Hi Lucas. The project I am working on right now does not have the funding to port the substantial Java library (AI library) I am working with to C# right now. However, that could be a future possibility if we can prove the library would be useful to use in the Unity context.

    Having web deployment is desired, but not required. I would be very happy with getting the use of the Java library working in a PC standalone deployment. Great to hear that you think this may be possible. Do you have any specific suggestions on how to approach getting a PC deployment working? What should I look for or try?

    I'd be happy to post my helloworld java example if you'd like to toy with it.

    Another thought I had is that maybe the version of Mono used by IKVM varies slightly from the version that Unity3D is using. I am going to look into which version exactly IKVM is using this afternoon. For comparison, which version does Unity3D use? And is it a completely vanilla version, or has it had any in-house modifications made to it that might cause issues?

    Thanks,

    Angela
     
  4. awoods

    awoods

    Joined:
    May 20, 2010
    Posts:
    4
    I have done some further experimenting this afternoon and have had some success. I have been able to create a stand-alone windows deployment and a stand-alone Mac deployment that work. As Lucas indicated, the same fix does not work for a web deployment, as there are likely security restrictions in play there.

    Here is what I did to get it to work. I placed all of the IKVM .dll files in a folder called IKVM in the Assets folder of my project. This is sufficient for running inside the Unity Editor. I then built a stand alone web stand alone pc version. The stand alone versions have embedded in them my IKVM .dll files. For instance in the case of windows, they are in the myproject_data folder that is alongside the myproject.exe. If I run just like this, it does not work. However, if I place a copy of IKVM.Runtime.dll outside the myproject_data folder and in the same folder as the myproject.exe, then everything runs fine. Same for the Mac deployment. If I place IKVM.Runtime.dll in the same folder as the myproject.app then it works.

    I have a couple further questions to fully understand what is going on here.

    The key to figuring out my problem was finding the player log file (which as a Unity3D newbie I didn't know about). While no error messages go to the console, error messages do go to the player log. I used this very useful post (http://answers.unity3d.com/questions/2252/how-can-i-find-editor-log-file). When running Unity3D Pro installed on a Mac, I find the player log as indicated and it had this helpful error message:

    ** (process:2613): WARNING **: The following assembly referenced from data-0x19548000 could not be loaded:
    Assembly: IKVM.Runtime (assemblyref_index=0)
    Version: 0.42.0.3
    Public Key: 13235d27fcbfff58
    The assembly was not found in the Global Assembly Cache, a path listed in the MONO_PATH environment variable, or in the location of the executing assembly (/Users/awoods/work/unity/javapluginB/Deploy/mac/).

    This told me where I needed to put the .dll it couldn't find, which was great.

    I develop on a Mac that has Windows installed using Bootcamp (and Parallels able to run the Bootcamp install in a virtual machine). I have Unity3D Pro installed on Mac. But as a Mac newbie, it is useful for me to shunt back to windows for situations where I need my more extensive OS knowledge. So I also have the free version of Unity3D on the Windows side (in Parallels). In Windows, when I open up the same Unity3D project I had been working on from the Mac side, I don't have a "Player log" button on my console window, nor can I find the log file on disk. If I run a web deployment I do find the log file in the temp folder, but no log file when I run a standalone exe deployment.

    So is the "Player log" a feature of Unity3D Pro? Or is this a Mac/PC issue I don't understand? I will be bringing other developers on my team up to speed on this project soon and they will be developing on Windows machines without access to Mac hardware. In order to be effective, they will need to be able to see the Player log. So I want to understand if they need a Pro license to do so.

    My other question is in regard to the actual solution I found that works, which is a bit cobbled together. Is this my best option? To do the Unity3D build and then paste in an additional .dll at the top level? Or is there someplace else in my project that I could have placed my dll so that the Unity3D build alone would suffice? I tried numerous places, but have not had any success getting the Unity3D build process to place the .dll in a spot where it is useable. The current solution will get me by, but my innate engineering mind can't help but wonder if there is a better solution.

    Thanks,

    Angela
     
  5. Lucas Meijer_old

    Lucas Meijer_old

    Joined:
    Apr 5, 2008
    Posts:
    436
    Playerlog is not a pro feature, and should work across free/pro across platforms.
    I'm very surprised Unity is asking for the dll in the rootfolder. This actually looks like a bug. I'm glad you have it working though. good luck with the project.
     
  6. charles.rich

    charles.rich

    Joined:
    Mar 27, 2011
    Posts:
    14
    Hi Angela, Your last post on calling Java from Unity was about a year ago. Has this worked out ok for you? I am thinking of going down the same path and would appreciate either a go-ahead or any warnings.

    Thanks in advance,

    -CR
     
  7. awoods

    awoods

    Joined:
    May 20, 2010
    Posts:
    4
    Hi Charles. I have not made any further progress on this effort. While I was able to use IKVM to create a .net dll out of my Java code, trying it with Unity 2.x it did not work. My investigation let me to believe it was due to a lib mismatch in Mono. The version of the the Mono libs used by Unity3D version 2.x were too old to work with the version in use by the latest release of IKVM. And while I could get some simple things to work, the more advanced areas of my application did not work. The hope was that when Unity3D 3.0 was released, it would contain a more recent version of the Mon libs which would be compatible. However, while waiting for the Unity3D release, the project direction shifted and I never got back to it. So you might have better luck if you try now with the latest version of Unity3D, but I have not had a chance to try it.

    Good luck!

    Angela
     
unityunity