- Home /
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.
Hi, I am facing the same issue, did you figure out any solution?
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.
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.
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$$.
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.
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.
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.