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

问题描述 投票:0回答:1

我使用此代码将我的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”和“connection”的Debug.Logs之间发生错误。换句话说,它在连接失败.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 find中:

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包含在我的Visual Studio引用中,以及所有I18N文件中。我还将这些文件放在我构建的项目文件路径中。

如果您需要其他信息,请告诉我,我试图包括我认为相关的所有内容。

c# mysql unity3d unity-webgl
1个回答
0
投票

由于WebGL构建归结为javascript,因此您无法在这些构建中使用标准C#网络API(MySQL连接器使用ADO.NET,因此,不幸的是,它不符合WebGL构建版本)。关于Networking with Unity WebGL Builds有一些很好的信息,但这里有与你的情况相关的部分:

由于安全隐患,JavaScript代码无法直接访问IP套接字以实现网络连接。因此,.NET网络类(即System.Net命名空间中的所有内容,特别是System.Net.Sockets)在WebGL中不起作用。这同样适用于Unity的旧UnityEngine.Network *类,这些类在构建WebGL时不可用。

如果您需要在WebGL中使用Networking,您当前可以选择使用Unity中的WWW或UnityWebRequest类或支持WebGL的新Unity Unity功能,或者使用JavaScript中的WebSockets或WebRTC实现您自己的网络。

请记住它提到的“新的Unity网络功能”是UNet,which is being deprecated的一部分。编辑:看起来整个NetworkTransport API消失了。


希望您在挖掘这些文章的同时找到解决方案。我没有尝试使用UNet,WebSockets或WebRTC,但个人而言,为了在我自己的WebGL项目中解决这个问题,我使用UnityWebRequestPOST请求发送到我正在运行LAMP堆栈的AWS上托管的服务器。我的php页面处理请求,MySQL数据库坐在同一台服务器上。

© www.soinside.com 2019 - 2024. All rights reserved.