简体   繁体   English

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

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

I used this code to connect my Unity project to my MySql database: 我使用此代码将我的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());
        }
    }
}

This works fine when running my project in the Unity3d editor. 这在Unity3d编辑器中运行我的项目时工作正常。 However, when I build the WebGL, it no longer connects to the database. 但是,当我构建WebGL时,它不再连接到数据库。

The error happens between the Debug.Logs of "new MySqlConnection" and "connection". 在“new MySqlConnection”和“connection”的Debug.Logs之间发生错误。 In other words, it is failing at the connection.Open(); 换句话说,它在连接失败.Open(); statement. 声明。

Here is the console log in Google Chrome: 以下是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)

And here is the source of that error (it is so large that I had to break it apart into sections)(see following section to know what to look for): 以下是该错误的来源(它太大了,我不得不将它分成几个部分)(请参阅以下部分以了解要查找的内容):

https://0bin.net/paste/-28c3BvLx+Nx+Q+L#ltyOWWb+IembwSAgNKzPCRwvI5MkPOrUfgOAs3i4R+f 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/ZleyLDeOcgr3kj-e#OPVr3oIC8-mcO9mXB1pV/+ONcdnUB+3I1RCy37/NI+p

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

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

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

Copy this and paste it into ctrl+f find: 复制此内容并将其粘贴到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);
}
}

the lines that are in question are 有问题的线是

case 4:
console.error(str);

I have included System.Data.dll and MySql.Data.dll to my Visual Studio references, and also all the I18N files. 我已将System.Data.dll和MySql.Data.dll包含在我的Visual Studio引用中,以及所有I18N文件中。 I have also put those files in my built project file path. 我还将这些文件放在我构建的项目文件路径中。

Please tell me if you need additional information, I tried to include everything I thought was relevant. 如果您需要其他信息,请告诉我,我试图包括我认为相关的所有内容。

Since WebGL builds boil down to javascript, you cannot use standard C# networking APIs in those builds (MySQL connector uses ADO.NET and is therefore, unfortunately, disqualified from WebGL builds). 由于WebGL构建归结为javascript,因此您无法在这些构建中使用标准C#网络API(MySQL连接器使用ADO.NET,因此,不幸的是,它不符合WebGL构建版本)。 There is some good information on Networking with Unity WebGL Builds but here's the part relevant to your situation: 有关Unity WebGL Builds网络的一些很好的信息,但这里有与您的情况相关的部分:

Due to security implications, JavaScript code does not have direct access to IP Sockets to implement network connectivity. 由于安全隐患,JavaScript代码无法直接访问IP套接字以实现网络连接。 As a result, the .NET networking classes (ie, everything in the System.Net namespace, particularly System.Net.Sockets) are non-functional in WebGL . 因此,.NET网络类(即System.Net命名空间中的所有内容,特别是System.Net.Sockets)在WebGL中不起作用。 The same applies to Unity's old UnityEngine.Network* classes, which are not available when building for WebGL. 这同样适用于Unity的旧UnityEngine.Network *类,这些类在构建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. 如果您需要在WebGL中使用Networking,您当前可以选择使用Unity中的WWW或UnityWebRequest类或支持WebGL的新Unity Unity功能,或者使用JavaScript中的WebSockets或WebRTC实现您自己的网络。

Keep in mind the "new Unity Networking features" it mentions are part of UNet, which is being deprecated . 请记住,它提到的“新的Unity网络功能”是UNet的一部分, 它已被弃用 EDIT: It looks like the entire NetworkTransport API is gone. 编辑:看起来整个NetworkTransport API都消失了。


Hopefully you can find a solution while digging through those articles. 希望您在挖掘这些文章的同时找到解决方案。 I haven't tried anything with UNet, WebSockets, or WebRTC but personally, to solve this issue in my own WebGL projects, I use UnityWebRequest to send POST requests to a server I'm hosting on AWS that's running a LAMP stack. 我没有尝试使用UNet,WebSockets或WebRTC进行任何操作,但就个人而言,为了在我自己的WebGL项目中解决这个问题,我使用UnityWebRequestPOST请求发送到我正在运行LAMP堆栈的AWS上托管的服务器。 My php pages process the requests and the MySQL database is sitting there on the same server. 我的php页面处理请求,MySQL数据库坐在同一台服务器上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM