Unity3d Editor camera

Hi,

I searched the forum, maybe this information is old or burried somewhere deep in the forum. Well in any cases I would happily like to know if i can get the camera in the unity3d editor in #scene mode. I am doing pathfinding placement and as you could guess drawing gizmos at each 1 Unit on a 100x100 map can slow the editor down. I added a show variable to disable all gizmos. But it would be better for me to test the distance for the said Node from the camera distance, if its less than for example 20 units, draw it. This would greatly improve visual cues speed for me.

I don’t think there is any way to get the position of the Scene view camera. Also, I know you were trying to give the impression of scale, but it is much better if you don’t add large images to your posts - it’s a nuisance to read when you have to keep scrolling horizontally.

Sorry for the image size, fixed it. Still searching for some answer for this any Unity dev can answer this quickly please ?

Hmm, if I recall right, isn’t in the editor

Camera.main

the game camera and

Camera.current

the editor camera? I think I read this in the docs somewhere.

Those are for runtime. When you use Camera.main, its the first tagged Main Camera camera that you receive.

I used this for testing:

	@MenuItem("PathFinding/Test")
	static function test()
	{
		var cam:Camera = Camera.main;
		Debug.Log("Camera name is : " + cam.name + " pos: " + cam.transform.position.ToString());
	}

So it returns me my camera hierarchy object. Camera.current returns null if used in the above code instead of .main

This is no good i need the editor camera for editor window gizmo culling.

Ok, found it. I have an editor script here which relies on the editor camera and uses Camera.current to calculate some stuff based on the editor camera (which is then Camera.current):

public class TrackNodeMenuItem : MonoBehaviour {

	[MenuItem("GameObject/Create Track Node")]

	static void CreateTrackNode() {	
		
		// Cast ray to find position to place the track node
		RaycastHit hit = new RaycastHit();
		Transform cameraTransform = Camera.current.transform;
		Vector3 fwd = cameraTransform.TransformDirection(Vector3.forward);

Ok thanks Martin,

I really do not know why it does go through with the following now and it did not 15 min. ago ^ ^

	@MenuItem("PathFinding/Test")
	static function test()
	{
		Debug.Log("Camera name is : " + Camera.current.name + " pos: " + Camera.current.transform.position.ToString());
	}

gives the following :

Camera name is : SceneCamera pos: (2.2, 6.7, 3.9)
UnityEngine.Debug:Log(Object)
PathFindingEditor:test() (at Assets\Editor\PathFindingEditor.js:8)

Thanks for helping, guess this was pretty obvious after all !

FYI…

I stumbled across this thread while trying to figure out this same thing on my own. Some further testing reveals that Camera.current is apparently only valid if a Scene view has focus, and it’ll be the camera for that particular Scene view. If no Scene view has focus (f.ex. you last clicked on the Inspector, or something) then Camera.current will be null.

In my case, I have a menu item creating a GameObject based on the editor camera’s position. It’s annoying to require Scene view focus, especially when I’m using a hotkey and making lots of adjustments in the Inspector… so now I keep a m_lastUsedCamera around, which I use in case Camera.current comes up null. Whenever Camera.current comes up valid, I update m_lastUsedCamera to that camera.

Seems to work for all cases so far, except if you start up a fresh editing session and invoke the menu command for the first time without Scene view focus… I’m just printing a warning in that case.

Not to resurect an old thread but noticed you were all using Camera.current and getting null.

Try camera.current to get the current camera (Notice no cap C on camera)

There doesn’t seem to be a member camera, the only way I can find a lower case one is through Camera.main.camera ?

But still on this subject, has anyone found a way to do this? I’m starting to write more editor scripts and this need starts coming up more and more…

For example, you might simply want a ‘decal projector’ script that uses the editor cam… a ‘select all in view’ using the editor cam, or you might be saving editor positions (like you can in UE3 Editor) in a very large level and want to quickly Get/Set the camera position… etc, etc! :slight_smile:

How can we access that camera considering the problems above that ‘current’ gets set to null if the Scene window isn’t focused?

my projects are still underway with U2.6 so I haven’t check in U3… but is this still a missing ‘feature’?
Can anyone access the editor camera yet?

I think the method involving Camera.current (capital C on Camera - it’s a class property) has always worked under “normal” circumstances. Is this not working for you under 2.6?

hey andeee - actually yes that works, sorry i misread the above mentioned camera.current (as the no capital was pointed out! :stuck_out_tongue: )
this does indeed return the Scene view camera though only when it is being rendered (hover the mouse… etc)
but cool, I’ll work from here then!
thanks! :slight_smile:

I create a complete script for the ones still looking:

using UnityEditor;
using UnityEngine;

public static class SceneCameraMenuItem
{
    private static Camera _sceneCamera = null;

    [MenuItem("Scene/Set camera")]
    public static void SetMainToScene()
    {
        if (Camera.main == null)
        {
            Debug.Log("Main camera not found.");
            return;
        }
        if (_sceneCamera == null)
        {
            if (Camera.current == null || Camera.current.name != "SceneCamera")
            {
                Debug.Log("Scene camera not selected. First click on Scene tab before calling.");
                return;
            }
            _sceneCamera = Camera.current;
        }

        Camera.main.transform.position = _sceneCamera.transform.position;
        Camera.main.transform.rotation = _sceneCamera.transform.rotation;
    }
}

Great work, Synercoder, note though that the functionality you seek is actually already available in the default Unity menus.

GameObject → Align With View

and

GameObject → Align View to Selected

2 Likes

Well, that is what you get when you don’t know the editor… Reinventing the wheel…

Best solution I’ve found is here at http://www.olivereberlei.com/517/wrestling-with-the-editor-camera-in-unity/