我正在制作RPG游戏,用户可以登录并保存和加载数据。我可以登录并保存。但是,当我想加载数据时,我很难加载它。
我将仅发布必要的代码(PHP代码(Load.php)):
<?PHP
$Username = $_POST['Username'];
$Location = $_POST['Location'];
$Gold = $_POST['Gold'];
$Level = $_POST['Level'];
$Attack = $_POST['Attack'];
$Defense = $_POST['Defense'];
$MagicDefense = $_POST['MagicDefense'];
$Evasion = $_POST['Evasion'];
$Health = $_POST['Health'];
$MaxHealth = $_POST['MaxHealth'];
$Mana = $_POST['Mana'];
$MaxMana = $_POST['MaxMana'];
$Exp = $_POST['Exp'];
$NextExp = $_POST['NextExp'];
$AcceptedChiefQuest = $_POST['AcceptedChiefQuest'];
$AddedChiefQuest = $_POST['AddedChiefQuest'];
$con = mysql_connect("SERVER_NAME","DATABASE_NAME","PASSWORD") or ("Cannot connect!" . mysql_error());
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("DATABASE_NAME" , $con) or die ("Could not load the database" . mysql_error());
$check = mysql_query("SELECT * FROM Information WHERE `Username` = '".$Username."'");
$numrows = mysql_num_rows($check);
if ($numrows > 0)
{
while($row = mysql_fetch_assoc($check))
{
$Username = $row['Username'];
$Location = $row['Location'];
$Gold = $row['Gold'];
$Level = $row['Level'];
$Attack = $row['Attack'];
$Defense = $row['Defense'];
$MagicDefense = $row['MagicDefense'];
$Evasion = $row['Evasion'];
$Health = $row['Health'];
$MaxHealth = $row['MaxHealth'];
$Mana = $row['Mana'];
$MaxMana = $row['MaxMana'];
$Exp = $row['Exp'];
$NextExp = $row['NextExp'];
$AcceptedChiefQuest = $row['AcceptedChiefQuest'];
$AddedChiefQuest = $row['AddedChiefQuest'];
echo $Username;
echo $Location;
}
die();
}
else
{
die ("Data does not exist!");
}
?>
在这里我通过Unity中的脚本访问它(问题所在的必要代码):
IEnumerator LoadCoroutine(WWW _www)
{
yield return _www;
if (_www.error == null)
{
message = _www.text;
if (_www.text == "Data does not exist!")
{
message = "Data does not exist";
clickedLoadGame = false;
}
else if (_www.text == "Saved data does not match!")
{
message = "Saved data does not match";
clickedLoadGame = false;
}
else
{
if (_www.text == "Village")
{
GameManager.CurrentLocation = "Village";
GameManager.Loader = 1;
}
else if (_www.text == "Yein Plain")
{
GameManager.CurrentLocation = "Yein Plain";
GameManager.Loader = 4;
}
message = "Successfully Loaded.";
mainMenu = false;
yield return new WaitForSeconds(3);
Reset();
GameManager.LoadLevel("Loading Scene");
}
}
else
{
message = "Error while trying to connect to the server.\nMessage: " + _www.error;
clickedLoadGame = false;
}
}
脚本只返回基于PHP代码的单个值 $ Location ,当我想要检索 $ Username 时, _www。文字将是 VillageYunnan 这是我不想要的,我只希望单个值会打印在 _www.text 上,就像村庄或云南而不是 VillageYunnan ,这只是2个值,如果我想要检索PHP中的所有 $ ,它将是喜欢 VillageYunnan15101010100 等等,这将非常难以维护。
我的问题是我只希望 _www.text 只打印像单个值,而不是像我之前提到的那样组合在一起。
任何帮助?
真的很感激你的回答。
谢谢
已编辑:(必填代码)
GameManager.cs
public static void WebsiteConnect(string link)
{
wwwForm = new WWWForm();
wwwForm.AddField("Username", LoggedInUsername);
wwwForm.AddField("Gold", UserInformation.CurrentGold);
wwwForm.AddField("Level", Status.level);
wwwForm.AddField("Attack", Status.attack);
wwwForm.AddField("Defense", Status.defense);
wwwForm.AddField("MagicDefense", Status.magicDefense);
wwwForm.AddField("Evasion", Status.evasion);
wwwForm.AddField("Health", Status.hp);
wwwForm.AddField("MaxHealth", Status.maxHp);
wwwForm.AddField("Mana", Status.mp);
wwwForm.AddField("MaxMana", Status.maxMp);
wwwForm.AddField("Exp", Status.CurrentExperience);
wwwForm.AddField("NextExp", Status.NextLevelExperience);
wwwForm.AddField("AcceptedChiefQuest", QuestManager._acceptedChiefQuest);
wwwForm.AddField("AddedChiefQuest", QuestManager._addedChiefQuest);
}
www = new WWW(link, wwwForm);
}
我这样称呼它:
private void OnGUI()
{
if (GUILayout.Button("Continue", customStartButton.customStyles[0]))
{
clickedLoadGame = true;
audio.PlayOneShot(audioSEClip);
Load();
}
}
private void Load()
{
GameManager.WebsiteConnect("WEBSITE_ADDRESS/Load.php");
StartCoroutine(LoadCoroutine(GameManager.www));
}
IEnumerator LoadCoroutine(WWW _www)
{
yield return _www;
if (_www.error == null)
{
message = _www.text;
var jsonObject = JSON.Parse(_www.text);
//string jsonString = jsonObject.ToString();
Debug.Log(jsonObject);
if (_www.text == "Data does not exist!")
{
message = "Saved data does not match!";
mainMenu = false;
fadeAudio = false;
yield return new WaitForSeconds(3);
clickedLoadGame = false;
Reset();
}
else
{
if (_www.text == "Village")
{
message = "Successfully Loaded.";
GameManager.CurrentLocation = "Village";
GameManager.Loader = 1;
mainMenu = false;
fadeAudio = true;
yield return new WaitForSeconds(3);
Reset();
GameManager.LoadLevel("Loading Scene");
}
else if (_www.text == "Yein Plain")
{
message = "Successfully Loaded.";
GameManager.CurrentLocation = "Yein Plain";
GameManager.Loader = 4;
mainMenu = false;
fadeAudio = true;
yield return new WaitForSeconds(3);
Reset();
GameManager.LoadLevel("Loading Scene");
}
else
{
message = "Data does not exist!";
mainMenu = false;
fadeAudio = false;
yield return new WaitForSeconds(3);
clickedLoadGame = false;
Reset();
}
}
}
else
{
message = "Error while trying to connect to the server.\nMessage: " + _www.error;
clickedLoadGame = false;
}
}
这是PHP代码(Load.php):
<?PHP
$Username = $_POST['Username'];
$Location = $_POST['Location'];
$Gold = $_POST['Gold'];
$Level = $_POST['Level'];
$Attack = $_POST['Attack'];
$Defense = $_POST['Defense'];
$MagicDefense = $_POST['MagicDefense'];
$Evasion = $_POST['Evasion'];
$Health = $_POST['Health'];
$MaxHealth = $_POST['MaxHealth'];
$Mana = $_POST['Mana'];
$MaxMana = $_POST['MaxMana'];
$Exp = $_POST['Exp'];
$NextExp = $_POST['NextExp'];
$AcceptedChiefQuest = $_POST['AcceptedChiefQuest'];
$AddedChiefQuest = $_POST['AddedChiefQuest'];
$con = mysql_connect("WEBSITE_ADDRESS","DATABASE_NAME","PASSWORD") or ("Cannot connect!" . mysql_error());
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("DATABASE_NAME" , $con) or die ("Could not load the database" . mysql_error());
$check = mysql_query("SELECT * FROM Information WHERE `Username` = '".$Username."'");
$numrows = mysql_num_rows($check);
if ($numrows > 0)
{
while($row = mysql_fetch_assoc($check))
{
if ($Username == $row['Username'])
{
$Username = $row['Username'];
$Location = $row['Location'];
$Gold = $row['Gold'];
$Level = $row['Level'];
$Attack = $row['Attack'];
$Defense = $row['Defense'];
$MagicDefense = $row['MagicDefense'];
$Evasion = $row['Evasion'];
$Health = $row['Health'];
$MaxHealth = $row['MaxHealth'];
$Mana = $row['Mana'];
$MaxMana = $row['MaxMana'];
$Exp = $row['Exp'];
$NextExp = $row['NextExp'];
$AcceptedChiefQuest = $row['AcceptedChiefQuest'];
$AddedChiefQuest = $row['AddedChiefQuest'];
json_encode($row);
}
else
{
die ("Saved data does not match!");
}
}
die();
}
else
{
die ("Data does not exist!");
}
?>
当我删除所有$Username
等等并仅用json_encode($row)
替换它时,如果PHP代码保持如上,结果仍然相同,则会出现{{1}的错误} string jsonString = jsonObject.ToString();
为空。
答案 0 :(得分:1)
您可以使用一些通用格式(如xml或json)在不同的应用程序之间共享结构化数据。
在PHP代码中替换:
$Username = $row['Username'];
$Location = $row['Location'];
$Gold = $row['Gold'];
$Level = $row['Level'];
$Attack = $row['Attack'];
$Defense = $row['Defense'];
$MagicDefense = $row['MagicDefense'];
$Evasion = $row['Evasion'];
$Health = $row['Health'];
$MaxHealth = $row['MaxHealth'];
$Mana = $row['Mana'];
$MaxMana = $row['MaxMana'];
$Exp = $row['Exp'];
$NextExp = $row['NextExp'];
$AcceptedChiefQuest = $row['AcceptedChiefQuest'];
$AddedChiefQuest = $row['AddedChiefQuest'];
echo $Username;
echo $Location;
使用:
echo json_encode($row);
在C#代码中,使用一些JSON库(如SimpleJSON)来解析JSON字符串并检索编码数据。
答案 1 :(得分:0)
我不是100%确定我得到了问题,但我认为这是导致问题的原因:
echo $Username;
echo $Location;
这会输出YunnanVillage
,这很糟糕。替换为:
echo '[' . $Username . ',' . $Location . ']';
这将输出[Yunnan, Village]
。您现在可以更轻松地解析此字符串(它是JSON格式)。请注意,这是一个非常简单的示例,有更好的方法可以在PHP中输出数组,例如:
json_encode(array('Username' => $Username, 'Location' => $Location));
我在这里是因为c#标签但知道PHP足以看到代码中的错误。希望这会有所帮助。