If you need to know when the unity editor is changing play mode states you can hook into the EditorApplication.playmodeStateChanged callback.