- Home /
How to pass ArrayList from Java to Unity C#
I keep finding answered (and many unanswered) questions on how to pass from Unity C# to Java but not the other way around.
So in Unity Plugins/Android i have an aar library that i'm creating as a wrapper for an sdk. The android app made in unity has to connect with the other device over bluetooth.
In Android Studio i have a PhoneBlueToothActivity where i turn bluetooth and discovery on then i fetch the bonded devices and put their names in an ArrayList and i first want to pass this on to my UnityActivity and then from there to Unity
PhoneBlueToothActivity
Intent sendDevicesToUnity = new Intent(PhoneBluetoothActivity.this, UnityActivity.class);
sendDevicesToUnity.putStringArrayListExtra("bondedDevices", bondedDevices);
UnityActivity
public void AndroidSendDevicesToUnity(){
Intent getBondedDevices = getIntent();
ArrayList<String> list = getBondedDevices.getStringArrayListExtra("bondedDevices");
//Todo: pass this to Unity
}
I'm new to using Android studio, Java and so far i've used UnityPlayer.UnitySendMessage but that can't pass an arrayList of strings so i'm not sure how to handle this and there isn't much to find on this topic.
Should i put the arrayList into 1 string, use SendMessage and then filter it out or are there better ways?
Have you tried just using a string array instead?
Sorry I have not got any experience with java/ c# interop but I would assume the more basic the type the more chance they are going to be supported.
Send$$anonymous$$essage only takes int, float and string but you definitely have a point, i'll see if i can get it to work with that approach.
I'm also encountering the same issue.
On the Java side, I'm using a USB SDK to enumerate USB devices attached to Android. I instantiate the library on the Unity side using AndroidJavaObject
and then I do a call to a method that returns a java.util.ArrayList
of device names. From that point in Unity code, I have an AndroidJavaObject
representing a java.util.ArrayList
that I don't know how to get it's elements from. It's an ArrayList of java.lang.String
, so in Unity that has to also be treated as a AndroidJavaObject
. But then from that point I don't know how to actually get the string
data from it.
Sorry, but we can not address your specific problem as you posted your question as "Answer" to this old question. If you want an answer to your specific problem, ask a seperate Question and add more details about your problem. What code do you have so far, what exactly do you want to do and where exactly are you stuck.
Answer by Bunny83 · Oct 01, 2021 at 07:07 PM
Since this question was bumped already, I'll post an answer.
An ArrayList is a Java class, so it can not be directly translated or converted to a C# managed class or array. The easiest solution may be to call ToArray on the ArrayList on the java side which will create a Java native array of the elements. With this it should be possible to use AndroidJNIHelper.ConvertFromJNIArray to actually convert the java array into a C# array. Of course this only works if the types can be marshalled. Though converting java strings to C# strings should work.
If this doesn't work for some reason, you can always "use" the ArrayList as it is. However every interaction with the Java class has to be done through the AndroidJavaObject instance of that ArrayList. For example you can call the size method of the ArrayList to get the element count. Then in a loop you can call the get method with a valid index to get one element at a time.
So assuming you have a reference to your ArrayList as AndroidJavaObject called "arrayList" in C#, you should be able to do something like:
int count = arrayList.Call<int>("size");
List<string> list = new List<string>(count);
for(int i = 0; i < count; i++)
{
list.Add(arrayList.Call<string>("get", i));
}
Though using the JNI bridge there are generally a lot things that can go wrong. So makes sure you wrap everything in a try catch block and also dispose the objects properly.
What if I created a Java ArrayList from the UNity side like so: var arraylist = new AndroidJavaObject("java.util.ArrayList");
Would I still be able to add
or get
elements to/from it?
I'm testing this now, and I seem to get a soft crash everytime I call add
or get
Again this is something completely different from what was asked here. The question is at the top. If you have a question you want to ask, asky your own, seperate question. As I said in the comment above, you should be more clear what you actually want to do in your question.
This is totally related to the original question and your answer that this is a comment of. Your answer suggests using the get
method on an AndroidJavaObject
of an ArrayList
. I'm here telling you that doesn't work because it seems to throw a silent error.
Your answer
Follow this Question
Related Questions
Facebook Deep Link in Unity3D equal to null 0 Answers
How to enable location dialog? 0 Answers
Android Native Audio Adapter - Load file to MediaPlayer 0 Answers
Disable/Enable Bluetooth on Android (NATIVE) 1 Answer
FacebookSDK Closes Every Action 1 Answer