我目前正在开发一个视频游戏,需要将级别,记录和其他一些大型数据结构上传到MySQL数据库。我将这些对象解析为十六进制字符串,并通过Unity3D中的WWW帖子将数据上传到我的数据库中作为varbinary。
object codedLevel = SaveGame.codedLevel;
byte[] codedLevelBin = ObjectToByteArray(codedLevel);
string codedLevelStr = "0x" + BitConverter.ToString (codedLevelBin).Replace("-", "");
由于网址的长度有限,我必须拆分十六进制字符串,将其上传部分并在下载时再次合并部分。
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
谢谢!
答案 0 :(得分:1)
由于网址的长度有限,我不得不拆分 十六进制字符串,将其部分上传并在下载时再次合并部分。
只有GET
方法的长度有限,为2048
个字符,而您当前正在使用GET
方法处理您的请求。
这应该在POST
类的帮助下使用WWWForm
方法完成。
假设您要发送播放器的name
,age
和score
,我们可以使用以下代码对其进行编码:
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
循环逐个发送此文件。