Roy B.
Roy B.

Reputation: 31

Is there any way to switch to scene mode from game mode when pressing the play button?


So basically my question: Is there any way to switch from scene mode to gamemode script wise? Image of what I want to do but from script

enter image description here


I am basically asking because I want to take a screenshot of the assets I load into the scene.
My code:

using UnityEngine;
using UnityEngine.SceneManagement;

public class NewBehaviourScript : MonoBehaviour {

    // Use this for initialization
    void Start () {
        Debug.Log("HELLO");
        var rss = AssetBundle.LoadFromFile(@"//MyFileLocation");
        Debug.Log(SceneManager.GetActiveScene().name);
        foreach (var asset in rss.LoadAllAssets<GameObject>())//<Texture2D>())
        {
            GameObject obj = Instantiate(asset, transform);
        }
        Debug.Log(transform.position);
        Application.CaptureScreenshot(@"//MyScreenshotlocation");
        Debug.Log("captured");
    }

    // Update is called once per frame
    void Update () {

    }   
}

Any help is appreciated! Thank you!

Upvotes: 2

Views: 169

Answers (1)

Programmer
Programmer

Reputation: 125435

You can switch to any Window with the EditorWindow.FocusWindowIfItsOpen function. To switch to Scene view, pass SceneView to it. You can use EditorApplication.playModeStateChanged to determine when you enter play mode.

This is an Editor plugin and must be placed in a folder named "Editor". Create a script called SceneSwitcher and copy everything below inside it. It should automatically switch to Scene View when you click the play button.

using UnityEditor;

[InitializeOnLoadAttribute]
public static class SceneSwitcher
{
    static SceneSwitcher()
    {
        EditorApplication.playModeStateChanged += LogPlayModeState;
    }

    private static void LogPlayModeState(PlayModeStateChange state)
    {
        if (state == PlayModeStateChange.EnteredPlayMode)
            SwitchToSceneView();
    }

    static void SwitchToSceneView()
    {
        EditorWindow.FocusWindowIfItsOpen<SceneView>();

        /////OR
        //SceneView sceneView = EditorWindow.GetWindow<SceneView>(); ;
        //Type type = sceneView.GetType();
        //EditorWindow.FocusWindowIfItsOpen(type);
    }
}

Upvotes: 3

Related Questions