Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kronas · Oct 17, 2012 at 05:07 PM · c#androidjava

How to pass Java object to C#?

Hey Everyone,

I am attempting to implement some Java native code into my Unity project, and I want to be able to use a function in my Java code that passes back an object based on a class defined within my java code to my C# Unity code. So far I have been unable to do this.

Here is an example of the JAVA side code:

Inventory:

 public List<Weapon> weapon;
 
 public List<Weapon> getWeaponFromInventory(Context context)
     {
         activity = (Activity)context;
         activity.runOnUiThread(new Runnable(){
             public void run()
             {
                 if(myInstance == null){
                      Log.e("Instance cannot be null");
                       throw new IllegalArgumentException("Instance was null when attempting to getWeaponFromInventory(context)");
                 }
                 weapon = instance.myInstance.getWeaponFromInventory(activity);
             }    
         });
         return weapon;
     }

I am then calling on this code from my C# in this way:

PlayerWeaponHandler:

 private static AndroidJavaObject instance;
         // Create link to java based Inventory
     private static AndroidJavaClass inventoryPlug= new AndroidJavaClass("com.myth.player.Inventory");
     //Create link to Unity Player
     public static AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 
     //Get the current activity to use as context
     public static AndroidJavaObject currentAct = jc.GetStatic<AndroidJavaObject>("currentActivity");
     
     public static List<AndroidJavaObject> Weapon;
 
     static PlayerWeaponHandler(){
         if(Application.platform == RuntimePlatform.Android){
             Debug.Log ("PlayerWeaponHandler- Created");
             instance = inventoryPlug.CallStatic<AndroidJavaObject>("getInstance");
         }
     }
 
     public static void getWeapon(bool show){
         if(Application.platform == RuntimePlatform.Android){
              //Here I am attempting to call on a Java method that returns a Java Object holding the information about the 
             //players Weapon
             Weapon = instance.Call<List<AndroidJavaObject>>("getWeaponFromInventory", currentAct);
             return;
         }
         return;
     }

The error I get back is:

 I/Unity   (17813): JNI: Unknown signature for type 'System.Collections.Generic.List`1[UnityEngine.AndroidJavaObject]' (obj = System.Collections.Generic.List`1[UnityEngine.AndroidJavaObject]) equal
 
 I/Unity   (17813):  
 
 I/Unity   (17813): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
 
 I/Unity   (17813): 
 
 I/Unity   (17813): JNI: Unknown signature for type 'System.Collections.Generic.List`1[UnityEngine.AndroidJavaObject]' (obj = System.Collections.Generic.List`1[UnityEngine.AndroidJavaObject]) equal
 
 I/Unity   (17813):  
 
 I/Unity   (17813): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
 
 I/Unity   (17813): 
 
 E/Unity   (17813): getMethodID("getWeaponFromInventory", "(Lcom.unity3d.player.UnityPlayerNativeActivity;Z)") FAILED!
 
 D/dalvikvm(17813): GetMethodID: method not found: Lcom/myth/player/Inventory;.getWeaponFromInventory:(Lcom.unity3d.player.UnityPlayerNativeActivity;Z)
 
 I/Unity   (17813): JNI: Unable to find method id for 'getWeaponFromInventory'
 
 I/Unity   (17813):  
 
 I/Unity   (17813): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
 
 I/Unity   (17813): 
 
 I/Unity   (17813): JNI: Unknown return type 'System.Collections.Generic.List`1[UnityEngine.AndroidJavaObject]'
 
 I/Unity   (17813):  
 
 I/Unity   (17813): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)


Has anyone ever dealt with trying to do this and found a way to make it work?

Would love to know if the Unity community has an answer to this question.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kronas · Oct 17, 2012 at 05:55 PM 0
Share

Yes, I am talking native Java.

avatar image chiranjiv · Oct 15, 2018 at 06:32 PM 0
Share

Hi, I am facing the same issue, did you figure out any solution?

avatar image Bunny83 chiranjiv · Oct 15, 2018 at 06:58 PM 0
Share

There is no straight forward solution because Java's List class can not be converted to a .NET / $$anonymous$$ono List class. That means the method signature would need to look like this:

 Weapon = instance.Call<AndroidJavaObject>("getWeaponFromInventory", currentAct);

And "Weapon" wouldn't be a List<AndroidJavaObject> but just a AndroidJavaObject. Though this AndroidJavaObject would represent the Java List instance and to access anything from inside that list you have to use the appropriate methods of the Java List class.


You said you have the same issue so this should solve this problem. If you have a different problem, do not post comments on other questions. If you have a specific question, ask a seperate question and add enough details.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Yokimato · Oct 17, 2012 at 08:36 PM

To my knowledge there is no good interoperability between the JVM and the CLR. My suggestion would be to port over your Java code to C#. However, if you truly couldn't, you try some of the frameworks out there such as jni4net. I think you're going to suffer big performance penalties from this though. Good luck, you make be stuck porting your code over.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kronas · Oct 24, 2012 at 08:57 PM 0
Share

I have actually looked into converting my java code over to C#. However, there are some issues also with doing that. The system that I used for converting the code was I$$anonymous$$V$$anonymous$$.

avatar image Yokimato · Oct 25, 2012 at 03:03 PM 1
Share

I've never had a good experience with doing a language conversion by an automated tool. Is it out of the question, quantity wise, to do it by hand? A large amount should be copy-pastable due to vast syntax similarities.

avatar image Kronas · Oct 25, 2012 at 03:20 PM 0
Share

Unfortunately yes it would be completely out of the question to just copy and paste. We are talking about a million plus lines of code.

avatar image
0

Answer by chiranjiv · Oct 16, 2018 at 12:27 AM

So I was trying to do something similar,

Java Code:

 public List<WifiAccessPointsData> GetBestThreeWifiSignalStrength() {
     List<WifiAccessPointsData> bestThreeWifiSignalsData = GetDataFromAccessPoints();
     return bestThreeWifiSignalsData;
 }
 

Below is the C# code:

   public List<AndroidJavaObject> BestThreeSignalData()
     {
         List<AndroidJavaObject> bestThreeAccessPointsInfo = androidInstance.Call<List<AndroidJavaObject>>("GetBestThreeWifiSignalStrength");
         return bestThreeAccessPointsInfo;
     }

I was getting the following error:

 Exception: JNI: Unknown signature for type 'System.Collections.Generic.List`1[System.String]' (obj = System.Collections.Generic.List`1[System.String]) instance

Consider the below example:

I am returning a List from a java function in android to C# in Unity

Below is my java code:

  public List<String> GetListOfNames() 
 {
         List<String> newListString = new ArrayList<String>();
         newListString.add("C");
         newListString.add("K");
         return  newListString; 
 }

Below is the C# code in Unity

 public void GetNames()
 { 
 AndroidJavaObject newNameList = androidInstance.Call<AndroidJavaObject>("GetListOfNames");
         string newName1 = newNameList.Call<string>("get", 0);
         Debug.Log("new name: " + newName1);
 }

Output:

 10-15 19:01:02.954 23278-23350/? I/Unity: new name: C

Hope this example will help others.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Integration of Java,C++,C# in Unity 0 Answers

what programming language is used to create slender? 2 Answers

java android plugin worker thread ends up in Unity mainthread 1 Answer

Call a Android Studio method from Unity 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges