2015-11-23 43 views
3

我必須找到一種方法來使用c#腳本和php保存並加載數據庫中的遊戲狀態。到目前爲止,我可以將播放器位置存儲到本地註冊表中,但是將其遠程保存是問題所在。有誰知道我需要實施這個步驟的列表?如何將遊戲狀態存儲到數據庫並使用php和c#腳本加載它

using UnityEngine; 
using System.Collections; 
using System; 
using System.Runtime.Serialization.Formatters.Binary; 
using System.IO; 

public class SaveSystem : MonoBehaviour { 

    // Use this for initialization 
    public void SaveState() { 
     BinaryFormatter bf = new BinaryFormatter(); 
     FileStream file = File.Create (Application.persistentDataPath + "/PlayerData.dat"); 
     PlayerData data = new PlayerData(); 

     data.posX = transform.position.x; 
     data.posY = transform.position.y; 
     data.posZ = transform.position.z; 

     data.rotX = transform.eulerAngles.x; 
     data.rotY = transform.eulerAngles.y; 
     data.rotZ = transform.eulerAngles.z; 

     bf.Serialize (file, data); 
     file.Close(); 

    } 

    // Update is called once per frame 
    public void LoadState() { 
     if(File.Exists(Application.persistentDataPath + "/PlayerData.dat")){ 
      BinaryFormatter bf = new BinaryFormatter(); 
      FileStream file = File.Open(Application.persistentDataPath + "/PlayerData.dat", FileMode.Open); 
      PlayerData data = (PlayerData) bf.Deserialize(file); 
      file.Close(); 
      transform.position = new Vector3(data.posX, data.posY, data.posZ); 
      transform.rotation = Quaternion.Euler(data.rotX, data.rotY, data.rotZ); 
     } 


     [Serializable] 
    class PlayerData{ 
     public float posX; 
     public float posY; 
     public float posZ; 

     public float rotX; 
     public float rotY; 
     public float rotZ; 
    } 

回答

0

行,所以我不記得正確的語法,但繼承人的東西讓你開始,所以在服務器上創建一個PHP文件,併爲負載創建功能和保存 和創造得到這樣的變量。

  if(isset($GET_["save"]){ 
      SaveFunctionName(); 
      } 

現在,當你想將數據發送到服務器,然後調用這個函數做到這一點:

public string url = "Your server url?ur get variable name"; 
       IEnumerator Start() { 
       WWWForm form = new WWWForm(); 
       form.AddBinaryData("fileUpload", bytes); 
       WWW w = new WWW(screenShotURL, form); 
       yield return w; 
       } 

確保你從PHP返回回一個字符串內W¯¯檢查,如果數據被保存與否。

0

到目前爲止,我找到了一種方法來存儲播放器位置位置X,Y,Z和旋轉X,Y,Z到我的數據庫統一C#腳本看起來像這樣的這種實現它

[Serializable] 
    class PlayerData{ 
     public float posX; 
     public float posY; 
     public float posZ; 

     public float rotX; 
     public float rotY; 
     public float rotZ; 

    } 

public void SaveState() { 
     PlayerData data = new PlayerData(); 

     data.posX = transform.position.x; 
     data.posY = transform.position.y; 
     data.posZ = transform.position.z; 

     data.rotX = transform.eulerAngles.x; 
     data.rotY = transform.eulerAngles.y; 
     data.rotZ = transform.eulerAngles.z; 
     //data.posX.ToString(); 
     //player = new Player(); 


     WWWForm form = new WWWForm(); 
     form.AddField("positionX", data.posX.ToString()); 
     form.AddField("positionY", data.posY.ToString()); 
     form.AddField("positionZ", data.posZ.ToString()); 
     form.AddField("rotateX", data.rotX.ToString()); 
     form.AddField("rotateY", data.rotY.ToString()); 
     form.AddField("rotateZ", data.rotZ.ToString()); 
     WWW w = new WWW("http://localhost:8080/player.php", form); 
     StartCoroutine(saved(w)); 
     //bf.Serialize (file, data); 
     //file.Close(); 


IEnumerator saved(WWW w) 
{ 
    yield return w; 
    if (w.error == null) 
    { 
     if (w.text == "login-SUCCESS") 
     { 
      print("WOOOOOOOOOOOOOOO!"); 
     } 
     else 
      message += w.text; 
    } 
    else 
    { 
     message += "ERROR: " + w.error + "\n"; 
    } 
}  
的最有效的方法

}

上保存在數據庫中我的球員狀態我的服務器稱爲玩家創建一個PHP文件

<?PHP 

$positionX = $_POST['positionX']; 
$positionY = $_POST['positionY']; 
$positionZ = $_POST['positionZ']; 
$roX = $_POST['rotateX']; 
$roY = $_POST['rotateY']; 
$roZ = $_POST['rotateZ']; 

$con = mysql_connect("localhost","root","usbw") or ("Cannot connect!" . mysql_error()); 
if (!$con) 
    die('Could not connect: ' . mysql_error()); 

mysql_select_db("game" , $con) or die ("could not load the database" . mysql_error()); 



//$pass = md5($pass); 
$ins = mysql_query("INSERT INTO `player` ( `Player_ID`, `positionX` , `positionY` , `positionZ` , `rotateX` , `rotateY` , `rotateZ`) VALUES ('' , '".$positionX."' , '".$positionY."' , '".$positionZ."', '".$roX."' , '".$roY."' , '".$roZ."') ; "); 
     if ($ins) 
      die ("Succesfully Created User!"); 
     else 
      die ("Error: " . mysql_error()); 
    ?> 
相關問題