C# Unity3d MYSQL大型对象上传
我目前正在开发一款视频游戏,需要将关卡、记录和其他一些大型数据结构上传到MySQL数据库。我将这些对象解析为一个十六进制字符串,并通过Unity3D中的WWW post将数据作为varbinary上传到我的数据库C# Unity3d MYSQL大型对象上传,c#,php,mysql,unity3d,C#,Php,Mysql,Unity3d,我目前正在开发一款视频游戏,需要将关卡、记录和其他一些大型数据结构上传到MySQL数据库。我将这些对象解析为一个十六进制字符串,并通过Unity3D中的WWW post将数据作为varbinary上传到我的数据库 object codedLevel = SaveGame.codedLevel; byte[] codedLevelBin = ObjectToByteArray(codedLevel); string codedLevelStr = "0x" + BitConverter.ToStr
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
}
int partSize=2000;
用于(int i=0;i零件尺寸)
part=codedLevelStr.子字符串(i,零件尺寸);
else if(codedLevelStr.Length<零件尺寸)
part=codedLevelStr;
其他的
部分=codedLevelStr.子串(i);
codedLevelLengthParts=codedLevelLengthParts+零件长度;
//这将连接到服务器端php脚本,该脚本将向MySQL数据库添加级别。
//为其提供表示级别的字符串
string hash=Md5Sum(User+part+i+LVLName+secretKey);
字符串post_url=addLevelURL+“&LVL=“+part+”&name=“+WWW.EscapeURL(User)+”&part=“+i/partSize+”&LVLName=“+WWW.EscapeURL(LVLName)+”&hash=“+hash;
//将URL发布到站点并创建下载对象以获得结果。
WWW hs_post=新的WWW(post_url);
收益率返回hs_post;//等待下载完成
}
如何从Unity3D中的C#脚本上传所有对象代码级别
谢谢
由于url的长度在大小上是有限的,所以我必须拆分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
}
只有GET
方法的长度有限,即2048
个字符,您当前正在使用GET
方法处理您的请求
这应该通过POST
方法在WWWForm
类的帮助下完成
假设您要发送球员的姓名
、年龄
和分数
,我们可以使用以下代码对其进行编码:
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");
或
现在,让我们将其发送到服务器
WWW connection = new WWW(url, form);
yield return connection;
就这样。您不需要使用for
循环逐件发送此文件