将Unity3d WebGL项目构建连接到数据库时遇到问题,在编辑器中工作正常

时间:2019-03-22 20:40:47

标签: c# mysql unity3d unity-webgl

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

spring.kafka.producer.keySerializer=org.apache.kafka.common.serialization.StringSerializer
spring.cloud.stream.bindings.my-message-out.destination=my.message.destination
spring.cloud.stream.bindings.my-message-out.content-type=application/json
spring.cloud.stream.bindings.my-message-out.producer.headerMode=raw
spring.cloud.stream.bindings.my-message-out.producer.partitionKeyExtractorClass=com.example.message.TransactionKeyExtractor
spring.cloud.stream.bindings.my-message-out.producer.partitionCount=80

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

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

这是Google Chrome浏览器中的控制台日志:

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());
        }
    }
}

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

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中:

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)

有问题的行是

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);
}
}

我已经将System.Data.dll和MySql.Data.dll包括在我的Visual Studio引用中,并且还包括所有I18N文件。我还把这些文件放在了我构建的项目文件路径中。

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

1 个答案:

答案 0 :(得分: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中使用网络,则当前可以选择   在Unity或新的Unity中使用WWW或UnityWebRequest类   支持WebGL或实现自己的网络功能   使用JavaScript中的WebSocket或WebRTC进行联网。

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


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