androidandroid-studiounity-game-engine3dandroid-native-library

Problem calling SendUnityMessage after Importing Unity project in Android Native Project


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?


Solution

  • 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 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.