Last active
April 1, 2020 15:47
-
-
Save gologius/ddc4d2f1b3d9c3fc632922f767bca13b to your computer and use it in GitHub Desktop.
Unityでサーバーと通信するためのサンプルみたいなもの
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//使っていたのを見せるために作ったので、このまま丸々コピペしてもうごきません | |
// | |
//MiniJson.csをDLしてAssetsフォルダ内に入れる必要があります | |
//詳しくは http://qiita.com/phi/items/914bc839b543988fc0ec あたりを参考にしてください.すぐできます | |
using UnityEngine; | |
using UnityEngine.UI; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Text; | |
using MiniJSON; | |
public class Test : MonoBehaviour { | |
// Use this for initialization | |
void Start () { | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
//JSONデータを送信して、プレイヤーデータを受信する | |
private IEnumerator getPlayerFromServer() | |
{ | |
//送信データの作成 | |
Dictionary<string, string> header = new Dictionary<string, string>(); | |
header.Add("Content-Type", "application/json"); | |
Dictionary<string, object> senddata = new Dictionary<string, object>(); | |
senddata["playerid"] = PlayerPrefs.GetInt("playerid"); | |
//変換 | |
string sendJSON = Json.Serialize(senddata); | |
byte[] sendbytes = Encoding.Default.GetBytes(sendJSON); | |
//送信 | |
var www = new WWW("http://0.0.0.0/getPlayer", sendbytes, header); | |
//受信 | |
yield return www; //受信待機 | |
if (www.error == null) | |
{ | |
print(www.text); | |
if (www.text != "-1" || www.text != "-2" || www.text != "-3") | |
{ | |
//結果を格納 foreach使ってるけど一人分しか返ってこないからね・・・ | |
IList playerList = (IList)Json.Deserialize(www.text); | |
foreach (IDictionary player in playerList) | |
{ | |
string temp_text = string.Copy(descriptions[1]); //テンプレートを作成 1はステータス画面のmodeid | |
string region = ""; | |
string element=""; | |
switch ((long)player["region"]) | |
{ | |
case 1: | |
region = "ALEK"; | |
break; | |
case 2: | |
region = "Anti ALEK"; | |
break; | |
} | |
switch ((long)player["element"]) | |
{ | |
case 1: | |
element = "ほのお"; | |
break; | |
case 2: | |
element = "みず"; | |
break; | |
case 3: | |
element = "でんき"; | |
break; | |
case 4: | |
element = "くさ"; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
print("function error"); | |
} | |
} | |
else | |
{ | |
print("access error"); | |
} | |
} | |
//bytesデータをサーバーに送信する(画像を送信するときに使用) | |
private IEnumerator sendBytesToServer(byte[] send_bytes) | |
{ | |
var form = new WWWForm(); | |
form.AddBinaryData("fileUpload", send_bytes, "receive.png", "image/png"); | |
form.AddField("playerid", PlayerPrefs.GetInt("playerid").ToString()); | |
var www = new WWW(URL, form); | |
//受信 | |
yield return www; //受信待機 | |
if (www.error == null) | |
{ | |
print(www.text); | |
if (www.text != "-1") | |
{ | |
print("string : " + www.text); | |
resultItemid = int.Parse(www.text); | |
} | |
} | |
else | |
{ | |
errorText.GetComponent<Text>().text = "サーバーエラー"; | |
nextTime = Time.time + interval; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment