英文:
Unity AndroidJavaObject not working without showing errors
问题
我一直在尝试在Unity中调用一个Java方法。对我来说没有起作用,即使是文档中最简单的示例。
using System.Collections;
using UnityEngine;
public class ExampleClass : MonoBehaviour {
void Start () {
AndroidJavaObject jo = new AndroidJavaObject ("java.lang.String", "some string");
int hash = jo.Call<int> ("hashCode");
Debug.Log ("hash=" + hash);
}
}
Unity控制台打印出 hash=0
,这不是提供的字符串的哈希码。即使我更改并使用java.lang.StringInvalidClass
作为类名,Unity仍然会向控制台报告相同的结果,而不会通知错误。我甚至可以尝试调用toString
,toStringa
,toInvalid2
,它们总是返回空字符串而不显示错误。
这是一个全新的2D项目,只有上面显示的脚本附加到相机对象上。我正在使用Ubuntu,Unity 2019.4,项目平台是Android。
感谢您的帮助。
<details>
<summary>英文:</summary>
I have been trying to call a Java method in unity. Not working for me, even the simplest example from the docs
using System.Collections;
using UnityEngine;
public class ExampleClass : MonoBehaviour {
void Start () {
AndroidJavaObject jo = new AndroidJavaObject ("java.lang.String", "some string");
int hash = jo.Call<int> ("hashCode");
Debug.Log ("hash=" + hash);
}
}
Unity console prints `hash=0`, which is not the hash code for provided String. Even if I change and use `java.lang.StringInvalidClass` as class name, unity still reports same result to the console without notifying errors. I can even try to call toString, toStringa, toInvalid2 and they always return empty string without showing errors.
This is a brand new 2d project with only script displayed above, attached to camara object. I am using Ubuntu, Unity 2019.4 and project platform is Android.
Thanks for the assistance.
</details>
# 答案1
**得分**: 1
Answering myself after some time working with Unity.
在一段时间与Unity一起工作后,我自己回答一下。
All examples in the web and Unity documentation doesn't mention it, which is weird since it is something simple to mention and about confusions: code needs to run as an Android application. Editor or even Unity Remote does not work, **in order to use AndroidJavaObject and related classes, your code needs to run as an Android application installed in the phone**.
网络上的所有示例和Unity文档都没有提到这一点,这很奇怪,因为这是一个很简单的要点,关于混淆的问题:代码需要作为Android应用运行。编辑器甚至Unity Remote都不起作用,**为了使用AndroidJavaObject和相关的类,你的代码需要作为安装在手机上的Android应用运行**。
<details>
<summary>英文:</summary>
Answering myself after some time working with unity.
All examples in the web and unity documentation doesn't mention it, which is weird since it is something simple to mention and about confusions: code needs to run as an android application. Editor or even unity remote does not work, **in order to use AndroidJavaObject and related classes, your code needs to run as an android application installed in the phone**.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论