Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Poor man's pause/quit

Discussion in 'Project Tiny' started by NiallT, Jan 14, 2019.

  1. NiallT

    NiallT

    Joined:
    Sep 22, 2018
    Posts:
    51
    So I was noodling around trying to get text input while the Tiny team's still working on getting the text input UI element built, and the simplest solution was just calling
    window.prompt("<string>")
    from TypeScript.

    But of course, JavaScript runs in a single thread in the browser, and calling a pop-up blocks the thread until the user clicks OK. Nothing moves on-screen, and even the timer stops counting

    Eureka!
    That means JavaScript popups can be used as a very quick and easy two-line pause function:
    Code (csharp):
    1. if (ut.Runtime.Input.getKeyDown(ut.Core2D.KeyCode.P))
    2.     alert ("Paused.\nPress OK to continue.");
    Place that line in absolutely any part of OnUpdate that is only run during gameplay and let the browser do the heavy lifting.

    Of course, if
    window.alert
    gives you a two-line pause, a three-line quit function can be built around
    window.confirm
    :
    Code (csharp):
    1. if (ut.Runtime.Input.getKeyDown(ut.Core2D.KeyCode.Q))
    2.     if (window.confirm("Really quit?"))
    3.         game.Manager.Quit();
    Obviously the third line here is just a stub for demonstration and depends on how your specific code handles the end-game state. For example, I first tried this out in Galaxy Raiders, where the most obvious way to end the game was with
    GameService.gameOver(this.world, context);



    So there you have it -- two-line pause and three-line quit-with-confirmation. I'm guessing quite a few of you have worked this out for yourselves, but hopefully one or two people will find this useful.

    It's not the prettiest of things and hardly production ready ("Don't let this page create more messages" means it's not a safe UI element), but right now I'm not planning on putting a lot of time into working out how best to do things in a beta product -- I see this as a handy placeholder that lets me start prototyping with the minimum of hassle and I can learn how to do things properly when the first stable version comes out.
     
    raymondyunity likes this.
  2. abeisgreat

    abeisgreat

    Joined:
    Nov 21, 2016
    Posts:
    44
    Haha that's clever. Thanks for sharing.