unityとサーバ間の連携について、unity→phpへ情報をポストする流れをまとめておきます。
unityのhierarchyはこんな感じです。
画面はこうなってます。NGUIのボタンだけが付いてる状態です。
このボタンにはButtonManagerというスクリプトがAddComponentで付けられてます。
ButtonManager.cs
public void OnClickButton(){ //Connectコルーチンの実行 StartCoroutine (Connect()); } private IEnumerator Connect(){ string url = "http://localhost:80/unity_text.php"; //WWWForm:WWWクラスを使用してwebサーバにポストするフォームデータを生成するヘルパークラス WWWForm wwwForm = new WWWForm(); //AddFieldでfieldに値を格納 wwwForm.AddField ("text","テキストだよー"); //WWWオブジェクトにURL,WWWFormをセットすることでPOST,GETを行える。 WWW www = new WWW(url,wwwForm); //実行 yield return www; }
<?php $text = $_POST['text']; $fno = fopen("test.txt",'a'); fwrite($fno,$text); fclose($fno); ?>
text.txt
無事に値が送られています。