如何从AndroidJavaObject变量放入使用Unity3D C#类 [英] How to get variables from AndroidJavaObject into a C# class using Unity3D

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

问题描述

我找不到从用C AndroidJavaObject #如何让 ListArray 变量。

我试图使用计数的项目在为功能> ListArray 存储为 AndroidJavaObject 。但我需要知道如何让计数 AndroidJavaObject 以及如何正确使用它。

这是我用什么来获取变量,也注意到套餐不是 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获取安装的应用程序列表?

这样的进度远远是在获取图标摊位,其他的一切都运行完美,我需要一种方式来获得无论是字符串的链接图标不知何故,或的Texture2D 图标。
另一种选择将是一个使用 AndroidJavaObject 包含绘制,就好像它是一个Texture2D。我不知道如何做到任何这虽然。
另一个想法我是把它转换为另一个变量,如字节[] 可传送和改回,但我还没有找到一个方法,在这情况下工作。

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.

最重要的是,唯一的接口统一公开与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数组工作,你仍然可以用时 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
}

我知道这是冗长和写作code这样是很难考,你需要做出修改并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侧调用。这样,至少使一个罐子,你会在插件/ Android版添加文件夹,当你获得静态类型的好处。

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆