Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Coroutine in OnApplicationQuit

Discussion in 'Scripting' started by DarHor, Jul 15, 2017.

  1. DarHor

    DarHor

    Joined:
    Apr 14, 2014
    Posts:
    4
    I must send scene file on server using UnityWebRequest and i must do it when user closing game. Code looks like this:
    Code (CSharp):
    1. public bool allowQuitting = false;
    2.  
    3. void OnApplicationQuit() {
    4.         XML.Write ();
    5.         StartCoroutine("UploadSceneFile");
    6.  
    7.         if (!allowQuitting)
    8.             Application.CancelQuit();
    9.  
    10.     }
    11.  
    12. IEnumerator UploadSceneFile() {
    13.         Debug.Log ("Try to upload xml file");
    14.  
    15.         XmlDocument sceneXML = new XmlDocument ();
    16.         sceneXML.Load (Application.persistentDataPath + "/scene.xml");
    17.  
    18.         byte[] sceneData = Encoding.UTF8.GetBytes(sceneXML.InnerXml);
    19.  
    20.         WWWForm form = new WWWForm();
    21.         form.AddBinaryData("xmlFile", sceneData,"scene.xml","text/xml");
    22.  
    23.         UnityWebRequest www = UnityWebRequest.Post("http://localhost.pl/unity/upload.php", form);
    24.         www.Send ();
    25.  
    26.         yield return www;
    27.  
    28.         allowQuitting = true;
    29.         Debug.Log ("Upload");
    30.         Application.Quit();
    31.  
    32.     }
    But game is closed before completion upload file. I would wait for upload file
     
  2. charmcrescini

    charmcrescini

    Joined:
    Sep 9, 2019
    Posts:
    18
    Were you able to find a workaround?
     
  3. guneyozsan

    guneyozsan

    Joined:
    Feb 1, 2012
    Posts:
    99
    You can write a wrapper Quit() class of your own and call your own Quit() method. Example below.

    An improved approach can be to decouple upload and quit control functionality as Quiter and Uploader components communicating via events.

    Code (CSharp):
    1.  
    2. public class MyGame : MonoBehaviour
    3. {
    4.     public void GameLogic()
    5.     {
    6.         MyApplication.QuitWithSceneFileUpload();
    7.     }
    8. }
    9.  
    10. public class MyApplication : Application
    11. {
    12.     public static void QuitWithSceneFileUpload()
    13.     {
    14.         Quiter quiter = new GameObject().AddComponent<Quiter>();
    15.         // Or if you need editor customization,
    16.         // use a static reference to a prefab with Quiter on it
    17.         // via a Singleton or ServiceLocator. Something like:
    18.         // Quiter quiter = Instantiate(ServiceLocator.QuiterPrefab);
    19.  
    20.         quiter.Quit();
    21.     }
    22. }
    23.  
    24. public class Quiter : MonoBehaviour
    25. {
    26.     private bool isSceneFileUploadSuccessful;
    27.  
    28.     public void Quit()
    29.     {
    30.         StartCoroutine(UploadSceneFile());
    31.     }
    32.  
    33.     private IEnumerator UploadSceneFile()
    34.     {
    35.         XmlDocument sceneXML = new XmlDocument();
    36.         sceneXML.Load(Application.persistentDataPath + "/scene.xml");
    37.  
    38.         byte[] sceneData = Encoding.UTF8.GetBytes(sceneXML.InnerXml);
    39.  
    40.         WWWForm form = new WWWForm();
    41.         form.AddBinaryData("xmlFile", sceneData, "scene.xml", "text/xml");
    42.  
    43.         UnityWebRequest www = UnityWebRequest.Post("http://localhost.pl/unity/upload.php", form);
    44.         www.Send();
    45.  
    46.         yield return www;
    47.  
    48.         if (www.isError)
    49.         {
    50.             isSceneFileUploadSuccessful = false;
    51.         }
    52.         else
    53.         {
    54.             isSceneFileUploadSuccessful = true;
    55.         }
    56.  
    57.         OnCoroutineEnd();
    58.     }
    59.  
    60.     // This helps you to run multiple coroutines on quit and quit when all are successful.
    61.     private void OnCoroutineEnd()
    62.     {
    63.         if (!isSceneFileUploadSuccessful)
    64.         {
    65.             // Perhaps publish an event here so that others know quit is cancelled.
    66.                        
    67.             // Destroy itself or you will have empty quiter game objects for each unsuccessful attempt.
    68.             Destroy(gameObject);
    69.             return;
    70.         }
    71.  
    72.         Application.Quit();
    73.     }
    74. }