Cancel quit before stop in Editor Playmode
I have an important question. Is there a way to allow AsyncOperation activation scenes to be finished just before stopping playmode in Editor? I ask that in a sense that i have allowSceneActivation = false on these scenes that wait to be activated and each time somebody stops playmode then Unity crashes as scene is loading though it is not yet allowed to load. Interestingly if you change anything in whatever script running in the scene, AsyncOperation will finish and new scene will load itself ignoring allowSceneActivation.
I tried to use Application.CancelQuit and finish AsyncOperations and load empty scene to quit but that does not work in Editor or somehow is ignored. I tried loading other Scene right before quitting (OnApplicationQuit) but no success. So do you have any other ideas how i should approach it?
Our idea is that we have player in the level and we load new one in the background to not interrupt the experience but it screws us when we want to just stop testing game in the Editor.
Hey! I know that this is an old question, but I also want to know if there is a way to cancel stopping play mode in the Editor, just like Application.CancelQuit()
does (or, used to do: in Unity 2018 it is deprecated, and you have to register a delegate into Application.wantsToQuit
).
$$anonymous$$y reason is a bit simpler: I want to be able to test Application.CancelQuit()
from the Editor. Did you ever find a solution to this?
Ask new question. This is not the place to ask about this. Also you cannot test Application from Editor as it is only used when you create executable. You can emulate it by using this method here.
As the original question has not been answered, and my question is the same, just the goal is different, I think it is correct to comment on the question and ask if a solution was found. If I posted my question as an answer, that would indeed be wrong. But this way, if an answer is found, it is in one place, not in multiple answers.
Anyway, I obviously don't use functions of Application
from the Editor, I use EditorApplication
, and indeed there is an EditorApplication.wantToQuit
event, but it doesn't seem to work (it's never called). However, the link was useful to implement a hacky way, by calling EditorApplicaiton.isPlaying = true;
to "cancel" exiting play mode.
In fact, I will post an answer based on this.
Answer by Harinezumi · Oct 22, 2018 at 09:07 PM
As @shakecaine posted in a comment, you can register a function to the event EditorApplication.playModeStateChanged
, which needs the signature void Function (PlayModeStateChange)
. In case you have an operation going that you don't want to be interrupted by play mode change, you can set EditorApplication.isPlaying = true;
, which effectively cancels the change.
Of course, it would be nicer to use the dedicated event EditorApplication.wantsToQuit
(available since Unity 2017.4), but alas, in Unity 2018.2 I could not get it to work (it was never called). So until then, the following should work:
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public static class PlayStateNotifier {
// use this to control allowing exiting play mode
public static bool ShouldNotExit { get; set; }
private static PlayStateNotifier() {
EditorApplication.playModeStateChanged += ModeChanged;
}
private static void ModeChanged (PlayModeStateChange state) {
if (state == PlayModeStateChange.ExitingPlayMode && ShouldNotExit) {
EditorApplication.isPlaying = true; // just make sure that sometimes the condition is false, otherwise you have to kill the Editor process
}
}
}
#endif
⚠️ EditorApplication.wantsToQuit
is called when you quit/close Unity. This event is not called when you stop playing your project. The best way for this problem stay EditorApplication.play$$anonymous$$odeStateChanged
.
Ah, you are right, and now it makes sense why it was never called. Thanks!