using Newtonsoft.Json;
using LitJson;
using Newtonsoft.Json.Linq;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class APICall : MonoBehaviour
{
// ###############################################
// NAME : Simstealer
// MAIL : minsub4400@gmail.com
// ###############################################
// Start is called before the first frame update
void Start()
{
StartCoroutine(UnityWebRequestGet());
}
IEnumerator UnityWebRequestGet()
{
// URL, API KEY 변수
string apiKey = "API KEy";
string url = "URL";
// UnityWebRequest.Get으로 요청을 보냅니다.
UnityWebRequest www = UnityWebRequest.Get(url);
// 요청 시, API KEY가 필요하다면 아래와 같이 RequestHeader에 Key와 Value를 적어 줍니다.
www.SetRequestHeader("api-key", apiKey);
// 응답이 올때까지 대기합니다.
yield return www.SendWebRequest();
// 응답에 에러가 없으면
if (www.error == null)
{
// 응답으로 받은 결과를 디버그합니다.
Debug.Log(www.downloadHandler.text);
}
else
{
Debug.Log("ERROR");
}
// 파일을 유니티 현재 프로젝트 경로에 저장하기(윈도우)
string path = Application.dataPath + "/APIData.json";
// Path.Combine()은 path의 구분자가 정확하지 않은 경우(맥 OS 등)에 사용합니다.
// string path = Path.Combine(Application.dataPath, "/playerData.json");
// JsonUtility.ToJson(playerData, true); true는 Json파일을 보기 좋게 저장해줍니다.
// 직렬화가 된 데이터에만 사용이 가능합니다. (class 등)
//string jsonData = JsonUtility.ToJson("text", true);
// 직렬화가 되지 않은 string 등은 JObject를 사용합니다.
JObject json = JObject.Parse(www.downloadHandler.text);
// 파일로 저장
File.WriteAllText(path, json.ToString());
}
}
댓글 영역