static Scene GetIsolationScene() { EditInIsolation edit = null; Scene result = default(Scene); // find the isolation scene for (int i = 0; i < EditorSceneManager.sceneCount; i++) { result = EditorSceneManager.GetSceneAt(i); GameObject[] roots = result.GetRootGameObjects(); edit = GetFrom(roots); if (edit != null) { break; } } // no isolation scene found, go ahead and make one if (edit == null) { Scene oldScene = EditorSceneManager.GetActiveScene(); Scene scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Additive); EditorSceneManager.SetActiveScene(scene); GameObject go = new GameObject("_EditInIsolationScene"); go.AddComponent <EditInIsolation>(); EditorSceneManager.SetActiveScene(oldScene); result = scene; } return(result); }
static void HandleChange(PlayModeStateChange aChange) { if (aChange == PlayModeStateChange.ExitingEditMode) { EditInIsolation.DisableObjects(); } if (aChange == PlayModeStateChange.EnteredEditMode) { EditInIsolation.EnableObjects(); } }
static EditInIsolation GetFrom(GameObject[] objs) { EditInIsolation result = null; for (int i = 0; i < objs.Length; i++) { result = objs[i].GetComponent <EditInIsolation>(); if (result != null) { break; } } return(result); }
static void HandleChange() { // Disable objects -before- we start playing. Don't want Awake events. if (!wasPlayingPre && EditorApplication.isPlayingOrWillChangePlaymode) { EditInIsolation.DisableObjects(); } // Enable objects -after- playing has ended. Don't want Unity to restore original disabled state afterwards if (wasPlayingPost && !EditorApplication.isPlaying) { EditInIsolation.EnableObjects(); } wasPlayingPre = EditorApplication.isPlayingOrWillChangePlaymode; wasPlayingPost = EditorApplication.isPlaying; }
static EditInIsolation HasIsolationScene() { EditInIsolation edit = null; // find the isolation scene for (int i = 0; i < EditorSceneManager.sceneCount; i++) { Scene scene = EditorSceneManager.GetSceneAt(i); GameObject[] roots = scene.GetRootGameObjects(); edit = GetFrom(roots); if (edit != null) { break; } } return(edit); }
public static void EnableObjects() { EditInIsolation edit = HasIsolationScene(); if (edit == null) { return; } GameObject[] objs = GetIsolationScene().GetRootGameObjects(); // restore previous state for (int i = 0; i < objs.Length; i++) { objs[i].SetActive(edit._wasEnabled[i]); } }
public static void DisableObjects() { EditInIsolation edit = HasIsolationScene(); if (edit == null) { return; } GameObject[] objs = GetIsolationScene().GetRootGameObjects(); edit._wasEnabled = new bool[objs.Length]; // disable all objects, but track their previous states in an object that will serialize/maintain the data for (int i = 0; i < objs.Length; i++) { edit._wasEnabled[i] = objs[i].activeSelf; objs[i].SetActive(false); } }