【问题标题】:Unity3d MYSQL large objects uploadunity3d MYSQL 大对象上传
【发布时间】:2016-07-18 14:35:26
【问题描述】:

我目前正在开发一款需要将关卡、记录和其他一些大型数据结构上传到 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# 脚本上传所有 codedLevel 对象¿

谢谢!

【问题讨论】:

    标签: c# php mysql unity3d


    【解决方案1】:

    由于 url 的长度有大小限制,所以我必须拆分 hex string ,分块上传,下载时再次合并。

    只有GET 方法的长度有限,即2048 字符,并且您当前正在使用GET 方法来处理您的请求。

    这应该通过POST 方法在WWWForm 类的帮助下完成。

    假设您要发送播放器的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 循环逐个发送此内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-08
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多