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.

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?