AndroidJavaObject.Call array passing error (Unity for Android)

前端 未结 2 636
时光取名叫无心
时光取名叫无心 2021-01-20 05:17

I\'m working on Unity for Android application, which uses native Android plugin. Inside of it I use AndroidJavaObject\'s Call method.

As it

相关标签:
2条回答
  • 2021-01-20 05:41

    Arrays require special treatment when being sent to an AndroidJavaObject. Taken from this article, you can implement a function that will handle it like so:

    private AndroidJavaObject javaArrayFromCS(string [] values) {
        AndroidJavaClass arrayClass  = new AndroidJavaClass("java.lang.reflect.Array");
        AndroidJavaObject arrayObject = arrayClass.CallStatic<AndroidJavaObject>("newInstance", new AndroidJavaClass("java.lang.String"), values.Count());
        for (int i=0; i<values.Count(); ++i) {
            arrayClass.CallStatic("set", arrayObject, i, new AndroidJavaObject("java.lang.String", values[i])));
        }
    
        return arrayObject;
    }
    

    You can then call into your function like this:

    myAndroidJavaObject.Call("myMethod", javaArrayFromCS(strings));
    
    0 讨论(0)
  • 2021-01-20 05:45

    All you need to do is just cast the string array to an object, like the following code;

    string[] strings = new string[] { "string1", "string2", ...};
    
    myAndroidJavaObject.Call("myMethod", **(object)** strings);
    
    0 讨论(0)
提交回复
热议问题