2016-07-18 28 views
1

我目前正在開發一個視頻遊戲,需要將級別,記錄和一些其他大數據結構上傳到MySQL數據庫。我將這些對象解析爲一個十六進制字符串,然後將數據通過Unity3D中的WWW作爲varbinary上傳到我的數據庫。Unity3d MYSQL大對象上傳

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 
} 

如何上傳所有的對象codedLevel從C#腳本Unity3D¿

謝謝!

回答

1

由於URL的長度有大小的限制,我不得不拆分 十六進制字符串,可以直接上傳到零件和下載再合併部分。只有

GET方法具有有限的長度爲2048人物和你目前正在使用您的要求GET方法。

這應該在WWWForm類的幫助下用POST方法完成。

比方說,你要發送的玩家的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循環一起發送。

+0

非常感謝!!!!!! 10/10 – Charles

+0

@Charles歡迎您! – Programmer