예제 #1
0
    private IEnumerator UpdateDB() //DB上のユーザー情報を更新する
    {
        //PUTリクエストを生成
        string          url     = $"http://127.0.0.1:8000/Unity/DB/{this.Header.ID}";
        UnityWebRequest request = new UnityWebRequest(url, "PUT");

        //jsonを生成
        Data_DB data = new Data_DB()
        {
            name  = this.Header.Name,
            block = this.Header.Block
        };

        //byteに変換
        byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(JsonUtility.ToJson(data));

        //ヘッダーにタイプを設定
        request.uploadHandler   = (UploadHandler) new UploadHandlerRaw(bodyRaw);
        request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
        request.SetRequestHeader("Content-Type", "application/json");

        //送信
        yield return(request.SendWebRequest());

        //エラー判定・確認
        if (request.isHttpError || request.isNetworkError)
        {
            Debug.Log(request.error);
        }
    }
예제 #2
0
    private IEnumerator LoadDB() //DBからユーザー情報を読み込む
    {
        //GETリクエストを生成
        string          url     = $"http://127.0.0.1:8000/Unity/DB/{this.Header.ID}";
        UnityWebRequest request = UnityWebRequest.Get(url);

        //サーバーに送信
        yield return(request.SendWebRequest());

        if (request.isHttpError || request.isNetworkError) //エラー確認
        {
            Debug.Log(request.error);
        }
        else
        {
            //受信したデータを、jsonとして扱う
            Data_DB jsonClass = JsonUtility.FromJson <Data_DB>(request.downloadHandler.text);

            //jsonを使用して更新
            { this.Header.Name  = jsonClass.name;
              this.Header.Block = jsonClass.block; }
        }
    }