Unity WebGL and Native Plugins can't build

I am trying to build a native plugin from C/C++ code with external library references for Unity WebGL.

According to these docs: Unity - Manual: Web native plug-ins for Emscripten and Unity - Manual: Compile a static library as a Unity plug-in

What I am doing is the following:

  • Compile my code using emcc and the following flags: -s USE_BOOST_HEADERS=1 -s ENVIRONMENT=web,worker -pthread -o filename.a

  • Put the output file (“.a” or “.bc”) in the Assets/Plugins/WebGL folder.

  • Reference the methods in a c# script

   #if UNITY_EDITOR || UNITY_STANDALONE
   [DllImport("my.dll")]
   #elif UNITY_WEBGL && !UNITY_EDITOR
   [DllImport("__Internal")]
   #endif
   public static extern int sumNumbers(int a, int b);
  • Build In Unity

  • I tried building using the .a file, but I face the following error: emcc: error: Assets/Plugins/WebGL/filename.a: Unknown format, not a static library!

  • I tried building using the .bc file,but I face the following error: wasm-ld: error: unknown file type: Assets/Plugins/WebGL/filename.bc

  • in both cases I also tried also unchecking “WebGL” from the inspector of the plugin file (.a, .bc) I will end with lots of errors like the following: undefined symbol: sumNumbers

  • I tried what they suggested here Unity - Manual: Web native plug-ins for Emscripten but when I try to run the command suggested emar I face the following error: llvm-ar.exe: error: unable to load ‘filename.a’: file too small to be an archive

I am really having bad time in understanding what should be the correct workflow, documentation is fragmented and apparently doesn’t contain all the info needed ( or I am missing some parts of it, despite days of research).

I am using Unity 2023.2.11f1. according to Unity - Manual: WebGL native plug-ins for Emscripten, I should use the Emscripten version ‘Emscripten 3.1.38-unity’, but if I look (as they suggest) in the following path C:\Program Files\Unity\Hub\Editor<Editor version>\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten\emscripten-version.txt I see the version is 3.1.39-git, so I tried with emscripten version 3.1.39 considering it is the version reported by unity editor installation.

I am just getting confused by reading documentation. I would just expect to be able to build for WebGL with my native plug ins, or see more explanatory errors if possible.

Did you already faced something similar and solved? what was eventually your pipeline? I would like to have a better documentation about this topic do you have any tips?

1 Like