Unity3d MYSQL大型对象上传

本文关键字:对象 大型 MYSQL Unity3d | 更新日期: 2023-09-27 17:57:41

我目前正在开发一款视频游戏,需要将级别、记录和其他一些大型数据结构上传到MySQL数据库。我将这些对象解析为十六进制字符串,并通过Unity3D中的WWW帖子将数据以varbinary的形式上传到我的数据库。

object codedLevel = SaveGame.codedLevel;
byte[] codedLevelBin = ObjectToByteArray(codedLevel);
string codedLevelStr = "0x" + BitConverter.ToString (codedLevelBin).Replace("-", "");

由于url的长度大小有限,我必须拆分十六进制字符串,将其分为多个部分上传,并在下载时再次合并这些部分。

int partSize = 2000; 
for( int i= 0; i <= codedLevelStr.Length   ;i = i+partSize){
    string part = "";
    if (codedLevelStr.Length - i > partSize)
        part = codedLevelStr.Substring (i, partSize);
    else if (codedLevelStr.Length < partSize)
        part = codedLevelStr;
    else
        part = codedLevelStr.Substring (i);
    codedLevelLengthParts = codedLevelLengthParts + part.Length;
    //This connects to a server side php script that will add the level to a MySQL DB.
    // Supply it with a string representing the level
    string hash = Md5Sum(User + part+ i + LVLName +  secretKey);
    string post_url = addLevelURL+ "&LVL=" + part + "&name=" + WWW.EscapeURL(User)  + "&part=" + i/partSize + "&LVLName=" + WWW.EscapeURL(LVLName) + "&hash=" + hash;
    // Post the URL to the site and create a download object to get the result.
    WWW hs_post = new WWW(post_url);
    yield return hs_post; // Wait until the download is do 
}

如何在Unity3D中从C#脚本上传所有对象代码级别?

谢谢!

Unity3d MYSQL大型对象上传

由于url的长度大小有限,我不得不将十六进制字符串,将其分部分上传,并在下载时再次合并这些部分。

只有GET方法的长度有限,为2048个字符,并且您当前正在使用GET方法进行请求。

这应该在WWWForm类的帮助下使用POST方法来完成。

假设你想发送玩家的nameagescore,我们可以用下面的代码进行编码:

WWWForm form = new WWWForm();
form.AddField("name", "Charles");
form.AddField("age", 29);
form.AddField("scrore", 67);

添加玩家的个人资料图片或某种数组数据?

byte[] bytes = playerProfilePic;
form.AddBinaryData("profilePic", bytes, "profilePic.png", "image/png");

form.AddBinaryData("profilePic", bytes);

现在,让我们将其发送到服务器。

WWW connection = new WWW(url, form);
yield return connection;

就是这样。你不需要用for循环一块一块地发送这个。