Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
5
Question by puargs · Apr 30, 2013 at 03:31 PM · eventpauseeventsstop

Event for Unity Editor "Pause" and "Play/Stop" events?

I am using my own custom external C# DLL for music, music loading, etc. Because I'm using an external DLL, I need to be able to inform my external threads when to start/stop. I can do this fine from within my game if it's running start to finish with no interruption, but when I'm actually editing and debugging my game, I need to be able to know when the "Pause" or "Play/Stop" buttons have been hit inside the Unity IDE. This will allow me to clean up my external threads so I don't have a bunch of mystery threads running when the program isn't actually running inside the IDE.

Is there any event to handle to do this?

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

Answer by iwaldrop · Apr 30, 2013 at 05:32 PM

I like to create a delegate method and assign it to handle play-mode changes, like so:

 EditorApplication.playmodeStateChanged = HandleOnPlayModeChanged;

 void HandleOnPlayModeChanged()
 {
     // This method is run whenever the playmode state is changed.

     if (EditorApplication.isPaused)
     {
         // do stuff when the editor is paused.
     }
 }

Flags that you can check:

  • isPlaying

  • isPlayingOrWillChangePlaymode

  • isPaused

  • isCompiling

  • isUpdating

EditorApplication reference.

Comment
Add comment · Show 1 · 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 puargs · Apr 30, 2013 at 06:50 PM 0
Share

This is exactly what I was looking for! Thank you :D

For reference, here's what I had to wind up doing to detect "stopped" state (since there is not a "isStopped" property):

 void HandleOnPlay$$anonymous$$odeChanged()
     {
         // This method is run whenever the playmode state is changed.
          
         if (EditorApplication.isPaused)
         {
             if (!wasPaused)
             {
                 Pause();
                 wasPaused = true;
             }
             else //.isStopped equivalent
             {
                 Stop ();
                 initLoadComplete = false;
             }
         }
         else if (initLoadComplete && (EditorApplication.isPlaying || EditorApplication.isPlayingOrWillChangePlaymode))
         {
             if (wasPaused)
             {
                 wasPaused = false;
                 Play ();
             }
             else //.isStopped equivalent
             {
                 Stop ();
                 initLoadComplete = false;
             }
         }
     }

Edit: Forgot a condition for Paused -> Stop. Above code should work properly now.

avatar image
4

Answer by stephenlautier · Feb 21, 2014 at 02:04 AM

Unity API for playmodeChanged state is a bit arghh..

I created the following to make it easier, usage would be something like this:

 [InitializeOnLoad]
 public class SingleEntryPoint
 {
     static SingleEntryPoint()
     {
         Debug.Log("SingleEntryPoint. Up and running");
         EditorPlayMode.PlayModeChanged += OnPlayModeChanged;
     }
     
     private static void OnPlayModeChanged(PlayModeState currentMode, PlayModeState changedMode)
     {
           // DO your stuff here...
         }
 }

Playmode API source below.

 public enum PlayModeState
 {
     Stopped,
     Playing,
     Paused
 }
 
 [InitializeOnLoad]
 public class EditorPlayMode
 {
     private static PlayModeState _currentState = PlayModeState.Stopped;
 
     static EditorPlayMode()
     {
         EditorApplication.playmodeStateChanged = OnUnityPlayModeChanged;
     }
 
     public static event Action<PlayModeState, PlayModeState> PlayModeChanged;
 
     public static void Play()
     {
         EditorApplication.isPlaying = true;
     }
 
     public static void Pause()
     {
         EditorApplication.isPaused = true;
     }
 
     public static void Stop()
     {
         EditorApplication.isPlaying = false;
     }
 
 
     private static void OnPlayModeChanged(PlayModeState currentState, PlayModeState changedState)
     {
         if (PlayModeChanged != null)
             PlayModeChanged(currentState, changedState);
     }
 
     private static void OnUnityPlayModeChanged()
     {
         var changedState = PlayModeState.Stopped;
         switch (_currentState)
         {
             case PlayModeState.Stopped:
                 if (EditorApplication.isPlayingOrWillChangePlaymode)
                 {
                     changedState = PlayModeState.Playing;
                 }
                 break;
             case PlayModeState.Playing:
                 if (EditorApplication.isPaused)
                 {
                     changedState = PlayModeState.Paused;
                 }
                 else
                 {
                     changedState = PlayModeState.Stopped;
                 }
                 break;
             case PlayModeState.Paused:
                 if (EditorApplication.isPlayingOrWillChangePlaymode)
                 {
                     changedState = PlayModeState.Playing;
                 }
                 else
                 {
                     changedState = PlayModeState.Stopped;
                 }
                 break;
             default:
                 throw new ArgumentOutOfRangeException();
         }
 
         // Fire PlayModeChanged event.
         OnPlayModeChanged(_currentState, changedState);
 
         // Set current state.
         _currentState = changedState;
     }
 
 }

hope it helps, and feel free to use. :)

Comment
Add comment · Show 2 · 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 paraself · Jul 11, 2015 at 11:05 AM 0
Share

Thanks for sharing. I tested. However events are fired twice when exiting from play mode with changeState = stopped. I made a few modifications to your code, and now it works fine.

 using UnityEditor;
 using System;
 
 public enum Play$$anonymous$$odeState
 {
     Stopped,
     Playing,
     Paused
 }
 
 [InitializeOnLoad]
 public class EditorPlay$$anonymous$$ode
 {
     private static Play$$anonymous$$odeState _currentState = Play$$anonymous$$odeState.Stopped;
     
     static EditorPlay$$anonymous$$ode()
     {
         EditorApplication.playmodeStateChanged = OnUnityPlay$$anonymous$$odeChanged;
         if (EditorApplication.isPaused) 
         _currentState = Play$$anonymous$$odeState.Paused;
     }
     
     public static event Action<Play$$anonymous$$odeState, Play$$anonymous$$odeState> Play$$anonymous$$odeChanged;
     
     public static void Play()
     {
         EditorApplication.isPlaying = true;
     }
     
     public static void Pause()
     {
         EditorApplication.isPaused = true;
     }
     
     public static void Stop()
     {
         EditorApplication.isPlaying = false;
     }
     
     
     private static void OnPlay$$anonymous$$odeChanged(Play$$anonymous$$odeState currentState, Play$$anonymous$$odeState changedState)
     {
         if (Play$$anonymous$$odeChanged != null)
             Play$$anonymous$$odeChanged(currentState, changedState);
     }
     
     private static void OnUnityPlay$$anonymous$$odeChanged()
     {
         var changedState = Play$$anonymous$$odeState.Stopped;
         switch (_currentState)
         {
         case Play$$anonymous$$odeState.Stopped:
             if (EditorApplication.isPlayingOrWillChangePlaymode)
             {
                 changedState = Play$$anonymous$$odeState.Playing;
             }
             else if (EditorApplication.isPaused)
             {
                 changedState = Play$$anonymous$$odeState.Paused;
             }
             break;
         case Play$$anonymous$$odeState.Playing:
             if (EditorApplication.isPaused)
             {
                 changedState = Play$$anonymous$$odeState.Paused;
             }
             else if (EditorApplication.isPlaying)
             {
                 changedState = Play$$anonymous$$odeState.Playing;
             }
             else 
             {
                 changedState = Play$$anonymous$$odeState.Stopped;
             }
             break;
         case Play$$anonymous$$odeState.Paused:
             if (EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPaused)
             {
                 changedState = Play$$anonymous$$odeState.Playing;
             }
             else if (EditorApplication.isPlayingOrWillChangePlaymode && EditorApplication.isPaused)
             {
                 changedState = Play$$anonymous$$odeState.Paused;
             }
             break;
         default:
             throw new ArgumentOutOfRangeException();
         }
         
         // Fire Play$$anonymous$$odeChanged event.
         if (_currentState!=changedState)
             OnPlay$$anonymous$$odeChanged(_currentState, changedState);
         
         // Set current state.
         _currentState = changedState;
     }
     
 }
 
avatar image paraself · Jul 15, 2015 at 03:07 PM 1
Share

Simplified further, add support for AboutToStop event, so it's easier to hook up before the unity editor quit runtime. https://gist.github.com/paraself/0d75962a2c509759a6c9

avatar image
0

Answer by the_9an · Jun 23, 2021 at 01:57 PM

With Pause Event, You must use EditorApplication.pauseStateChanged

Comment
Add comment · 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
0

Answer by Proliecan · Jun 23, 2021 at 09:19 AM

This should work just fine:

  private void OnApplicationPause(bool pause)
  {
                 
  }

Comment
Add comment · 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

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

15 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

Related Questions

When to use 'delegate', 'event' or 'Action' ? 1 Answer

How can I detect Keyboard Events? 4 Answers

How to pause Time(Time.timeSinceLevelLoad) 1 Answer

Pause Menu 3 Answers

Unity Event Inspector Condition 0 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