###Unityを使用した画像データのMySQLへの保存とUnityでの表示
###発生している問題・エラーメッセージ
ご覧いただきありがとうございます。
現在Unityを使用して簡単なシューチングゲームを作成しているのですがその機能の一つとしてランキング機能(ユーザー名、スコア、画面のスクリーンショットの表示)を作成しております。
UnityとPHPを使用した画像データ(画面のスクリーンショット)の送受信なのですが方法がわからず困っております。サーバーへの送信部分は一応書いたのですがform.AddBinaryDataのところでNULLが出てしまっています。
文字列や数値は送受信ができているのが確認できております。
受信はJsonを使用しているのですがJsonだとバイナリーは対応していないと出てきたのですが方法はありますでしょうか?
送信部分 using UnityEngine; using System.Collections; using System.IO; public class Score : MonoBehaviour { // スコアを表示するGUIText public GUIText scoreGUIText; // ハイスコアを表示するGUIText public GUIText highScoreGUIText; // スコア private int score; // ハイスコア private int highScore; // PlayerPrefsで保存するためのキー private string highScoreKey = "highScore"; string m_url = 該当のURL string PlayerName; Texture2D tex; byte[] pngdata; bool isCreate = true; // Use this for initialization void Start () { Initialize(); } // Update is called once per frame void Update () { // スコアがハイスコアより大きければ if (highScore < score) { highScore = score; } // スコア・ハイスコアを表示する scoreGUIText.text = score.ToString(); highScoreGUIText.text = "HighScore : " + highScore.ToString(); } // ゲーム開始前の状態に戻す private void Initialize() { // スコアを0に戻す score = 0; highScore = 0; // ハイスコアを取得する。保存されてなければ0を取得する。 //highScore = PlayerPrefs.GetInt(highScoreKey, 0); } public IEnumerator LoadScreenshot() { yield return new WaitForEndOfFrame(); tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); tex.Apply(); pngdata = tex.EncodeToPNG(); Destroy(tex); } // ポイントの追加 public void AddPoint(int point) { score = score + point; } public void SetName(string name) { PlayerName = name; } // ハイスコアの保存 public void Save() { // ハイスコアを保存する PlayerPrefs.SetInt(highScoreKey, highScore); PlayerPrefs.Save(); StartCoroutine("SetScore"); // ゲーム開始前の状態に戻す Initialize(); } //サーバーにハイスコアを送信 IEnumerator SetScore() { WWWForm form = new WWWForm(); form.AddField("name", PlayerName); form.AddField("score", score); form.AddField("frameCount", Time.frameCount.ToString()); form.AddBinaryData("fileUpload", pngdata, "screenShot.png", "image/png"); WWW result = new WWW(m_url, form.data); yield return result; if(result.error == null) { Debug.Log("登録完了"); } } }
php $name = $_REQUEST['name']; $score = $_REQUEST['score']; $screen = $_REQUEST['screen']; print($screen); $sql = "INSERT INTO Score(name, score, screen) VALUES('$name', '$score', 'screen')"; $result_flag = mysql_query($sql); //クエリ文の実行 if(!$result_flag) { die('INSERTクエリーが失敗しました。'.mysql_error()); }
受信部分 IEnumerator GetScore() { WWW result = new WWW(url); yield return result; if (result.error == null) { rdbUserGet = new JSONObject(result.text); for (int ii = 0; ii < rdbUserGet.Count; ii++) { JSONObject jsonPos = rdbUserGet[ii]; JSONObject jsonName = jsonPos.GetField("name"); JSONObject jsonScore = jsonPos.GetField("score"); PlayerName = jsonName.str; Score = jsonScore.str; if (PlayerName == "" || Score == "") continue; top = new GameObject("name_" + ii); score = new GameObject("score_" + ii); GUIText Text = top.AddComponent<GUIText>(); GUIText Text2 = score.AddComponent<GUIText>(); Text.fontSize = 20; Text2.fontSize = 20; Text.text = Rank + ":" + PlayerName; Text2.text = Score; top.transform.position = new Vector3(0.1f, 0.6f + move, 0); score.transform.position = new Vector3(0.5f, 0.6f + move, 0); a = top.GetComponent<GUIText>().color; a.a = 0.0f; top.GetComponent<GUIText>().color = a; score.GetComponent<GUIText>().color = a; move -= 0.05f; Rank += 1; Debug.Log("名前:" + PlayerName + "スコア:" + Score); } } else { string error = result.error; Debug.LogError("www Error:" + result.error); } }
php $query = "SELECT name, score FROM Score Order by score desc"; if($result = mysql_query($query)) { $user = array(); // 結果をオブジェク形式で出力. while($row = mysql_fetch_assoc($result)) { //print($row['name']); //print($row['score']); //$user[] = array('name'=> $row->name, 'score'=> $row->score); $user[] = $row; //var_dump($user); } // 出力結果が空の時は、nullを出し、JSON形式で変換. if(empty($user)) { $user = null; echo json_encode( $user ); } else { echo json_encode( $user ); } // 結果を解放 //$result->close(); }
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
Unity5.4使用
PHP5.3
Json使用
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/11/28 09:30
2016/11/28 10:43