关于unity发布WebGL的一点小坑

1、添加自定义webgl templates遇到的问题:

按照unity官方示例,https://docs.unity3d.com/cn/current/Manual/webgl-templates.html

移植Default到项目中,需要设置一个全局变量用于跟unity通信。

使用vscode打开index.html。

关于unity发布WebGL的一点小坑_第1张图片

 打开后报错。但是不影响直接打包发布。

然后按照官方示例,添加myGameInstance然后使用vscode格式化然后保存。

保存后发现原本的{{{  变成{ { {。多了一些空格。当时也没有注意。

关于unity发布WebGL的一点小坑_第2张图片

 打包成功

但是启动报错

 看源码后发现productName和productVersion是没有改变的。所以初始化失败了

关于unity发布WebGL的一点小坑_第3张图片

 vscode格式化问题。后面使用nodepad++就可以了

2、参照官方示例

Interaction with browser scripting - Unity 手册

添加unity和js的调用代码如下:

mergeInto(LibraryManager.library, {

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

  HelloString: function (str) {
    console.log("unity 调用了js接口,执行HelloString", 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]);
  },

});

打包后报错。

Building Library\Bee\artifacts\WebGL\build\debug_WebGL_wasm\build.js failed with output:
Traceback (most recent call last):
  File "E:\Unity\Hub\Editor\2021.3.22f1c1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten\emcc2.py", line 3571, in 
    sys.exit(main(sys.argv))
  File "E:\Unity\Hub\Editor\2021.3.22f1c1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten\emcc2.py", line 3564, in main
    ret = run(args)
  File "E:\Unity\Hub\Editor\2021.3.22f1c1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten\emcc2.py", line 1082, in run
    phase_post_link(options, wasm_target, wasm_target, target)
  File "D:\obj\windows-release\37amd64_Release\msi_python\zip_amd64\contextlib.py", line 74, in inner
  File "E:\Unity\Hub\Editor\2021.3.22f1c1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten\emcc2.py", line 2400, in phase_post_link
    phase_emscript(options, in_wasm, wasm_target, memfile)
  File "D:\obj\windows-release\37amd64_Release\msi_python\zip_amd64\contextlib.py", line 74, in inner
  File "E:\Unity\Hub\Editor\2021.3.22f1c1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten\emcc2.py", line 2428, in phase_emscript
    emscripten.run(in_wasm, wasm_target, final_js, memfile)
  File "E:\Unity\Hub\Editor\2021.3.22f1c1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten\emscripten.py", line 830, in run
    emscript(in_wasm, out_wasm, outfile_js, memfile, shared.DEBUG)
  File "E:\Unity\Hub\Editor\2021.3.22f1c1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten\emscripten.py", line 308, in emscript
    glue, forwarded_data = compile_settings()
  File "E:\Unity\Hub\Editor\2021.3.22f1c1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten\emscripten.py", line 178, in compile_settings
    cwd=path_from_root('src'), env=env)
  File "E:\Unity\Hub\Editor\2021.3.22f1c1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten\tools\shared.py", line 216, in run_js_tool
    return check_call(command, *args, **kw).stdout
  File "E:\Unity\Hub\Editor\2021.3.22f1c1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten\tools\shared.py", line 202, in check_call
    return run_process(cmd, *args, **kw)
  File "E:\Unity\Hub\Editor\2021.3.22f1c1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten\tools\shared.py", line 97, in run_process
    ret = subprocess.run(cmd, check=check, input=input, *args, **kw)
  File "D:\obj\windows-release\37amd64_Release\msi_python\zip_amd64\subprocess.py", line 474, in run
  File "D:\obj\windows-release\37amd64_Release\msi_python\zip_amd64\subprocess.py", line 926, in communicate
UnicodeDecodeError: 'gbk' codec can't decode byte 0x81 in position 138147: illegal multibyte sequence
UnityEditor.GenericMenu:CatchMenu (object,string[],int)

发现gbk报错,开始以为是UTF-8的保存模式问题。后面改了很多次,发现是中文的原因!。包括中文标点。都不可以。替换中文后成功打包。

你可能感兴趣的:(unity,webgl,游戏引擎)