Search Unity

Camera View hotkey

Discussion in 'Editor & General Support' started by Jefreestyles, Dec 11, 2013.

  1. Jefreestyles

    Jefreestyles

    Joined:
    Mar 24, 2013
    Posts:
    3
    Is there a way to get the Scene view to quickly zoom to what a Camera object is looking at? I was thinking along the same lines as the 'f' Focus hotkey.

    It's great that the Play view does that but I think it would be easier to manipulate objects in the Scene view if the two views matched.
     
  2. drewradley

    drewradley

    Joined:
    Sep 22, 2010
    Posts:
    3,063
    The game window only shows what the camera sees so you have to move the camera. To do this select the main camera and hit Ctrl Shift F. The camera will then align with the scene view.

    btw, you have it backwards: the "f" key focuses the Scene window not the game window.
     
    Last edited: Dec 11, 2013
    Pheonix14 and Charmatataa like this.
  3. Jefreestyles

    Jefreestyles

    Joined:
    Mar 24, 2013
    Posts:
    3
    I'm not referring to the Game window. I'm referring to the Scene window (the window where you can move around objects). I want the Scene window to snap to what my Game window is viewing.

    I think it would be easier to manipulate objects while looking at the view the player will be seeing at the same time.

    Though I'll try the Cltr-Shift F hotkey when I get home from work today. Thanks!
     
  4. drewradley

    drewradley

    Joined:
    Sep 22, 2010
    Posts:
    3,063
    That can be done by selecting the camera and clicking on GameObject/Align View to Selected in the top menu. Sorry about that. Either way can be done. :)
     
    E4M4L likes this.
  5. Jefreestyles

    Jefreestyles

    Joined:
    Mar 24, 2013
    Posts:
    3
    Thanks. The option is called "Align View to Selected".

    Is there a way to create a shortcut key for this? I read that you can do that in System Preferences > Mouse and Keyboard. But I can't find that menu in v.4.3. I see Edit > Preferences > Keyes but the options are limited.

    I found this thread: http://blogs.unity3d.com/2011/08/24/unity-hotkeys-keyboard-shortcuts-in-unity/. Looks like a custom menu script needs to be created.

    Ashkan
    25 Aug 2011, 8:58 am

    @DanJ
    some of the keys are modifiable in Edit/Preferences in Keys tab of the preferences window.
    for others you should write simple editor script menus which have your hotkey. then in the menu’s function just execute the menu you want
    for example do this in js

    @MenuItem(“HotKey/Build%g”)
    static function DoIt()
    {
    EditorApplication.ExecuteMenuItem(“File/Build”);
    }

    now with ctrl+g (%g) you can execute the build command. the drawback is that you’ll see the hotkey menu in your menu bar but maybe there is another way to do this that i forgot. for more information about assigning shortkeys to menus see the [MenuItem] in Editor classes of the scripting reference.

    hope this helps​

    Maybe someone already created this and is generous enough to share? :D
     
    levlaj likes this.
  6. Hatsuko

    Hatsuko

    Joined:
    Oct 14, 2013
    Posts:
    5

    This is my first time to post on the forum. So if I've done anything inappropriate please pardon me...

    I use "Align View to Selected" very often recently, but I need to repeatedly go to "GameObject > Align View to Selected" and that's not very handy :/ I was wondering if there is a way to set a shortcut so I found this thread. I know this thread is old but I think this might help someone.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class Hotkey : EditorWindow {
    5.  
    6.    [MenuItem ("Hotkey/Align View to Selected %g")]
    7.    // Use this for initialization
    8.    static void Start () {
    9.        GameObject activeGO = Selection.activeGameObject;
    10.        SceneView.lastActiveSceneView.rotation = activeGO.transform.rotation;
    11.  
    12.        Vector3 position = activeGO.transform.position + (activeGO.transform.forward * 10);
    13.        SceneView.lastActiveSceneView.pivot = position;
    14.  
    15.        SceneView.lastActiveSceneView.Repaint();
    16.    }
    17. }
    18.  
    19. // Reference:
    20. // http://answers.unity3d.com/questions/802411/api-for-sceneview-class.html
    21. // http://answers.unity3d.com/questions/26606/align-with-view-programmatically.html
    22. // https://forum.unity3d.com/threads/moving-scene-view-camera-from-editor-script.64920/
    I'm unfamiliar with SceneView but I just mess with the code and create this. Put this script in your project folder in a folder called Editor. Press Ctrl+G to fake "Align View to Selected". (Well Ctrl+G is just a random one in fact I'll change it later)

    But it will not 100% work the same as "Align View to Selected". The position is slightly different. But it suits my need for now. Maybe someone can make it better :)
     
    noob2089, uxworldwide and noemis like this.
  7. noemis

    noemis

    Joined:
    Jan 27, 2014
    Posts:
    76
    Thank you @Hatsuko - for me it works perfect and saves some anoying clicks...
     
  8. Hatsuko

    Hatsuko

    Joined:
    Oct 14, 2013
    Posts:
    5
    Sorry to bump again
    I finally found it!

    It now uses Unity's API so it works more naturally. (The smooth moving of the scene camera, I mean)

    Code (CSharp):
    1. // using UnityEngine;
    2. using UnityEditor;
    3.    
    4. public class Hotkey : EditorWindow {
    5.    
    6.     [MenuItem("Hotkey/Align View to Selected &#f")] // Alt+Shift+F
    7.     public static void HotkeyAlignViewToSelected () {
    8.         if (SceneView.lastActiveSceneView != null && Selection.activeTransform != null) {
    9.             SceneView.lastActiveSceneView.AlignViewToObject(Selection.activeTransform);
    10.         }
    11.     }
    12. }
    13.    
    14. // Reference:
    15. // https://github.com/MattRix/UnityDecompiled/blob/master/UnityEditor/UnityEditor/SceneView.cs
    @noemis Glad I can help :)
     
    Tonymotion, echoMike, dbrinda and 3 others like this.
  9. noemis

    noemis

    Joined:
    Jan 27, 2014
    Posts:
    76
    Then again thank you @Hatsuko ... I now use this solution to align my camera with a object. Because with this there is no position offset anymore (caused when using the scroll wheel and the old method)... but i'm still wondering, why we can not simply access the virtual scene view camera and just set the position. Ideally I don't want to align with the selected object rotation, but only move to the position... something like: AlignViewPositionToObject.

    But still, this already saves me time.
     
  10. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    That's really cool. The fov is just not the same. Even when i set the SceneView's camera's fov equal to my Camera.main's fov it still stays at ~55-60fov for some reason. Does someone know how I can set the FOV of the scene camera?
     
  11. uxworldwide

    uxworldwide

    Joined:
    Jul 28, 2020
    Posts:
    1
    For those who are going to use this script (the first one from the top), which is fantastic:

    1) select the camera's GameObject first
    2) and then hit CTRL+G (CMD+G for MacOs) and the scene view will align to the camera, or any other selected GameObject)
     
  12. Tonymotion

    Tonymotion

    Joined:
    Dec 5, 2018
    Posts:
    12
    Thank you so much!! This is super-helpful!

    Based on your script, I made a new script called Hotkeys which searched for and found an object named "Spawn Point", and moved my Scene View to 1.8m above it, so my Scene View would be where my avatar's eyes were when I spawned into the world I'm building.

    But wait! There's more! :D

    I then put a regular C# script on a Game Object, calling them both "HotkeysData", to hold empty Game Objects which I put into fields on that Hotkeysdata. Then I modified that original Editor script so that it finds that HotkeysData Game Object, and can access all those other objects through it.

    THEN I added more shortcuts to the Editor "Hotkeys script", and ended up with:
    - Alt+Shift+F for Align View to Selected
    - Ctrl+G to move my Scene View to the Avatar's eyes position;
    - Ctrl+RightArrow to cycle my Scene View through "View Point" Game Objects I set up at various locations (& orientations) I go to often;
    - Alt+LeftArrow to cycle in the reverse order.

    Note1: I had to mark as "static" all the class variables and internal methods in the Hotkeys Editor script.

    Note2: There is NO WAY to clone a Transform. So, in order to add the avatar height to the transform *without* modifying the original spawn point, I had to make my own semi-deep copy method to get something to submit to the AlignViewToObject() command. :(

    Your script has opened up some power for me in being able to navigate quickly to distinct, pre-set points around my world. This will come in super-handy as the world gets bigger. :)
     
    Last edited: Feb 24, 2023