无法将 Unity3d WebGL 项目构建连接到数据库,在编辑器中工作正常

Having trouble connecting Unity3d WebGL project build to database, works fine in editor

我使用此代码将我的 Unity 项目连接到我的 MySql 数据库:

public void SetupSQLConnection()
{
    Debug.Log("Connection Function Started");
    if (connection == null)
    {
        Debug.Log("If connection == null");
        try
        {
            Debug.Log("Try block started");
            MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder();

            connBuilder.Server = "localhost";
            connBuilder.UserID = "root";
            connBuilder.Database = "therapygame";
            connBuilder.Password = "";
            connBuilder.OldGuids = true;

            Debug.Log("String set");
            connection = new MySqlConnection(connBuilder.ConnectionString);
            Debug.Log("new MySqlConnection");
            connection.Open();
            Debug.Log("connection");
        }
        catch (MySqlException ex)
        {
            Debug.LogError("MySQL Error: " + ex.ToString());
        }
    }
}

当 运行 我的项目在 Unity3d 编辑器中时,这工作正常。但是,当我构建 WebGL 时,它不再连接到数据库。

错误发生在"new MySqlConnection"的Debug.Logs和"connection"之间。换句话说,它在 connection.Open(); 处失败了。声明。

这里是控制台登录 Google Chrome:

SystemException: Thread creation failed.

Rethrow as TypeInitializationException: The type initializer for 'System.Threading.Timer.Scheduler' threw an exception.

Rethrow as TypeInitializationException: The type initializer for 'System.Threading.Timer' threw an exception.

Rethrow as TypeInitializationException: The type initializer for 'MySql.Data.MySqlClient.MySqlPoolManager' threw an exception.


(Filename: currently not available on il2cpp Line: -1)

这是该错误的来源(错误太大,我不得不将其分成几个部分)(请参阅下一节以了解要查找的内容):

https://0bin.net/paste/-28c3BvLx+Nx+Q+L#ltyOWWb+IembwSAgNKzPCRwvI5MkPOrUfgOAs3i4R+f

https://0bin.net/paste/ZleyLDeOcgr3kj-e#OPVr3oIC8-mcO9mXB1pV/+ONcdnUB+3I1RCy37/NI+p

https://0bin.net/paste/+rbHWsKmXhIXM50A#PaAFLCeDJZzIQTWczYbDepmubFhSaeDWqzcB+ItUTnb

https://0bin.net/paste/VdNv2NA8K0--ZXNM#fomzOVZ9iNOJDHequiSAbuv6I3lxDXXL+E5WK6GPKZv

https://0bin.net/paste/zcDPoycv5lqbjRcA#h0WNQdMSU4VtdIbwzOByW5csu68FERPvEdOKJz9oUeA

复制并粘贴到 ctrl+f 查找:

function _JS_Log_Dump(ptr, type) {
var str = Pointer_stringify(ptr);
if (typeof dump == "function") dump(str);
switch (type) {
case 0:
case 1:
case 4:
console.error(str);
return;
case 2:
console.warn(str);
return;
case 3:
case 5:
console.log(str);
return;
default:
console.error("Unknown console message type!");
console.error(str);
}
}

有问题的行是

case 4:
console.error(str);

我已将 System.Data.dll 和 MySql.Data.dll 以及所有 I18N 文件包括在我的 Visual Studio 参考文献中。我也将这些文件放在我构建的项目文件路径中。

如果您需要更多信息,请告诉我,我尽量包含所有我认为相关的信息。

由于 WebGL 构建归结为 javascript,您不能在这些构建中使用标准 C# 网络 API(MySQL 连接器使用 ADO.NET,因此不幸的是,WebGL 构建被取消资格). Networking with Unity WebGL Builds 上有一些很好的信息,但这里是与您的情况相关的部分:

Due to security implications, JavaScript code does not have direct access to IP Sockets to implement network connectivity. As a result, the .NET networking classes (ie, everything in the System.Net namespace, particularly System.Net.Sockets) are non-functional in WebGL . The same applies to Unity’s old UnityEngine.Network* classes, which are not available when building for WebGL.

If you need to use Networking in WebGL, you currently have the options to use the WWW or UnityWebRequest classes in Unity or the new Unity Networking features which support WebGL, or to implement your own networking using WebSockets or WebRTC in JavaScript.

请记住它提到的 "new Unity Networking features" 是 UNet 的一部分,which is being deprecated. EDIT: It looks like the entire NetworkTransport API 已经消失了。


希望您在浏览这些文章时能找到解决方案。我没有尝试使用 UNet、WebSockets 或 WebRTC,但就我个人而言,为了在我自己的 WebGL 项目中解决这个问题,我使用 UnityWebRequest 向我在 AWS 上托管的服务器发送 POST 请求那是 运行 一个 LAMP 堆栈。我的 php 页面处理请求并且 MySQL 数据库位于同一台服务器上。