Calling JavaScript functions in C# [KNI engine WebGL]

Hello, I’m making a cross platform web game. How do I call JS script functions inside game C# scripts? Will it work using jslib like in unity?

Hello @droken,

The BlazorGL platform is wrapped in a Blazor webassembly project.
See Blazor JS interop for more details.

However, reaching outside the Game class for native functionality is not something I can guarantee won’t break in future releases. The same is true for all other platform targets (WinForms, SDL, UAP, etc). The framework use those technologies to host a Game class but does not support interaction with say, WinForm controls, etc.

Ok, i think i got it working. I pass JsRuntime from index.razor.js to my game class and then use it to call functions. Is this approach ok or is there a better way to do it?
Also, how can I get user device platform (desktop, android, etc)?

Yes I think JsRuntime will be fine.
The BlazorGL platform uses InvokeUnmarshalled for the most part, but this is on the way out.
.net7 introduced a new way for JS interop which I haven’t see yet.

The XNA API doesn’t provide info on what the OS is. For whatever it’s worth, the platform is ‘Web’.
I never had to do that, and the Browser might not give reliable info.
You could try JsRuntime.Invoke(“getUserAgent”); and parse the user agent but that was messy since the early days of the browser wars.

1 Like

I made os check using js script that is checking userAgent, its working fine so far. Thank you for your answers and the kni engine!

1 Like