用户在parse.com和unity3d中注册后如何加载级别?

时间:2015-06-04 16:09:56

标签: c# parse-platform unity3d coroutine

我需要在用户注册后加载下一级,但它无法正常工作。我一直得到“StartCoroutine_Auto只能从主线程调用”错误。当我尝试使用LoadLevel()方法而不是startcoroutine方法时,我也得到“只能从主线程调用LoadLevelAsync”错误。我究竟做错了什么?以及如何解决它(这个问题的示例解决方案将是超级的)。拜托,谢谢。

void Start () 
{
    detectDevice();

    if(ParseUser.CurrentUser == null)
    {
        hasError = false;
        SignInPanel.SetActive(true);
        errorMessage = "";
        HeaderText.text = "A warm welcome to you!!!\nWhat shall we call you ?";
        registrationSuccessful = false;
    }
    else if(ParseUser.CurrentUser != null)
    {
        Application.LoadLevel(nextLevel);
    }
}


// Update is called once per frame
void Update () 
{
    ParseUser currentUser = ParseUser.CurrentUser;
    Debug.Log (currentUser["deviceType"]);


    if(hasError)
    {
        HeaderText.text = errorMessage;
    }
    else if(!hasError)
    {
        HeaderText.text = "A warm welcome to you!!!\nWhat shall we call you ?";
    }
    else if(registrationSuccessful)
    {
        HeaderText.text =" success";
    }
}

public void registerNewParseUser ()
{
    int randomNumber = Random.Range(1, 1000);
    string myUsername = usernameField.text;
    string myPassword = myUsername + randomNumber;

    ParseUser newUser = new ParseUser();
    newUser.Username = myUsername;
    newUser.Password = myPassword;
    newUser["currentLevel"] = "1";
    newUser["RemainingLives"] = 5;
    newUser["deviceType"] = deviceTypeString;

    Debug.Log ("Registering User....");

    try
    {               
        newUser.SignUpAsync().ContinueWith(t =>
        {
            if (t.IsFaulted)
            {
                foreach(var e in t.Exception.InnerExceptions) 
                {
                    hasError = true;
                    ParseException parseException = (ParseException) e;
                    // string that displays the error to the user
                    errorMessage = "Error: "+parseException.Message;
                }
            }
            else
            {
                // Login was successful.
                Debug.Log("Registration was successful!");
                registrationSuccessful = true;
                StartCoroutine(WaitAndLoad());
            }
        });
    }
    catch (System.Exception error)
    {
        Debug.Log (error.Message);
    }
}


IEnumerator WaitAndLoad()
{
    registrationSuccessful = true;
    yield return new WaitForSeconds(2);
    LoadLevel();
}

void LoadLevel ()
{
    Application.LoadLevel(1);
}

}

1 个答案:

答案 0 :(得分:0)

我找到了解决自己问题的方法。我刚刚将Update()中的“else if(registrationSuccessful){....}语句更改为它自己独立的if(){....}语句。工作得很好。