Search Unity

Alternative to OnApplicationQuit for WebPlayer?

Discussion in 'Editor & General Support' started by Puritan, Mar 18, 2013.

  1. Puritan

    Puritan

    Joined:
    Sep 6, 2012
    Posts:
    3
    I am uploading statistics for my game with the WWW call to an PHP script that adds the data to my SQL database. Everything has been handled in the OnApplicationQuit so far and i just noticed that OnApplicationQuit is never called on the Webplayer build so i wonder if anyone has any clever way of doing this in another way?

    I have researched quite a bit and come upon something that seem to work but is in no way optimal.

    Doing an externaleval call like this

    Code (csharp):
    1.  
    2.  
    3. class MyScript extends MonoBehaviour
    4. {
    5.  
    6. function Awake() : void
    7. {
    8.         gameObject.name =
    9.         var sCommand : String = "";
    10.         sCommand += "function OnBeforeBrowserUnload()";
    11.         sCommand += "{";
    12.         sCommand += "   GetUnity().SendMessage('MyGameObject', 'MyFunction', 'Param');";
    13.         sCommand += "   return 'some text';";
    14.         sCommand += "}";
    15.         sCommand += "window.onbeforeunload = OnApplicationQuit;";
    16.         Application.ExternalEval(sCommand);
    17. }
    18.  
    19. function MyFunction(p_sMsg) : void
    20. {
    21.     Debug.Log(p_sMsg); // Outputs "some text"
    22. }
    23.  
    24. }
    25.  
    this one actually does work, it pops a "do you want to leave this window blablabla" to the user and at the same time sends a msg to unity and gives you the time to pick it up, start a coroutine and do your stuff hopefully.

    One is supposed to be able to fire window.onbeforeunload quietly by returning a void value but that also seem to remove that the webplayer actually gets the function call.

    Critical solution is to have the game upload the userstats on a timed interval but that does not make it certain some data will not be lost and i would be happier with a solution that did not send unneccesary querys to my SQL database.

    So has anyone found a trick around this without popping a popup msg in the face of the user when he wants to go away from your webpage?
     
  2. hyperhippo

    hyperhippo

    Joined:
    Jan 16, 2014
    Posts:
    37
    did you find this worked?