How to call async/await JavaScript function in Unity/C# WebGL platform?
Asked Answered
W

1

6

I followed this doc to call JavaScript function from my C# script in Unity to make a WebGL game.

But there is a problem if the js code contains async/await, for example:

C# script:

    [DllImport("__Internal")]
    private static extern void Foo();

    [DllImport("__Internal")]
    private static extern void Boo();

    void Start(){
      Foo();
      Boo();
    }

JavaScript mylib.jslib

mergeInto(LibraryManager.library, {
  // works well
  Foo: function () {
    window.alert("Hello, world!");
  },

  // error: can't compile
  Boo: async function (){
    var s = function (ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    };
    await s(2000);
    window.alert("Boo!");
  }
});

When I tried to build these code, it showed error as below:

Failed process stderr log:
error: failure to execute js library "D:\CloudLinProject\Unity\My project\Assets\Plugins\Javascripts\PhantomAPI.jslib": SyntaxError: Unexpected token function,,SyntaxError: Unexpected token function
    at Object.load (eval at globalEval (D:\Program Files\Unity\2020.3.22f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\src\compiler.js:105:8), <anonymous>:179:14)
    at JSify (eval at globalEval (D:\Program Files\Unity\2020.3.22f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\src\compiler.js:105:8), <anonymous>:87:20)
    at D:\CloudLinProject\Unity\My project\Assets\Plugins\Javascripts\PhantomAPI.jslib (D:\Program Files\Unity\2020.3.22f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\src\compiler.js:221:3)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
preprocessed source (you can run a js engine on this to get a clearer error message sometimes):


Internal compiler error in src/compiler.js! Please raise a bug report at https://github.com/kripken/emscripten/issues/ with a log of the build and the input files used to run. Exception message: "SyntaxError: Unexpected token function" | SyntaxError: Unexpected token function
    at Object.load (eval at globalEval (D:\Program Files\Unity\2020.3.22f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\src\compiler.js:105:8), <anonymous>:179:14)
    at JSify (eval at globalEval (D:\Program Files\Unity\2020.3.22f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\src\compiler.js:105:8), <anonymous>:87:20)
    at Object.<anonymous> (D:\Program Files\Unity\2020.3.22f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\src\compiler.js:221:3)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
Traceback (most recent call last):
  File "D:\Program Files\Unity\2020.3.22f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emcc.py", line 3063, in <module>
    sys.exit(run())
  File "D:\Program Files\Unity\2020.3.22f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emcc.py", line 1780, in run
    final = shared.Building.emscripten(final, append_ext=False, extra_args=extra_args)
  File "D:\Program Files\Unity\2020.3.22f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\tools\shared.py", line 2274, in emscripten
    emscripten._main(cmdline)
  File "D:\Program Files\Unity\2020.3.22f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten.py", line 2233, in _main
    return temp_files.run_and_clean(lambda: main(
  File "D:\Program Files\Unity\2020.3.22f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\tools\tempfiles.py", line 93, in run_and_clean
    return func()
  File "D:\Program Files\Unity\2020.3.22f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten.py", line 2238, in <lambda>
    DEBUG=DEBUG,
  File "D:\Program Files\Unity\2020.3.22f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten.py", line 2164, in main
    temp_files=temp_files, DEBUG=DEBUG)
  File "D:\Program Files\Unity\2020.3.22f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten.py", line 86, in emscript
    glue, forwarded_data = compiler_glue(metadata, libraries, compiler_engine, temp_files, DEBUG)
  File "D:\Program Files\Unity\2020.3.22f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten.py", line 218, in compiler_glue
    glue, forwarded_data = compile_settings(compiler_engine, libraries, temp_files)
  File "D:\Program Files\Unity\2020.3.22f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten.py", line 541, in compile_settings
    cwd=path_from_root('src'), error_limit=300)
  File "D:\Program Files\Unity\2020.3.22f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\tools\jsrun.py", line 132, in run_js
    raise Exception('Expected the command ' + str(command) + ' to finish with return code ' + str(assert_returncode) + ', but it returned with code ' + str(proc.returncode) + ' instead! Output: ' + str(ret)[:error_limit])
Exception: Expected the command ['D:/Program Files/Unity/2020.3.22f1/Editor/Data\\Tools\\nodejs\\node.exe', '--stack_size=8192', '--max-old-space-size=4096', 'D:\\Program Files\\Unity\\2020.3.22f1\\Editor\\Data\\PlaybackEngines\\WebGLSupport\\BuildTools\\Emscripten\\src\\compiler.js', 'C:\\Users\\eucyl\\AppData\\Local\\Temp\\tmpoqhbko.txt', 'D:\\Program Files\\Unity\\2020.3.22f1\\Editor\\Data\\PlaybackEngines\\WebGLSupport\\BuildTools\\lib\\Audio.js', 'D:\\Program Files\\Unity\\2020.3.22f1\\Editor\\Data\\PlaybackEngines\\WebGLSupport\\BuildTools\\lib\\case_1174367_workaround.js', 'D:\\Program Files\\Unity\\2020.3.22f1\\Editor\\Data\\PlaybackEngines\\WebGLSupport\\BuildTools\\lib\\case_1179945_workaround.js', 'D:\\Program Files\\Unity\\2020.3.22f1\\Editor\\Data\\PlaybackEngines\\WebGLSupport\\BuildTools\\lib\\case_1187965_workaround.js', 'D:\\Program Files\\Unity\\2020.3.22f1\\Editor\\Data\\PlaybackEngines\\WebGLSupport\\BuildTools\\lib\\case_1208971_workaround.js', 'D:\\Program Files\\Unity\\2020.3.22f1\\Editor\\Data\\PlaybackEngines\\WebGLSupport\\BuildTools\\lib\\Cursor.js', 'D:\\Program Files\\Unity\\2020.3.22f1\\Editor\\Data\\PlaybackEngines\\WebGLSupport\\BuildTools\\lib\\Eval.js', 'D:\\Program Files\\Unity\\2020.3.22f1\\Editor\\Data\\PlaybackEngines\\WebGLSupport\\BuildTools\\lib\\FileSystem.js', 'D:\\Program Files\\Unity\\2020.3.22f1\\Editor\\Data\\PlaybackEngines\\WebGLSupport\\BuildTools\\lib\\Logging.js', 'D:\\Program Files\\Unity\\2020.3.22f1\\Editor\\Data\\PlaybackEngines\\WebGLSupport\\BuildTools\\lib\\Profiler.js', 'D:\\Program Files\\Unity\\2020.3.22f1\\Editor\\Data\\PlaybackEngines\\WebGLSupport\\BuildTools\\lib\\SystemInfo.js', 'D:\\Program Files\\Unity\\2020.3.22f1\\Editor\\Data\\PlaybackEngines\\WebGLSupport\\BuildTools\\lib\\UnetWebSocket.js', 'D:\\Program Files\\Unity\\2020.3.22f1\\Editor\\Data\\PlaybackEngines\\WebGLSupport\\BuildTools\\lib\\Video.js', 'D:\\Program Files\\Unity\\2020.3.22f1\\Editor\\Data\\PlaybackEngines\\WebGLSupport\\BuildTools\\lib\\WebCam.js', 'D:\\Program Files\\Unity\\2020.3.22f1\\Editor\\Data\\PlaybackEngines\\WebGLSupport\\BuildTools\\lib\\WebRequest.js', 'D:\\CloudLinProject\\Unity\\My project\\Assets\\Plugins\\Javascripts\\PhantomAPI.jslib', 'D:\\Program Files\\Unity\\2020.3.22f1\\Editor\\Data\\PlaybackEngines\\WebGLSupport\\BuildTools\\Emscripten\\src\\library_pthread_stub.js'] to finish with return code 0, but it returned with code 1 instead! Output: // The Module object: Our interface to the outside world. We import
// and export values on it. There are various ways Module can be used:
// 1. Not defined. We create it here
// 2. A function parameter, function(Module) { ..generated code.. }
// 3. pre-run appended it, var Module = {}; ..generated
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

So how should I handle js async/await functions in C#?

Wells answered 11/11, 2021 at 6:21 Comment(4)
Are you using a very old version of Unity? As far as I know Unity doesn't support JS anymore, which means it also probably doesn't support ES6+ syntaxes ...Debouch
@Debouch No, I'm using Unity 2020 LTS. The deprecated JS support you mentioned is about using JS as game script, but I'm using JS for WebGL game which use C# as game script and try to call JS script that is in web page.Wells
Ahhh okay, so kind of like a webservice call but for client side codeDebouch
@Debouch Unity never supported JavaScript at all ;) What you refer to is UnityScript and only used a syntax that was JavaScript like-ish but similar to the c# layer was basically just an interface to the underlying C++ engine. OP is asking about WebGL where you basically host your app embedded in an HTML page and can then indeed interact with the JavaScript of that page ;)Theressathereto
T
5

tl;dr: This is how. c# doesn't need to be aware of the async and it should work.


I just made a little test using

Assets/Plugins/mylib.jslib

mergeInto(LibraryManager.library, {

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

    Boo: async function () {
        var s = function (ms) {
            return new Promise(resolve => setTimeout(resolve, ms));
        };
        await s(2000);
        window.alert("Boo!");
    }
});

and

Assets/Exmaple.cs

public class Example : MonoBehaviour
{
    [DllImport("__Internal")]
    private static extern void Foo();

    [DllImport("__Internal")]
    private static extern void Boo();

    void Awake()
    {
        Foo();
        Boo();
    }
}

And this is how it looks like without any issue

enter image description here

So without further information I would claim: You issue is not the async but something else.

Theressathereto answered 11/11, 2021 at 8:57 Comment(3)
I just tried the same code as yours but still getting error. I guess it might cause by the Unity version diff. (I'm using Unity 2020 and you're in 2021). I am going to install Unity 2021 and try again later.Wells
@Wells oh yeah .. maybe they actually didn't support it before can check that tomorrow againTheressathereto
I've tried in Unity 2021 with exactly the same code and it worked like a charm! I looked over the the Unity 2021.2 update notes and it mentioned "Updated WebGL compiler to Emscripten 2.0.19" so it might be the key. In this github issue also has mentioned about async support. Overall, thanks for your answer otherwise I can't figure out it's Unity version problem by myself :DWells

© 2022 - 2024 — McMap. All rights reserved.