Exemple #1
0
    public void SubmitConfirm_OnClick()
    {
        // Deactive input field and buttons until server response
        GameObject Background_Canvas = GameObject.Find("Background_Canvas");

        Set_InteractableSubmitScore(false);
        Set_Error(false);

        // Send high score to server
        string Name       = Background_Canvas.transform.GetChild(14).GetComponent <InputField>().text;
        int    High_Score = Data_Manager.Get_HighScore();

        // Check string name is null
        if (string.IsNullOrEmpty(Name.Replace(" ", "")))
        {
            // Name error
            Button_OnClick.Set_InteractableSubmitScore(true);
            Button_OnClick.Set_Error(true, "enter your name");
        }
        else if (Name.IndexOf("'") >= 0 || Name.IndexOf("|") >= 0)
        {
            // Correct name error
            Button_OnClick.Set_InteractableSubmitScore(true);
            Button_OnClick.Set_Error(true, "enter correct name");
        }
        else
        {
            GameObject.Find("Game_Controller").GetComponent <Submit_Score>().Post_Score(Name, High_Score);
        }
    }
    IEnumerator Post(SubmitInfo Info)
    {
        SubmitCallBack Call_Back;

        // Set server url and data to send
        string Submit_Url = Url + "/YourWebPage";

        // Post request to server
        var Request = new UnityWebRequest(Submit_Url, "POST");

        byte[] Body_Raw = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(Info));
        Request.uploadHandler   = (UploadHandler) new UploadHandlerRaw(Body_Raw);
        Request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
        Request.SetRequestHeader("Content-Type", "application/json");
        yield return(Request.Send());

        if (Request.responseCode == 200)
        {
            string Content = Request.downloadHandler.text;
            Call_Back = JsonConvert.DeserializeObject <SubmitCallBack>(Content);

            if (Call_Back.Your_CallBack == "Insert or Update your information")
            {
                // Save informations
                File_Manager.Save_Info();

                // Deactive submit score menu and active ten highscore
                Button_OnClick.Set_SubmitScoreMenu(false);
                Button_OnClick.Set_TopTenMenu(true);

                StartCoroutine(TopTen_Records(Info));
            }
            else if (Call_Back.Your_CallBack == "Name is taken")
            {
                // Server error
                Button_OnClick.Set_InteractableSubmitScore(true);
                Button_OnClick.Set_Error(true, "this name is taken");
            }
            else
            {
                // Server error
                Button_OnClick.Set_InteractableSubmitScore(true);
                Button_OnClick.Set_Error(true, "server error");
            }
        }
        else
        {
            // Server error
            Button_OnClick.Set_InteractableSubmitScore(true);
            Button_OnClick.Set_Error(true, "server error");
        }
    }