Created
December 20, 2016 13:20
-
-
Save tomsseisums/ba7d370cdd0a44ed17989d0236a3f0d5 to your computer and use it in GitHub Desktop.
SceneWasOpened callback for Unity3D Editor
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
| using System.Reflection; | |
| using UnityEditor.SceneManagement; | |
| using UnityEngine.Events; | |
| using UnityEngine.SceneManagement; | |
| public static class EditorExtensions | |
| { | |
| private static FieldInfo sceneWasOpenedCallbackField; | |
| private static FieldInfo SceneWasOpenedCallbackField | |
| { | |
| get | |
| { | |
| if (sceneWasOpenedCallbackField == null) | |
| { | |
| sceneWasOpenedCallbackField = typeof(EditorSceneManager).GetField("sceneWasOpened", BindingFlags.Static | BindingFlags.NonPublic); | |
| } | |
| return sceneWasOpenedCallbackField; | |
| } | |
| } | |
| public static event UnityAction<Scene, OpenSceneMode> SceneWasOpened | |
| { | |
| add | |
| { | |
| OnSceneWasOpened(value); | |
| } | |
| remove | |
| { | |
| OffSceneWasOpened(value); | |
| } | |
| } | |
| private static void OnSceneWasOpened(UnityAction<Scene, OpenSceneMode> callback) | |
| { | |
| UnityAction<Scene, OpenSceneMode> currentValue = (UnityAction<Scene, OpenSceneMode>)SceneWasOpenedCallbackField.GetValue(null); | |
| SceneWasOpenedCallbackField.SetValue(null, currentValue + callback); | |
| } | |
| private static void OffSceneWasOpened(UnityAction<Scene, OpenSceneMode> callback) | |
| { | |
| UnityAction<Scene, OpenSceneMode> currentValue = (UnityAction<Scene, OpenSceneMode>)SceneWasOpenedCallbackField.GetValue(null); | |
| SceneWasOpenedCallbackField.SetValue(null, currentValue - callback); | |
| } | |
| } |
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
| using UnityEditor; | |
| using UnityEditor.SceneManagement; | |
| using UnityEngine; | |
| using UnityEngine.SceneManagement; | |
| [CustomEditor(typeof(MyOwnEditableClass))] | |
| public class MyOwnEditableClassEditor : Editor | |
| { | |
| [InitializeOnLoadMethod] | |
| private static void LoadHook() | |
| { | |
| EditorExtensions.SceneWasOpened += SceneWasOpened; | |
| } | |
| private static void SceneWasOpened(Scene scene, OpenSceneMode mode) | |
| { | |
| Debug.LogFormat("'{0}' opened in '{1}' mode.", scene.path, mode); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment