简体   繁体   中英

Create account on Parse with Unity3D

I am trying to create a new account using the parse api with Unity and running into all kinds of trouble. All I would like to do is create a account on parse. Then on success load a scene.

This is the code used to create a new account.

bool success = true;
string error;
try
{
    Task signup = user.SignUpAsync().ContinueWith(t =>
    {
        if (t.IsFaulted || t.IsCanceled)
        {
            success = false;
        }
    });
}
catch (System.Exception e)
{
    error = "Failed to sign up Parse User. Reason: " + e.Message;
    success = false;
}

if(success)
    Application.LoadLevel("ExampleScene");

I wont post all the things I have tried because I have tried a lot of things.

The main problems that always occur:

  • The code is ASync so it does not wait for the account to be created at all.
  • If I use a IEnumerator to wait it fails as it cannot yield inside a try catch.
  • Even if I take the try catch out Unity complains that LoadLevel cannot be called from the main thread.
  • I tried passing a callback. To the ContinueWith call but that still complains about calling Load from the main thread.

So what it boils down to is that I cannot figure out how to wait for the task to finish without disabling all Unity functionality.

How am I supposed to go about creating a parse account and getting notified when it is finished and then call Unity functions again?

You can yield your Task like this for this case:

private IEnumerator SignUpHandler()
{
    bool success = true;
    string error;

    Task signup = user.SignUpAsync();//.ContinueWith(t =>
    while (!signup.IsCompleted) yield return null;
    if (signup.IsFaulted || signup.IsCanceled)
    {
        //Debug.Log("Error " + signup.Exception.Message);
        error = "Failed to sign up Parse User. Reason: " + signup.Exception.Message;
        success = false;
    }
    else
    {
        Debug.Log("Done");
        Application.LoadLevel("ExampleScene");
    }

}

and call your SignUpHandler from somewhere like this: StartCoroutine("SignUpHandler");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM