skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. The method is UnitySendMessage.

    Example can be found in the manual Plugins For Android.

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

    or

    UnityPlayer.UnitySendMessage("GameManager", "NativeAlert", "Hello World!");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search