douba1498 2016-07-18 14:35
浏览 95
已采纳

Unity3d MYSQL大对象上传

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("-", "");

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 
}

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

Thanks!

  • 写回答

1条回答 默认 最新

  • duanqin2026 2016-07-18 23:50
    关注

    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.

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

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

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

    or

    form.AddBinaryData("profilePic", bytes);
    

    Now, let's send this to the Server.

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置