Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by shakecaine · Nov 24, 2017 at 01:49 PM · unityeditorplaymodequit

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Harinezumi · Oct 22, 2018 at 03:17 PM 0
Share

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?

avatar image shakecaine Harinezumi · Oct 22, 2018 at 03:32 PM 0
Share

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.

avatar image Harinezumi shakecaine · Oct 22, 2018 at 08:53 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image patrik-org · Apr 19, 2020 at 06:45 PM 0
Share

Thank you, works for me.

avatar image RemiCarreira · May 16, 2020 at 01:10 PM 1
Share

⚠️ 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.

avatar image Harinezumi RemiCarreira · Jun 01, 2020 at 03:48 PM 0
Share

Ah, you are right, and now it makes sense why it was never called. Thanks!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

121 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Weird error appearing after playing in editor? 0 Answers

Trying to set scene object variables in MenuItem editor script and start playing 0 Answers

Unity Editor got hang after clicking play 1 Answer

Extiting Play Mode Takes Too Much Time 1 Answer

Problem with Application.Quit 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges