Babylon Native Android

Is it possible to communicate between BabylonView to Android and back.
Like a button click in Android UI needs to trigger something in BabylonNative

Pinging @cedric / @bghgary

Yes, this is possible. We unfortunately don’t have good documentation for this yet. At a high level, you need to use N-API to talk to JavaScript from native code. There is a bit of a learning curve.

@ryantrem Can you provide more details on BabylonView usage?

If you want to use Babylon Native directly in a “regular” Anrdoid app, and you want your own Android based UI that triggers things in the Babylon.js code, then yea, I think you need to implement your own “interop” code from Java to C++ to JavaScript using JNI and N-API. An example that is sort of like this is the InputManager in the Playground app that forwards pointer input to the JavaScript. This starts with BabylonView.onTouch (Java code), which calls into BabylonNative::Wrapper::setTouchInfo (C++ via JNI), which calls into InputBuffer::SetPointerDown (C++ using N-API), which is then used in experience.js (JavaScript).

Another option is (will be) to leverage the React Native integration we are working on. This drastically simplifies the type of scenario you are describing, as React Native already does a lot of heavy lifting for communication between things like platform native controls and the JavaScript code. Leveraging this integration will start with installing an NPM package into your existing React Native application, putting an EngineView into the JSX, using useEngine to manage the lifetime of Babylon engine instance, and then simply using the usual Babylon.js API and the usual React Native UI components. You can see an example of this here: BabylonReactNative/App.tsx at master · BabylonJS/BabylonReactNative · GitHub.


I hope there will be a easier way in the future, like java to javascript communication through evaluateJavascript and JavascriptInterface

I think it is possible to do script evaluation as well. @bghgary or @syntheticmagus will know for sure.

What do you mean when you say “JavascriptInterface?” Are you referring to the JavaScriptInterface (JSI) that is part of React Native, or something else?

Yes, you can certainly evaluate script either directly or with the ScriptLoader contract.

1 Like

By JavascriptInterface in Android, I was referring to this.