Gin Rummy Indian Rummy Nine Men's Morris and more Air Attack

Friday, 12 June 2015

Setting fields of an android java object from unity C#, including array

Lets say we want to set fields of a Java object in Unity with different types, including array.

Let the class in java be,
package com.example; 

public class Address {
 int mHouseNumber;
 String mStreet;
 String mCity;
 String mCountry;
 String [] mPhoneNumders;

Now lets create Address object from c# and populate its fields.
First of all lets create an Address object
AndroidJavaObject addressObj = new AndroidJavaObject ("com.example.Address");

Now populate all the fields except the mPhoneNumders.
addressObj.Set<int> ("mHouseNumber", 10);
addressObj.Set<string> ("mStreet", "abc");
addressObj.Set<string> ("mCity", "def");
addressObj.Set<string> ("mCountry", "ghi");

Now lets create a java object from the c# array so that we can set the field mPhoneNumders. Lets create a small method for it.
private AndroidJavaObject javaArrayFromCS(string [] values) {
    AndroidJavaClass arrayClass  = new AndroidJavaClass("java.lang.reflect.Array");
    AndroidJavaObject arrayObject = arrayClass.CallStatic<AndroidJavaObject>("newInstance",
                                             new AndroidJavaClass("java.lang.String"),
 for (int i=0; i<values.Count(); ++i) {
     arrayClass.CallStatic("set", arrayObject, i,
                               new AndroidJavaObject("java.lang.String", values[i])));

Now as the last step lets set the field mPhoneNumders
string [] phoneNumbers = {"12345678", "87654321"};
addressObj.Set<AndroidJavaObject> ("mPhoneNumders", javaArrayFromCS(phoneNumbers));


  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

  2. Hello There,

    Your writing shines! There is no room for gibberish here clearly you have explained about
    Setting fields of an android java object from unity C#, including array Keep writing!

    I'd really like to know what type of arguments does this function take, and what happens in the for loop.
    I realize that first 2 are arrays, the next one is an unsigned number, what about the last two?
    It was cool to see your article pop up in my Google search for the process yesterday. Great Guide.

    Keep up the good work!


  3. How do I do the reverse of this array phonenumber? I want to send from java to c#

  4. It has three basic strides to make Android application. As a matter of first importance you need to pick an application at that point redo your substance (text) of your decision and even pictures. install league of legends