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?
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 is an amazing hack. I was already trying to get the addresses with CheatEngine and such, but this is a lot easier on my nerves. The only downside is that there is a lot of smoothing so it's probably going to be weird to use with home-brewed camera navigation code.
– ZylIn addition, you likely want view.orthographic = false; (or maybe true if you want the iso cam). Also, you can use view.camera as your dummy object! For some reason using SceneView.lastActiveSceneView worked better for me (currentDrawingSceneView resulted in nothing happening even though I only have one scene view open).
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.
Thanks. If it's null while the scene view does not have focus then this won't work for me. I need it to follow while I play the game. However, you did answer my question!
– anon28030230Then I don't know if what you want is possible... you could wait to see if someone else responds here, or ask on the forums if you need it that badly. :P
– qJakeBob, perhaps you could save what camera you are referencing when the scene does have focus, and then use that reference later? I'm atm trying to do something similar myself, don't know if it'll work. Think of it like this: I open my custom editor window. Editor window keeps trying to assign a camera - if null, retry. I click on the scene view. Camera get's assigned. I click outside, but the reference is kept.
– anon73820239This is not really correct. I found more useful this UnityEditor.SceneView.lastActiveSceneView.camera
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...
or, to combine it with other answers, if you need the last active scene view camera: SceneView.lastActiveSceneView.pivot = ... (pivot is a property of SceneView, so once you get the scene view of interest you can just set it)
– huulong