DEV Community

Discussion on: Game Programming Fundamentals

Collapse
 
fkkarakurt profile image
Fatih Küçükkarakurt

What I mean here is being able to interact with the WebGL Browser script. When creating content for the web, you may need to communicate with other elements on your web page. Or you may want to implement functionality using Web APIs that Unity does not currently expose by default. Either way, you need to interface directly with the browser's JavaScript engine.

For this we can call JavaScript functions from Unity scripts.

mergeInto(LibraryManager.library, {

  Hello: function() {
    window.alert("Hello, world!");
  },

  HelloString: function (str) {
    window.alert(UTF8ToString(str));
  },

  PrintFloatArray: function (array, size) {
    for(var i = 0; i < size; i++)
    console.log(HEAPF32[(array >> 2) + i]);
  },

  AddNumbers: function (x, y) {
    return x + y;
  },

  StringReturnValueFunction: function () {
    var returnStr = "bla";
    var bufferSize = lengthBytesUTF8(returnStr) + 1;
    var buffer = _malloc(bufferSize);
    stringToUTF8(returnStr, buffer, bufferSize);
    return buffer;
  },

  BindWebGLTexture: function (texture) {
    GLctx.bindTexture(GLctx.TEXTURE_2D, GL.textures[texture]);
  },

});
Enter fullscreen mode Exit fullscreen mode

The recommended way to use browser JavaScript in your project is to add your JavaScript resources to your project and then call these functions directly from your script code. To do this, place JavaScript encoded files using the .jslib extension under a "Plugins" subfolder in your Assets folder. The plugin file should look like the one above.

Then you can call them with your C# script.

In conclusion, you are right. There is no longer active support for JavaScript. I agree with you. But if we look at the WebGL side, we can use Unity and JavaScript together. I'll edit the sentence in the article anyway.

Thank you.