Unity的AndroidJavaObject在没有显示错误的情况下不起作用。

huangapple go评论96阅读模式
英文:

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仍然会向控制台报告相同的结果,而不会通知错误。我甚至可以尝试调用toStringtoStringatoInvalid2,它们总是返回空字符串而不显示错误。

这是一个全新的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 (&quot;java.lang.String&quot;, &quot;some string&quot;);
            int hash = jo.Call&lt;int&gt; (&quot;hashCode&quot;);
            Debug.Log (&quot;hash=&quot; + 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&#39;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>



huangapple
  • 本文由 发表于 2020年6月29日 13:06:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/62631567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定