本文介绍了如何使用 Unity3D 从 AndroidJavaObject 获取变量到 C# 类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到如何从 C# 中的 AndroidJavaObject 获取 ListArray 变量.
我正在尝试使用 CountListArray 中存储为 AndroidJavaObject 的项目数创建一个 for 函数.但我需要知道如何从 AndroidJavaObject 获取 Count 以及如何正确使用它.
这是我用来获取变量的方法,还要注意包"不是 AndroidJavaObject[].

I can't find how to get ListArray variables from an AndroidJavaObject in C#.
I'm trying to make a for function using a Count for the number of items in the ListArray that is stored as an AndroidJavaObject. But I need to know how to get the Count from the AndroidJavaObject and how to use it properly.
This is what I'm using to get the variables, also notice that "packages" is not an AndroidJavaObject[].

AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
int flag = new AndroidJavaClass("android.content.pm.PackageManager").GetStatic<int>("GET_META_DATA");
AndroidJavaObject pm = currentActivity.Call<AndroidJavaObject>("getPackageManager");
AndroidJavaObject packages = pm.Call<AndroidJavaObject>("getInstalledApplications", flag);

在这一点上它是非常基本的,但它的工作要归功于这个如何使用 Unity3D 获取已安装的应用程序列表?

it's very rudimentary at this point, but it works thanks to some help from this How to get installed apps list with Unity3D?

到目前为止的进展在获取图标方面停滞不前,其他一切都很完美,我需要一种方法来获取以某种方式链接到图标的string,或者Texture2D 图标.
另一种选择是使用包含 drawableAndroidJavaObject,就好像它是 Texture2D.不过,我不知道如何完成其​​中任何一项.
我的另一个想法是将其转换为另一个变量,例如可以传输和转换回来的 byte[],但我有尚未找到在这种情况下有效的方法.

The progress thus far stalls at getting the icon, everything else works perfect, I need a way to get either a string that links to the icon somehow, or the Texture2D of the icon.
Another alternative would be a to use a AndroidJavaObject that contains a drawable as if it's a Texture2D. I have no idea how to accomplish any of this though.
Another Idea I had was to convert it to another variable, like byte[] that can be transferred and converted back, but I have yet to find a method of that which works under the circumstance.

int count = packages.Call<int>("size");
AndroidJavaObject[] links = new AndroidJavaObject[count];
string[] names = new string[count];
Texture2D[] icons = new Texture2D[count];
int ii =0;
for(int i=0; ii<count;){
    //get the object
    AndroidJavaObject currentObject = packages.Call<AndroidJavaObject>("get", ii );
    try{
        //try to add the variables to the next entry
        links[i] = pm.Call<AndroidJavaObject>("getLaunchIntentForPackage", currentObject.Get<AndroidJavaObject>("processName"));
        names[i] = pm.Call<string>("getApplicationLabel", currentObject);
        icons[i] = pm.Call<Texture2D>("getApplicationIcon", currentObject);
        Debug.Log ("(" + ii + ") " + i +" "+ names[i]);
        //go to the next app and entry
        i++;
        ii++;
    }
    catch
    {
        //if it fails, just go to the next app and try to add to that same entry.
        Debug.Log("skipped "+ ii);
        ii++;
    }

}

我真的很讨厌在这里连续问两个问题,我很抱歉不得不这样做,但这是一个困难且看似尴尬的情况,几乎没有文档(我可以找到).

I really hate to ask two questions in a row here, and I apologize for having to do so, but this is a difficult and seemingly awkward circumstance, that has little to no documentation (that I can find).

推荐答案

首先,关于 Unity3D 和 Android 之间互操作的文档充其量是稀缺的.

First of all the docs on the interop between Unity3D and Android are scarce at best.

最重要的是,Unity 公开的唯一用于处理 java 对象的接口是 AndroidJavaObject,并且该接口只有几个方法 定义.你只能使用那些,而且只能使用那些.

The most important thing is that the only interface Unity exposes to work with java object is AndroidJavaObject and that interface has only a couple of methods defined. You can only use those ones and only those.

这意味着您在使用 java 数组时不会获得 Count 对象,并且您仍将使用 AndroidJavaObject.

This means that you don't get a Count object when working with an java array and you'll still be working with AndroidJavaObject.

int count = packages.Call<int>("size"); //getting the size of the array

for( int i = 0; i < count; i++ )
{
 //getting the object at location i
 AndroidJavaObject currentObject = packages.Call("get", i );

 //use currentObject using the same methods as before: Call, CallStatic, Get, GetStatic
}

我知道这很冗长,编写这样的代码很难测试,您需要制作和 apk 并将其部署到设备以检查它是否运行.

I know this is verbose, and writing code like this is hard to test, you need to make and apk and deploy it to a device to check that it runs.

可能更快的方法是创建自己的 java 类,该类在 java 端执行所有这些操作,并公开一个从 Unity 端调用的方法.通过这种方式,您至少可以在制作要添加到 Plugins/Android 文件夹中的 jar 时获得静态类型的好处.

Probably a faster way of doing this is to make your own java class that does all this on the java side and expose one method that gets called from the Unity side. This way at least you get the benefit of static typing when making a jar that you'll add in the Plugins/Android folder.

这篇关于如何使用 Unity3D 从 AndroidJavaObject 获取变量到 C# 类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 12:37