Start game with editor scripts? Get scene camera while playing?

Hello! I have a level editor that runs inside the Unity editor. I would love to give the level designers the ability to press a button and play what they are looking at.

Basically, I want to know the initial center of the scene view when the game is running. We use an orthographic camera. I plan on using Camera.ViewportToWorldPoint().

Option A: Apparently I can get the scene camera in editor scripts through Camera.current, but I don’t know how to start the game from editor scripts.

Option B: I can start the game then get the scene camera to find out what should load first, but I don’t know how to get the scene camera once the game is playing.

So anyone know how I can start game with editor scripts or get the scene camera while playing?

If you are using an editor window, you can do these things:

    void OnSceneGUI ( SceneView sceneView ) {
        Handles.BeginGUI();
        if ( GUILayout.Button("Click" ) ) {
            EditorApplication.isPlaying = true;
            Debug.Log( sceneView.pivot );
            Debug.Log( sceneView.camera.ViewportToWorldPoint(Vector3.zero) );
        }
        Handles.EndGUI();
    }
1 Like

I didn’t know EditorApplication.isPlaying has a setter, I just assumed it was read-only so thanks for that!

As for void OnSceneGUI(SceneView), my console gets spammed with errors when I try to do that. I’m going to try using Camera.current next and see if that gives me the scene camera or not.

Camera.current works for the scene view camera in OnSceneGUI() but not in OnInspectorGUI(). Which makes sense. I’m able to get a position for the center of the scene view and run the game now. Thanks @hpjohn for your help!

Sceneview sceneview ?

Ah yeah I had that, still getting those errors. They aren’t compiler errors. They don’t appear until I saved my editor script then looked at the console. The error seems to indicate 1 parameter is the wrong number of parameter’s it’s looking for. I’m wondering maybe it’s a Unity version specific? I couldn’t find any documentation on the SceneView class either. I’m on 4.5.4 right now.

Working fine in 4.5.4f1

Sceneview is an undocumented ‘feature’ , though I don’t see why, it’s pretty useful.

public class SceneGUI : EditorWindow {
    [MenuItem( "EDITORS/SceneGUI" )]
    static void Init () {
        SceneGUI window = (SceneGUI) EditorWindow.GetWindow( typeof( SceneGUI ) );
        window.Show();
        window.position = new Rect( 20, 80, 300, 400 );
    }

    void OnFocus () {
        SceneView.onSceneGUIDelegate -= this.OnSceneGUI;
        SceneView.onSceneGUIDelegate += this.OnSceneGUI;
    }

    void OnDestroy () {
        SceneView.onSceneGUIDelegate -= this.OnSceneGUI;
    }

    string clickedView;

    void OnGUI () {
        GUILayout.Label( "Clicked on " + clickedView );
    }

    void OnSceneGUI ( SceneView sceneView ) {
        Handles.BeginGUI();
        if ( GUILayout.Button( "Click" ) ) {
            clickedView = "" + ( sceneView.orthographic ? "Orhtographic " : "Perspective " ) + sceneView.renderMode;
            this.Repaint();
        }
        Handles.EndGUI();
    }
}