How to get access to the editor camera?

Hello,

I'd like to write an editor script that forces the scene view camera (in the editor) to follow an object as it moves while the game is playing (Like the player). How can I get access to the editor camera so that I can do this?

If you want a KISS* answer, I think this is what you need:

var view = SceneView.currentDrawingSceneView;
if (view != null)
{
    view.pivot = YOUR_GAME_OBJECT.transform.position;
}

Just use this piece of code in any Update function and the editor view will follow YOUR_GAME_OBJECT.
The beauty of it is that it only locks the editor’s pivot position, meaning that you can freely rotate the camera in any direction and you can zoom in and out at will.

*KISS: Keep It Simple, Stupid.

There is another way to accomplish this using the (undocumented) UnityEngine.SceneView interface. While there is no way to directly set the current scene view camera’s transform, there’s a handy AlignViewToObject call (identical to the GameObject menu item). Here’s some C# trickery:

var view = SceneView.currentDrawingSceneView;
if(view != null)
{
    var target = new GameObject();
    target.transform.position = NewCameraPosition;
    target.transform.rotation = NewCameraRotation;
    view.AlignViewToObject(target.transform);
    GameObject.DestroyImmediate(target);
}

I use this general strategy in a number of ways to make scene navigation less painful for myself, but you can probably see how it might be altered to suit your needs (e.g. since you are updating the camera every frame, it probably makes sense to keep a persistent dummy object around for alignment, rather than creating and destroying one each time).

Hope this helps!

This thread should give you the information you need:

http://forum.unity3d.com/viewtopic.php?t=33679

Basically, you can get the scene camera with `Camera.current`, but you NEED to check if it's null, since that variable is only available when the Scene view has focus, otherwise it will be null.

I'm not sure how much you can modify the Scene camera, either... I've heard mixed reports in the past. I've heard you can't really change how the camera behaves very much, but I've also seen some editor scripts that (claim to) modify it, so if something isn't working for you, it might just not be possible.

You could easily move the SceneView perspective through:

if (GUILayout.Button("Change To TopDown"))
{
      var targetPos = Vector3.zero;
      var sceneView = SceneView.lastActiveSceneView;
      sceneView.AlignViewToObject(target.transform); 
      sceneView.LookAtDirect(targetPos, Quaternion.Euler(90, 0, 0));
      sceneView.orthographic = true;
}

I had the same issue and wrote a script that follows the specified target transform. It even works for multiple SceneViews. It’s on Unity3D’s wiki now: http://wiki.unity3d.com/index.php/SceneViewCameraFollower Hope it helps.

I was trying to get the editor cam from a custom editor trough Camera.Curent, but it is null unless focused of the sceneView. So I ended up adding to EditorApplication.update a function that gets the Camera.current.transform for my editor window, then register it after i have a cam to use.

EditorApplication.update+=GetterFunction;

public static void GetterFunction(){
   pointerToSceneCam=Camera.current.transform;
   if(pointerToSceneCam!=null)EditorApplication.update-=GetterFunction;
}

There’s also SceneView.camera which could be used by SceneView.currentDrawingSceneView.camera.

Another undocumented but public part of SceneView is SceneView.GetAllSceneCameras().

There’s no mention of any SceneView changes in Unity’s API history, which leads me to believe this property and method might’ve been available all the time.

Correct answer is something like this (tnx @chomp).

private new Camera camera;
		bool isEditorCam;
		
		void Start()
		{
			camera = GetComponent<Camera>();

			#if UNITY_EDITOR
			isEditorCam = camera == null || SceneView.currentDrawingSceneView != null;
			#endif
		}

		protected override void OnRenderImage(RenderTexture source, RenderTexture destination)
		{
			if (camera == null) camera = GetComponent<Camera>();
                        if (isEditorCam) ;// do stuff...