Detect when editor stops 'playing'

I have an editor script where I want to perform some function after the user finishes ‘playing’ the scene in the editor (using the play button). Is there some way I can have a script that will get a function call or something when the preview has finished playing?

I know I can get whether the editor is currently playing using EditorApplication.isPlaying but what I need to know is when it changes from true to false.

2 Answers

2

Implement the

OnApplicationQuit()

callback! It occurs immediately before the application closes, and works in both the editor and the standalone player. Just like collision callbacks and other such things, you can put whatever you need to do inside of it, and be sure that it will execute on the very last frame.

Is there any way to get a function call AFTER the application quit?

Well, the application quitting is kind of the end of the world. There isn't anything afterwards!

In the editor, I guess you could use EditorApplication.playmodeStateChanged- it's not exactly straightforward, but as far as I can tell, you can handle that. I think it is implemented a bit differently from a usual callback, in that you manually tell it what delegate to call on your scripts.

I thought maybe there was some special editor thing for detecting when the user was coming back from previewing the application. I can just poll the EditorApplication.isPlaying in a script that has the ExecuteInEditMode attribute, and use a bool to detect if it's the first time since the game was playing... But if there was something built in I'd rather use that.

if you make a function in your script called, say, 'OnEditorChangedPlayMode', and then set EditorApplication.playmodeStateChanged to OnEditorChangedPlayMode in your Awake function, as far as I can tell it should then call that function whenever the Editor swaps between one playmode and another. I don't really have any experience with this (other than general delegates work), but you should try it out since as far as I know it's the 'built in thing' you are looking for.