我是新手解析并且想知道是否有一种内置的方式等待任务完成然后继续下一个语句。我已经通过解决方法完成了这项工作,但我希望能够正确完成。任何帮助将不胜感激。这是我的代码。
public IEnumerator CreateNewPet(int type)
{
bool canContinue = false; /*this is the boolean that stops the script from moving forward until parse has finished */
gcMenuController.LoadingOn();
Texture2D photo = GameObject.Find("PetPicture").GetComponent<MeshRenderer>().material.mainTexture as Texture2D;
imageData = photo.EncodeToPNG();
byte[] data = imageData;
ParseObject petInfo = new ParseObject("Pet");
petInfo["user"] = ParseUser.CurrentUser;
petInfo["petType"] = type;
petInfo["petName"] = gcMenuController.createName;
petInfo["dateOfBirth"] = DateTime.Parse(gcMenuController.createBirthday);
petInfo["petBio"] = gcMenuController.createBio;
Task savePetTask = petInfo.SaveAsync().ContinueWith(t => {
petID2 = petInfo.ObjectId;
canContinue = true;
});
while (!canContinue)
{ /*This while loop waits until the canContinue bool has been set to true before moving forward */
yield return null;
}
ParseFile file = new ParseFile(gcMenuController.createName + ".png", data);
Task saveTask = file.SaveAsync();
ParseObject point = ParseObject.CreateWithoutData("Pet", petID2);
var picture = new ParseObject("Photo");
picture["imageFile"] = file;
picture["user"] = ParseUser.CurrentUser;
picture["pet"] = point;
Task saveTask2 = picture.SaveAsync();
print("ParseController.AddPetPhoto() - Photo uploaded under petID2 " + petID2);
gcMenuController.StartCoroutine("LoadingOff");
yield return null;
}