Unity Network Http Get 사용하기
안녕하세요. 최근 Unity 공부를 하고 있어 Android 가 아닌 Unity 포스트를 써보려 합니다. ^^
이번에 FlappyBird 를 개발해 보았습니다 .

제가 만들고 있는 FlappyBird 입니다 .
FlappyBird 를 만들면서 Unity Network http get 에 관련해서 사용해 보았습니다 .
Server 는 Java Spring 을 통해서 Test 서버를 구축하였습니다 .
Unity 에서 서버를 콜하기 위해사용한 코드 입니다 .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
using UnityEngine;
using System.Collections;
public class EndScreenScript : MonoBehaviour {
WWWWiki wwwwiki;
BirdMovement bird;
static bool isGame = true;
static int score;
private static string name = "dlsdnd345";
void Start () {
wwwwiki = gameObject.AddComponent<WWWWiki>();
if(isGame){
GetComponent<SpriteRenderer>().enabled = false;
}
}
void GameOver(){
savePlayer ();
selectRank ();
bird = GameObject.Find("PlayerBird").GetComponent<BirdMovement>();
if(bird.dead){
GetComponent<SpriteRenderer>().enabled = true;
}
}
void savePlayer(){
score = Score.getPoint();
saveUri = saveUri + "?name="+name+"&score="+score;
Debug.Log (wwwwiki.GET (saveUri));
}
void selectRank(){
selectRankUri = selectRankUri + "?name=" + name;
Debug.Log (wwwwiki.GET (selectRankUri));
}
}
|
여기서 핵심 코드는 하단의 코드 입니다 .
1
2
3
4
5
|
void Start () {
wwwwiki = gameObject.AddComponent<WWWWiki>();
}
|
Start 메서드에서 생성자 초기화를 진행합니다 ,
1
2
3
4
5
6
7
8
9
10
|
void savePlayer(){
score = Score.getPoint();
saveUri = saveUri + "?name="+name+"&score="+score;
Debug.Log (wwwwiki.GET (saveUri));
}
void selectRank(){
selectRankUri = selectRankUri + "?name=" + name;
Debug.Log (wwwwiki.GET (selectRankUri));
}
|
savePlayer 는 사용자가 Play 한 FlappyBird 의 점수를 저장하기 위한 메서드 입니다 .
selectRank 는 사용자의 점수 결과를 통해서 Rank 를 보여주는 메서드 입니다 .
메서드의 내부는 변수의 값을 통해서 Uri 를 구성하고 Get 호출을 불러오는 방식입니다 .
이와 같이하면 json Type 의 리턴값을 확인할수 있습니다 .

다음 시간에 Unity Json 파싱에 관련해서 포스트를 작성해 보도록 하겠습니다 .