douba1498
douba1498
2016-07-18 14:35

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 duanqin2026 5年前

    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.

    点赞 评论 复制链接分享

为你推荐