Passing data from native Android app to PlayCanvas

I have a playcanvas project which is running inside web view of native android application.
Now I am trying to pass some data from android to playcanvas at runtime using the following approach.

//android studio code
webView.evaluateJavascript("parfun(" + "{id: 123}" + ")", null /* callback */);

//parafun is defined in playcanvas
IframeCommunication.prototype.parfun = function(eventData)
{
    console.log(eventData.id);
};

I am getting parfun is not defined as the error.
Can some explain what is going wrong here or if there is a better approach of passing the data between playcanvas and android?

thanks.

Hi @Arjun_Sankhala and welcome,

I think given you are getting that error communicating from the Android parent app to the webview was successful.

The error is expected, even if you were running in the browser. You don’t have any global parfun() method to call. That method is a member method of the IframeCommunication script type.

Normally you will have to get a reference to an entity that has that script attached and call that method like this:

const entity = this.app.root.findByName('Entity Name');
entity.script.iframeCommunication.parfun(1);

Otherwise, declare your method as a global window method in your PlayCanvas script and access it as you attempted to do:

// note this is a global method
function parfun(eventData){

};
1 Like

Hi @Leonidas Thanks for reply.
How can i call a playcanvas function from the global function?

You will need to get a reference to the PlayCanvas application, and from there find an entity that references the script you are looking for and call the method:

const app = pc.Application.getApplication();
const entity = app.root.findByName('Entity Name');
entity.script.iframeCommunication.exampleMethod();