Unity WebGL throws Error: "ReferenceError: Runtime is not defined"

Unity WebGL throws Error: "ReferenceError: Runtime is not defined"

我想为 WebGL 导出我的 Unity 项目(Unity 版本 2021.2),但出现此错误:

An error occurred running the Unity content on this page. See your browser JavaScript console for more info. The error was: ReferenceError: Runtime is not defined unityFramework/_WebSocketConnect/instance.ws.onopen@http://localhost:55444/Build/WEbGL.framework.js:3:67866

我正在使用这个 Websocket 包 (https://github.com/endel/NativeWebSocket),在 Unity 或 Windows Build 中一切正常。当我 运行 构建 WebGL 时,它确实与 websocket 连接,但随后出现错误。 错误消息说我的控制台中有更多信息,但 F12 上的控制台仅重复错误:

未捕获的引用错误:未定义运行时 在 WebSocket.instance.ws.onmessage (WEbGL.framework.js:3) instance.ws.onmessage@WEbGL.framework.js:3


为了给出一个最小的可重现示例,我刚刚使用 Unity 2021.2 创建了一个空的 3D Core 项目并导入了包 NativeWebSocket(我从 GitHub 下载了文件并手动安装了它:

Copy the sources from NativeWebSocket/Assets/WebSocket into your Assets directory

然后你必须在 https://github.com/endel/NativeWebSocket/pull/54 上由 kentakang 进行修复,否则构建将失败。

然后我用下面的代码(也来自 Github 页面)制作了一个新的 C# 脚本,并将它放在场景中的相机上。我为 WebGL 导出它并得到了提到的错误。

这发生在其中一种方法 websocket.OnOpen/OnError/OnClose/OnMessage 被调用时,所以你甚至不需要 运行ning websocket 因为那时 websocket.OnError 被调用并且 WebGL 构建抛出“运行时未定义”错误。或者,如果您还有 运行ning Websocket 服务器,它也包含在包中,您会在调用 websocket.OnOpen 时收到错误。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using NativeWebSocket;

public class Connection : MonoBehaviour
{
  WebSocket websocket;

  // Start is called before the first frame update
  async void Start()
  {
    websocket = new WebSocket("ws://localhost:2567");

    websocket.OnOpen += () =>
    {
      Debug.Log("Connection open!");
    };

    websocket.OnError += (e) =>
    {
      Debug.Log("Error! " + e);
    };

    websocket.OnClose += (e) =>
    {
      Debug.Log("Connection closed!");
    };

    websocket.OnMessage += (bytes) =>
    {
      Debug.Log("OnMessage!");
      Debug.Log(bytes);

      // getting the message as a string
      // var message = System.Text.Encoding.UTF8.GetString(bytes);
      // Debug.Log("OnMessage! " + message);
    };

    // Keep sending messages at every 0.3s
    InvokeRepeating("SendWebSocketMessage", 0.0f, 0.3f);

    // waiting for messages
    await websocket.Connect();
  }

  void Update()
  {
    #if !UNITY_WEBGL || UNITY_EDITOR
      websocket.DispatchMessageQueue();
    #endif
  }

  async void SendWebSocketMessage()
  {
    if (websocket.State == WebSocketState.Open)
    {
      // Sending bytes
      await websocket.Send(new byte[] { 10, 20, 30 });

      // Sending plain text
      await websocket.SendText("plain text message");
    }
  }

  private async void OnApplicationQuit()
  {
    await websocket.Close();
  }

}

有人知道如何解决这个错误吗?帮助将不胜感激:)

它接缝在 unity 2021.2 变量运行时不存在并且可以替换为模块['dynCall_*']。

在 webSocket.jslib 中更改模块 ['dynCall_*1'](*2, *3) 的所有 Runtime.dynCall('*1', *2, [*3, *4]) , *4)

示例 instance.ws.onopen WebSocket.jslib 中的函数:

change Runtime.dynCall('vi', webSocketState.onOpen, [ instanceId ]);
for
Module['dynCall_vi'](webSocketState.onOpen, instanceId);