เริ่มจากการส่งค่าไปบันทึกกันก่อนเลยครับ
1. ให้ทำการแก้ไข IEnumerator UploadRank() โดยการเพิ่ม WWWForm เข้ามา
IEnumerator UploadRank()
{
if (!string.IsNullOrEmpty(email))
{
string url = "http://savedata.com/index.php";
// เป็นคำสั่งสำหรับส่งค่าให้กับ PHP
WWWForm form = new WWWForm();
// AddField(ชื่อตัวแปรที่ส่ง, ค่าที่ส่ง)
form.AddField("PlayerName", "Kongkiat");
form.AddField("UserPic", picture.ToString());
form.AddField("Email", email);
form.AddField("score", PlayerDataHelper.GetScore());
// เรียก URL ที่เรากำหนด
WWW uplaod = new WWW(url);
// รอจนกว่าจะ Upload เสร็จครับ
yield return uplaod;
if (!string.IsNullOrEmpty(uplaod.error))
{
Debug.Log(uplaod.error);
}
else
{
Debug.Log("Finished Uploading");
}
}
$query = 'INSERT INTO playerscore(UserPic, PlayerName, Email, Score, CreateDate) VALUES (NULL, \''.$_POST['PlayerName'].'\', \''.$_POST['Email'].'\', '.$_POST['score'].', now())';
1. ให้ทำการสร้างไฟล์ rank.php ครับ จากนั้นทำการเพิ่ม
<?php
$host = ''; // ชื่อ Host
$dbName = ''; // ชื่อ Database
$user = ''; // username เข้าฐานข้อมูล
$pass = ''; // password เข้าฐานข้อมูล
// เชื่อต่อฐานข้อมูล
$conn = mysql_connect($host, $user, $pass);
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
// เลือก Database
$selectDB = mysql_select_db($dbName ,$conn);
// ดึงข้อมูล 10 คนแรกที่คะแนนเยอะที่สุด
$query = 'select * from playerscore order by score desc LIMIT 0, 10';
mysql_query("set names 'utf8'");
$result = mysql_query($query);
if($result)
{
while ($row = mysql_fetch_array($result))
{
// ส่งข้อมูลกลับ Unity รูปแบบ {id}:{PlayerName}:{Email}:{Score}|
// สามารถทำเปลี่ยนเป็น format อื่นได้ครับ
echo $row['Id'] . ":"
. $row['PlayerName'] . ":"
. $row['Email'] . ":"
. $row['Score'] . "|";
}
}
mysql_close($conn);
?>
IEnumerator GetTopTenRank()
{
string url = "http://loaddata.com/rank.php";
WWW download = new WWW(url);
// รอจนกว่าจะโหลดเสร็จ
yield return download;
if (!string.IsNullOrEmpty(download.error))
{
print("Error downloading: " + download.error);
}
else
{
// แสดงข้อความที่โหลดลงมา
Debug.Log(download.text);
}
}
หวังว่าจะเป็นประโยชน์ ไม่มากก็น้อยนะครับ
NO GAME NO DREAM!!!!