Z
Z
Zimaell2020-07-06 17:57:11
Unity
Zimaell, 2020-07-06 17:57:11

How to get the response from the server into a method?

I wrote such a simple authorizer, here is the method itself hung on the button

spoiler
public class Authorization : MonoBehaviour{
    private string login;
    private string password;
    public string answer;
    public void ClickAuthorization(){
...........
        Dictionary<string,string> a = new Dictionary<string,string>();
        a.Add("login",login);
        a.Add("password",password);
        Server.Inst.SendData(a);
        //answer = Server.Inst.SendData(a);
        //Debug.Log(answer);
        }
    }

here is the script itself that sends/receives data
spoiler
public class Server: MonoBehaviour{
    public static string answer;
    public static Server Inst;
    private void Awake(){ Inst = this; }
    public void SendData(Dictionary<string,string> a){
        StartCoroutine(SendDataWWW(a));
        }
    IEnumerator SendDataWWW(Dictionary<string,string> a){
        WWWForm form = new WWWForm();
        foreach(KeyValuePair<string,string> v in a){
            form.AddField(v.Key,v.Value);
            }
        UnityWebRequest www = UnityWebRequest.Post("https://..............", form);
        yield return www.SendWebRequest();
        if (www.isNetworkError || www.isHttpError){
            answer="error";
            }else{
                answer=www.downloadHandler.text;
                }
        Debug.Log("answer server => "+answer);
        }
    }

Everything works, the data is sent to the server and the server sends a response to Debug.Log("answer server => "+answer); the correct answer comes.
The problem is that I need to get this answer in the ClickAuthorization() method itself, there I commented out how to do this, I still don’t understand ...
Tell me how can I modify my script to get the data there?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question