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.
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);
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.
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!
How can we access that camera considering the problems above that ‘current’ gets set to null if the Scene window isn’t focused?
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! )
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!
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;
}
}