Last active
November 21, 2018 20:29
-
-
Save Seneral/388ab29807cecca9f2ab0df6ca657ee9 to your computer and use it in GitHub Desktop.
Extended editor callbacks for entering/leaving playmode and new scenes in Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if UNITY_EDITOR | |
using UnityEngine; | |
using UnityEditor; | |
#if UNITY_5_3_OR_NEWER || UNITY_5_3 | |
using UnityEngine.SceneManagement; | |
using UnityEditor.SceneManagement; | |
#endif | |
using System; | |
namespace NodeEditorFramework.Utilities | |
{ | |
[InitializeOnLoad] | |
public static class EditorLoadingControl | |
{ | |
#if UNITY_5_3_OR_NEWER || UNITY_5_3 | |
private static Scene loadedScene; | |
#else | |
private static string loadedScene; | |
#endif | |
private static bool serializationTest = false; | |
private static bool playmodeSwitchToEdit = false; | |
private static bool toggleLateEnteredPlaymode = false; | |
public static Action beforeEnteringPlayMode; | |
public static Action justEnteredPlayMode; | |
public static Action lateEnteredPlayMode; | |
public static Action beforeLeavingPlayMode; | |
public static Action justLeftPlayMode; | |
public static Action justOpenedNewScene; | |
static EditorLoadingControl () | |
{ | |
EditorApplication.playmodeStateChanged -= PlaymodeStateChanged; | |
EditorApplication.playmodeStateChanged += PlaymodeStateChanged; | |
EditorApplication.update -= Update; | |
EditorApplication.update += Update; | |
EditorApplication.hierarchyWindowChanged -= OnHierarchyChange; | |
EditorApplication.hierarchyWindowChanged += OnHierarchyChange; | |
// beforeEnteringPlayMode += () => Debug.Log ("beforeEnteringPlayMode"); | |
// justEnteredPlayMode += () => Debug.Log ("justEnteredPlayMode"); | |
// lateEnteredPlayMode += () => Debug.Log ("lateEnteredPlayMode"); | |
// beforeLeavingPlayMode += () => Debug.Log ("beforeLeavingPlayMode"); | |
// justLeftPlayMode += () => Debug.Log ("justLeftPlayMode"); | |
// justOpenedNewScene += () => Debug.Log ("justOpenedNewScene"); | |
} | |
private static void OnHierarchyChange () | |
{ // TODO: OnGUI might be called before this function and might cause problems. Find a better way to detect scene change! | |
#if UNITY_5_3_OR_NEWER || UNITY_5_3 | |
Scene currentScene = EditorSceneManager.GetActiveScene (); | |
#else | |
string currentScene = Application.loadedLevelName; | |
#endif | |
if (loadedScene != currentScene) | |
{ | |
if (justOpenedNewScene != null) | |
justOpenedNewScene.Invoke (); | |
loadedScene = currentScene; | |
} | |
} | |
// Handles just after switch (non-serialized values lost) | |
private static void Update () | |
{ | |
if (toggleLateEnteredPlaymode) | |
{ | |
toggleLateEnteredPlaymode = false; | |
if (lateEnteredPlayMode != null) | |
lateEnteredPlayMode.Invoke (); | |
} | |
serializationTest = true; | |
} | |
private static void PlaymodeStateChanged () | |
{ | |
//Debug.Log ("Playmode State Change! isPlaying: " + Application.isPlaying + "; Serialized: " + serializationTest); | |
if (!Application.isPlaying) | |
{ // Edit Mode | |
if (playmodeSwitchToEdit) | |
{ // After Playmode | |
//Debug.Log ("LOAD PLAY MODE Values in Edit Mode!!"); | |
if (justLeftPlayMode != null) | |
justLeftPlayMode.Invoke (); | |
playmodeSwitchToEdit = false; | |
} | |
else | |
{ // Before Playmode | |
//Debug.Log ("SAVE EDIT MODE Values before Play Mode!!"); | |
if (beforeEnteringPlayMode != null) | |
beforeEnteringPlayMode.Invoke (); | |
} | |
} | |
else | |
{ // Play Mode | |
if (serializationTest) | |
{ // Before Leaving Playmode | |
//Debug.Log ("SAVE PLAY MODE Values before Edit Mode!!"); | |
if (beforeLeavingPlayMode != null) | |
beforeLeavingPlayMode.Invoke (); | |
playmodeSwitchToEdit = true; | |
} | |
else | |
{ // After Entering Playmode | |
//Debug.Log ("LOAD EDIT MODE Values in Play Mode!!"); | |
if (justEnteredPlayMode != null) | |
justEnteredPlayMode.Invoke (); | |
toggleLateEnteredPlaymode = true; | |
} | |
} | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment