게임 엔진/Unity
Unity 라이브러리 추천 - RestClient
데브준우
2024. 3. 24. 02:19
RestClient
https://github.com/proyecto26/RestClient
GitHub - proyecto26/RestClient: 🦄 A Promise based REST and HTTP client for Unity 🎮
🦄 A Promise based REST and HTTP client for Unity 🎮 - proyecto26/RestClient
github.com
웹서버와 통신을 할때 조금 더 깔끔하고 편하게 코드를 짤 수있게 도와주는 라이브러리다.
코드를 조금 더 단촐하게 바꿀 수 있으며,
//그냥 사용할때
IEnumerator Start()
{
UnityWebRequest webRequest = UnityWebRequest.Get(url);
yield return webRequest.SendWebRequest();
if (webRequest.result == UnityWebRequest.Result.Success)
{
var data = JsonUtility.FromJson<User>(webRequest.downloadHandler.text);
Debug.Log(data.NickName);
}
else
{
Debug.Log("통신 실패!");
}
}
//라이브러리 사용
public void Request()
{
RestClient.Get(url).Then(res =>
{
var data = JsonUtility.FromJson<User>(res.Text);
Debug.Log(data.NickName);
}).Catch(ex =>
{
Debug.Log($"에러! {ex.Message}");
});
}
//라이브러리 사용
public void Request()
{
RestClient.Get<User>(url).Then(res =>
{
Debug.Log(res.NickName);
}).Catch(ex =>
{
Debug.Log($"에러! {ex.Message}");
});
}
이런식으로 제네릭을 이용하면 res에 바로 해당 클래스가 역직렬화가 되어서 돌아온다.
//라이브러리 사용
public void Request()
{
RestClient.Get<User>(url).Then(res =>
{
Debug.Log(res.NickName);
return RestClient.Get<User2>(url);
}).Then(res =>
{
Debug.Log(res.Inventory[0]);
}).Catch(ex =>
{
Debug.Log($"에러! {ex.Message}");
});
}
이런식으로 다른 요청을 return 해주어서 Then 함수를 통해 이어받아 콜백을 실행하는것도 가능하다.
Get,Post,Put,Delete,Head 를 지원한다.