- Home /
WebGL Build usiny async/await in JSlib plugin to load tensorflowjs model -Build Error,
Hi folks,
im trying to use Javascripts await/async functionalty in a jslib plugin for a WebGL build to load a pretrained tensorflowjs model. Unfortunately, this does not seem to be working. Im gussing this is due the fact that im trying to call a asynchron function from a synchron one.
Communication like in the manuel https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html works just fine.
This is the code im using:
DLL-Import and function call in unity:
using UnityEngine;
using System.Runtime.InteropServices;
public class interopHandler : MonoBehaviour
{
[DllImport("__Internal")]
private static extern void LoadModel();
[DllImport("__Internal")]
private static extern void PrintSummary();
public void Start()
{
}
public void jsBufferToTestFunction()
{
LoadModel();
Debug.Log("Test");
}
public void printSummary()
{
PrintSummary();
}
}
JSLIB Plugin:
var MyPlugin = {
LoadModel: async function()
{
const model = await tf.loadLayersModel('https://domain.domain/domain/domain/model.json');
tfvis.show.modelSummary({ name: 'Model Summary' }, model);
test();
},
PrintSummary: function()
{
console.log(model.summary());
console.log("called printsummar");
}
};
mergeInto(LibraryManager.library, MyPlugin);
So, i guess my question is: Does anyone know how to use async/await in a Unity jslib plugin? Is it even possible?
Alternativly, how can I implement a asynchron like behaviour in the plugin. I need the to know when and if the model is loaded properly.
Thank you very much! I forget the error output from the build:
Exception: Failed building WebGL Player. UnityEditor.WebGL.ProgramUtils.StartProgramChecked (System.Diagnostics.ProcessStartInfo p) (at /Users/builduser/buildslave/unity/build/PlatformDependent/WebGL/Extensions/Unity.WebGL.extensions/ProgramUtils.cs:48) UnityEditor.WebGL.WebGlBuildPostprocessor.EmscriptenLink (UnityEditor.Modules.BuildPostProcessArgs args, System.Boolean wasmBuild, System.String sourceFiles, System.String sourceFilesHash) (at /Users/builduser/buildslave/unity/build/PlatformDependent/WebGL/Extensions/Unity.WebGL.extensions/BuildPostprocessor.cs:445) UnityEditor.WebGL.WebGlBuildPostprocessor.LinkBuild (UnityEditor.Modules.BuildPostProcessArgs args) (at /Users/builduser/buildslave/unity/build/PlatformDependent/WebGL/Extensions/Unity.WebGL.extensions/BuildPostprocessor.cs:495) UnityEditor.WebGL.WebGlBuildPostprocessor.PostProcess (UnityEditor.Modules.BuildPostProcessArgs args) (at /Users/builduser/buildslave/unity/build/PlatformDependent/WebGL/Extensions/Unity.WebGL.extensions/BuildPostprocessor.cs:930) UnityEditor.Modules.DefaultBuildPostprocessor.PostProcess (UnityEditor.Modules.BuildPostProcessArgs args, UnityEditor.BuildProperties& outProperties) (at :0) UnityEditor.PostprocessBuildPlayer.Postprocess (UnityEditor.BuildTargetGroup targetGroup, UnityEditor.BuildTarget target, System.String installPath, System.String companyName, System.String productName, System.Int32 width, System.Int32 height, UnityEditor.BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.Build.Reporting.BuildReport report) (at :0) UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
I have exactly the same issue. Did you find a way to have an async function in the jslib file? Thanks a lot
Your answer
![](https://koobas.hobune.stream/wayback/20220613002920im_/https://answers.unity.com/themes/thub/images/avi.jpg)