1
\$\begingroup\$

lay

Hello, I would like to get in a script the event whenever we click on the pause button, or the Jump frame button.

I have already manage the Pause button:

private void OnEnable()
{
    EditorApplication.playmodeStateChanged += HandleOnPlayModeChanged;
}

private void OnDisable()
{
    EditorApplication.playmodeStateChanged -= HandleOnPlayModeChanged;
}

private void HandleOnPlayModeChanged()
{
    if (EditorApplication.isPaused)
    {
        // do stuff when the editor is paused.
    }
    else
    {
        // do stuff when the editor is unpaused
    }
}

But How to get the "jump frame" button ? Thanks !

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Because you can determine when the pause button is pressed you can use this to then deduce when a "jump frame" would have occured.

Essentially, when its in pause, one "frame" would call the update funtion, Update() or FixedUpdate() once. * Fixed Update only will work to the framerate of the physics system (Default of 50fps)

bool isPaused = false;

//Code you have provided
private void OnEnable()
{
    EditorApplication.playmodeStateChanged += HandleOnPlayModeChanged;
}

private void OnDisable()
{
    EditorApplication.playmodeStateChanged -= HandleOnPlayModeChanged;
}

private void HandleOnPlayModeChanged()
{
    isPaused = EditorApplication.isPaused;
}

//Update function to check every frame
void Update()
{
    //If a frame has passed in pause then it can be assumed a frame skip has occured
    if (isPaused)
    {
        //Do something when frame skip has occured
        print("skipped a frame");
    }

}
\$\endgroup\$
8
  • 1
    \$\begingroup\$ You're not guaranteed that FixedUpdate will be called (or called only once). Can you show an example of how you"d use this fact about Update to create the kind of event handler OP is looking for? \$\endgroup\$
    – DMGregory
    Sep 25, 2019 at 11:10
  • \$\begingroup\$ @DMGregory I've tested the code above, and it seems to be called whenever I pressed the jump frame button. Unless I'm misunderstanding something? \$\endgroup\$
    – Vitulus
    Sep 25, 2019 at 11:20
  • 1
    \$\begingroup\$ The code above uses Update. My word of caution was about FixedUpdate. Good edit. :) \$\endgroup\$
    – DMGregory
    Sep 25, 2019 at 11:21
  • \$\begingroup\$ @DMGregory Ah I see, Fixed Update should technically work unless you hit the jump frame button more than the frame rate of the fixed update button (Default: 50fps) - which I assumed was unreasonable. I'll clarify this. \$\endgroup\$
    – Vitulus
    Sep 25, 2019 at 11:23
  • 1
    \$\begingroup\$ if (a) b = true; else b = false; can be simplified to b = a; \$\endgroup\$ Sep 25, 2019 at 11:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .