การส่ง Data ด้วย WWWForm จะเป็นการส่งแบบ POST นะครับ
เริ่มกันเลย
1. ให้ทำการ สร้าง file C# Script แล้วทำการเพิ่ม
using System.Collections;
public class SaveData : MonoBehaviour
{
private string saveURL = "http://www.test.com/save.php";
void Start()
{
StartCoroutine(SendData());
}
IEnumerator SendData()
{
Debug.Log("SAVE DATA");
yield return new WaitForEndOfFrame();
WWWForm form = new WWWForm();
form.AddField("playerName", "Kongkiat");
form.AddField("health", 9999);
WWW w = new WWW(saveURL, form);
yield return w;
if(!string.IsNullOrEmpty(w.error))
{
Debug.LogError(w.error);
}
else
{
Debug.Log(w.text);
}
}
}
<?php
if(isset($_POST['playerName']) && isset($_POST['health']))
{
// ทำการเซฟข้อมูลลง Database
echo "Save Success";
}
else
{
echo "Can't Save!!!!";
}
?>
4. รัน Unity เพื่อทดสอบ เท่านี้ก็เป็นอันเสร็จครับ
ปล. มีอะไรแนะนำได้นะครับ