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.
public class MyGame : MonoBehaviour
{
public void GameLogic()
{
MyApplication.QuitWithSceneFileUpload();
}
}
public class MyApplication : Application
{
public static void QuitWithSceneFileUpload()
{
Quiter quiter = new GameObject().AddComponent<Quiter>();
// Or if you need editor customization,
// use a static reference to a prefab with Quiter on it
// via a Singleton or ServiceLocator. Something like:
// Quiter quiter = Instantiate(ServiceLocator.QuiterPrefab);
quiter.Quit();
}
}
public class Quiter : MonoBehaviour
{
private bool isSceneFileUploadSuccessful;
public void Quit()
{
StartCoroutine(UploadSceneFile());
}
private IEnumerator UploadSceneFile()
{
XmlDocument sceneXML = new XmlDocument();
sceneXML.Load(Application.persistentDataPath + "/scene.xml");
byte[] sceneData = Encoding.UTF8.GetBytes(sceneXML.InnerXml);
WWWForm form = new WWWForm();
form.AddBinaryData("xmlFile", sceneData, "scene.xml", "text/xml");
UnityWebRequest www = UnityWebRequest.Post("http://localhost.pl/unity/upload.php", form);
www.Send();
yield return www;
if (www.isError)
{
isSceneFileUploadSuccessful = false;
}
else
{
isSceneFileUploadSuccessful = true;
}
OnCoroutineEnd();
}
// This helps you to run multiple coroutines on quit and quit when all are successful.
private void OnCoroutineEnd()
{
if (!isSceneFileUploadSuccessful)
{
// Perhaps publish an event here so that others know quit is cancelled.
// Destroy itself or you will have empty quiter game objects for each unsuccessful attempt.
Destroy(gameObject);
return;
}
Application.Quit();
}
}