Problem calling SendUnityMessage after Importing Unity project in Android Native Project

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

Problem calling SendUnityMessage after Importing Unity project in Android Native Project

问题

以下是翻译好的部分:

"But what does not work is calling UnitySendMessage. We want to send an audio file for the character to speak, but so far we just cant call that message, keeps getting an error that the method is not recognized.

// This line fails each time, says 'unrecognized method'
mUnityPlayer?.UnitySendMessage("modelName", "functiontoCall", "msg")

What could be the issue that it doesn't recognize it? Is there some new/different way to pass messages? Is it potentially version issues between unity & Android studio, or the NDK im using?"

英文:

So we have an android native project, mostly in Kotlin. Separately we also have a small scene in Unity that we wanted to import.

We've been able to export the Unity project as an Android project, build it, and then import the .aar file into our Native project. That works OK and we're even able to build it and see the Unity scene.

In the code we can hold a reference to Unity like so

protected var mUnityPlayer: UnityPlayer? = null
...
// later initialize it
mUnityPlayer = UnityPlayer(this, this)

// And call some methods
mUnityPlayer?.requestFocus()
mUnityPlayer?.windowFocusChanged(true)

But what does not work is calling UnitySendMessage. We want to send an audio file for the character to speak, but so far we just cant call that message, keeps getting an error that the method is not recognized.

// This line fails each time, says 'unrecognized method'
mUnityPlayer?.UnitySendMessage("modelName", "functiontoCall", "msg")

What could be the issue that it doesn't recognize it? Is there some new/different way to pass messages? Is it potentially version issues between unity & Android studio, or the NDK im using?

答案1

得分: 0

以下是代码部分的中文翻译:

使用方法是 `UnitySendMessage`。

示例可以在手册 [Plugins For Android](https://docs.unity3d.com/540/Documentation/Manual/PluginsForAndroid.html) 中找到。

使用以下代码进行调用:

```csharp
using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { 
    jc.CallStatic("UnitySendMessage", "GameManager", "NativeAlert", "Hello World!"); 
}

或者

UnityPlayer.UnitySendMessage("GameManager", "NativeAlert", "Hello World!");

<details>
<summary>英文:</summary>

The method is `UnitySendMessage`.  

Example can be found in the manual [Plugins For Android](https://docs.unity3d.com/540/Documentation/Manual/PluginsForAndroid.html).

    using (AndroidJavaClass jc = new AndroidJavaClass(&quot;com.unity3d.player.UnityPlayer&quot;)) { 
        jc.CallStatic(&quot;UnitySendMessage&quot;, &quot;GameManager&quot;, &quot;NativeAlert&quot;, &quot;Hello World!&quot;); 
    } 

or 

    UnityPlayer.UnitySendMessage(&quot;GameManager&quot;, &quot;NativeAlert&quot;, &quot;Hello World!&quot;);

</details>



# 答案2
**得分**: 0

这是一种组合情况:
首先,我需要更改导入包的方式,我遵循了这个帖子的答案:https://stackoverflow.com/questions/66598542/importing-an-existing-jar-or-aar-as-new-project-module
出于某种原因,以前我使用的方法似乎没有暴露我所需的所有本地方法。

其次,正如hijinxbassist指出的,它需要作为UnityPlayer的静态方法直接调用。

<details>
<summary>英文:</summary>

So it was a combination of things:
First I needed to change how I was importing my package, I followed the answer from this post https://stackoverflow.com/questions/66598542/importing-an-existing-jar-or-aar-as-new-project-module
For whatever reason, when the method i was previously using just did seem to expose all the native methods I needed. 

Second, as hijinxbassist pointed out, it needs to be called as the static method on UnityPlayer directly.  

</details>



huangapple
  • 本文由 发表于 2023年2月24日 01:31:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548315.html
匿名

发表评论

匿名网友

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

确定