puargs
1
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?
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.
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. 
This should work just fine:
private void OnApplicationPause(bool pause)
{
}