Unity3d MYSQL大对象上传 [英] Unity3d MYSQL large objects upload

查看:74
本文介绍了Unity3d MYSQL大对象上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I`m currently developing a videogame that needs to upload levels, records, and some other large data strunctures to a MySQL database . I parse those objects to an hex string and upload the data to my database througth a WWW post in Unity3D as varbinary.

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

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

Since the length of the url is limited in size, I have to split the hex string , upload it in parts and merge the parts again on download.

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#脚本上载所有对象codedLevel

How I can upload all the object codedLevel from C# script in Unity3D¿

谢谢!

推荐答案

由于网址的长度受大小限制,因此我必须将 十六进制字符串,将其部分上传,然后在下载时再次合并这些部分.

Since the length of the url is limited in size, I have to split the hex string , upload it in parts and merge the parts again on download.

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

Only the GET method has a limited length which is 2048 characters and you are currently using the GET method for your request.

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

This should be done with the POST method with the help of the WWWForm class.

假设您要发送播放器的nameagescore,我们可以使用以下代码对其进行编码:

Let's say you want to send the player's name, age and score, We can encode it with the code below:

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

添加播放器的个人资料图片或某种数组数据吗?

Add the player's profile picture or array data of some kind?

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

form.AddBinaryData("profilePic", bytes);

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

Now, let's send this to the Server.

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

就是这样.您无需通过for循环逐个发送此邮件.

That's it. You don't need to send this piece by piece with a for loop.

这篇关于Unity3d MYSQL大对象上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆