Search Unity

Unity Editor: Viewport Navigation Hotkeys?

Discussion in 'Editor & General Support' started by Alan47, Mar 13, 2011.

  1. Alan47

    Alan47

    Joined:
    Mar 5, 2011
    Posts:
    163
    Hey guys,

    I'm new to Unity, but I have been using other 3D Engines and editors before - among them was Blender. Blender used to have an excellent way of navigating through the viewport using the following numpad hotkeys:

    1 - front view (CTRL + 1 - back view)
    3 - side view (CTRL + 3 - 'other' side view)
    7 - top view (CTRL + 7 - bottom view)
    5 - toggle ortographic / perspective display

    These would be ever so awesome to have inside the Unity editor as well. I know that these commands can be accessed using the viewport navigation gizmo, but having them as hotkeys would be so much faster... is there a way to do that? Maybe someone already wrote a script for that?

    And by the way - using ALT + left/middle/right mouse button is okay. But using ALT + LMB to rotate, SHIFT+ALT+LMB to pan and mouse wheel to zoom in and out would be even better. ALT+LMB is default anyway, zooming with mousewheel works too, but SHIFT+ALT+LMB unfortunately doesn't pan the view - any suggestions how to fix this? (Blender by default also has ALT+ MMB for screen paning, however, it can "emulate" a three-button-mouse by using the additional SHIFT key to basically turn LMB into MMB)

    Thanks in advance,


    Alan
     
  2. DiCola

    DiCola

    Joined:
    Nov 27, 2011
    Posts:
    1
    These hotkeys I'd like to see added in Unity. I've been cursing every time I switch back to Unity and my favourite ALT+SHIFT+LMB combination doesn't pan the view. :(
    The numpad view keys are hot too.
     
  3. CG-DJ

    CG-DJ

    Joined:
    Jan 28, 2012
    Posts:
    73
    Yeah, I'd really like to see hotkey buttons for switching to a view. I use Blender, therefore I automatically want the NumPad hotkeys as described above, but anything would be better then having to click that stupid diagram at the top right corner.
     
  4. Spanky555

    Spanky555

    Joined:
    May 6, 2013
    Posts:
    7
    Hey guys,

    I was actually looking for the same thing and I wrote a script that will allow you to switch the SceneView camera between Top, Bottom, Front, Back, Left, Right, and Perspective. This allows you to have quick changes which is great for level editing.

    There is a bool called 'sShouldTween'. If this is true, the camera will interpolate between the views similar to how it does when you click on the ViewCube in the top right. If it is false, it will instantly snap to the various views. Everything will appear in a new 'Camera' menu with associated hotkeys (Alt-Shift-Number). Feel free to change those.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6.  
    7. /// <summary>
    8. /// Adds a 'Camera' menu containing various views to switch between in the current SceneView
    9. /// </summary>
    10. public class CameraSwitcher : MonoBehaviour
    11. {
    12.     /// <summary>
    13.     /// The rotation to restore when going back to perspective view. If we don't have anything,
    14.     /// default to the 'Front' view. This avoids the problem of an invalid rotation locking out
    15.     /// any further mouse rotation
    16.     /// </summary>
    17.     static Quaternion sPerspectiveRotation = Quaternion.Euler(0, 0, 0);
    18.  
    19.     /// <summary>
    20.     /// Whether the camera should tween between views or snap directly to them
    21.     /// </summary>
    22.     static bool sShouldTween = true;
    23.  
    24.  
    25.     /// <summary>
    26.     /// When switching from a perspective view to an orthographic view, record the rotation so
    27.     /// we can restore it later
    28.     /// </summary>
    29.     static private void StorePerspective()
    30.     {
    31.         if (SceneView.lastActiveSceneView.orthographic == false)
    32.         {
    33.             sPerspectiveRotation = SceneView.lastActiveSceneView.rotation;
    34.         }
    35.     }
    36.  
    37.     /// <summary>
    38.     /// Apply an orthographic view to the scene views camera. This stores the previously active
    39.     /// perspective rotation if required
    40.     /// </summary>
    41.     /// <param name="newRotation">The new rotation for the orthographic camera</param>
    42.     static private void ApplyOrthoRotation(Quaternion newRotation)
    43.     {
    44.         StorePerspective();
    45.  
    46.         SceneView.lastActiveSceneView.orthographic = true;
    47.  
    48.         if (sShouldTween)
    49.         {
    50.             SceneView.lastActiveSceneView.LookAt(SceneView.lastActiveSceneView.pivot, newRotation);
    51.         }
    52.         else
    53.         {
    54.             SceneView.lastActiveSceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, newRotation);
    55.         }
    56.  
    57.         SceneView.lastActiveSceneView.Repaint();
    58.     }
    59.  
    60.  
    61.     [MenuItem("Camera/Top #1")]
    62.     static void TopCamera()
    63.     {
    64.         ApplyOrthoRotation(Quaternion.Euler(90, 0, 0));
    65.     }
    66.  
    67.  
    68.     [MenuItem("Camera/Bottom #2")]
    69.     static void BottomCamera()
    70.     {
    71.         ApplyOrthoRotation(Quaternion.Euler(-90, 0, 0));
    72.     }
    73.  
    74.  
    75.     [MenuItem("Camera/Left #3")]
    76.     static void LeftCamera()
    77.     {
    78.         ApplyOrthoRotation(Quaternion.Euler(0, 90, 0));
    79.     }
    80.  
    81.  
    82.     [MenuItem("Camera/Right #4")]
    83.     static void RightCamera()
    84.     {
    85.         ApplyOrthoRotation(Quaternion.Euler(0, -90, 0));
    86.     }
    87.  
    88.  
    89.     [MenuItem("Camera/Front #5")]
    90.     static void FrontCamera()
    91.     {
    92.         ApplyOrthoRotation(Quaternion.Euler(0, 0, 0));
    93.     }
    94.  
    95.     [MenuItem("Camera/Back #6")]
    96.     static void BackCamera()
    97.     {
    98.         ApplyOrthoRotation(Quaternion.Euler(0, 180, 0));
    99.     }
    100.  
    101.  
    102.     [MenuItem("Camera/Persp Camera #7")]
    103.     static void PerspCamera()
    104.     {
    105.         if (SceneView.lastActiveSceneView.camera.orthographic == true)
    106.         {
    107.             if (sShouldTween)
    108.             {
    109.                 SceneView.lastActiveSceneView.LookAt(SceneView.lastActiveSceneView.pivot, sPerspectiveRotation);
    110.             }
    111.             else
    112.             {
    113.                 SceneView.lastActiveSceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, sPerspectiveRotation);
    114.             }
    115.  
    116.             SceneView.lastActiveSceneView.orthographic = false;
    117.  
    118.             SceneView.lastActiveSceneView.Repaint();
    119.         }
    120.     }
    121. }
    122.  

    Hope that helps!
    Shawn
     
  5. Play_Edu

    Play_Edu

    Joined:
    Jun 10, 2012
    Posts:
    722
    Thanks for sharing.
     
  6. mosaim

    mosaim

    Joined:
    Aug 19, 2013
    Posts:
    1
    How can we change associated hotkeys?
     
  7. Spanky555

    Spanky555

    Joined:
    May 6, 2013
    Posts:
    7
  8. dmi3ich

    dmi3ich

    Joined:
    Mar 30, 2013
    Posts:
    4
    2 Spanky555: thank you for useful script!
     
  9. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    that's great. i was trying to find a way to map the hotkeys to numpad (blender style) but as far as i can tell, that's not possible http://answers.unity3d.com/questions/224603/numpad-keys-in-menuitem.html

    any ideas how to do that anyone?

    closest i can get is using the F keys
    [MenuItem("Camera/Top _F7")]
    static void TopCamera()
    {
    ApplyOrthoRotation(Quaternion.Euler(90, 0, 0));
    }


    thanks?
     
    Last edited: Dec 4, 2016
  10. IronHelmet

    IronHelmet

    Joined:
    May 2, 2017
    Posts:
    85
    Anybody know how to change the ShadingMode? I'd like my perspective view shaded but my tops and side be wireframe.
     
  11. ZuluOneZero

    ZuluOneZero

    Joined:
    Nov 10, 2016
    Posts:
    5
    Spanky555 That's Awsomely handy - thanks. Makes editing colliders so quick!
     
  12. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    can i use that script in edit mode?

    i tried adding it to my camera object but nothing happens in edit mode

    thanks
     
  13. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,327
    You're not supposed to attach the script to any specific camera. It always automatically targets the Scene view camera.

    All the methods are static (not tied to a specific instance), and you call them using the main menu items "Camera/Top" etc. or their respective shortcuts. It should work in edit mode.
     
    steveh2112 likes this.
  14. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    oh cool, that is so awesome, i set this
    [MenuItem("Camera/Top _HOME")]
    etc to get blender like keys
     
    SisusCo likes this.
  15. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    FYI, you need to surround the code with this
    #if (UNITY_EDITOR)
    ... your class/code ...
    #endif
    if you want it to build
     
  16. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,327
  17. mattwilson

    mattwilson

    Joined:
    Jul 26, 2020
    Posts:
    1
    Thanks for an incredibly useful script @Spanky555!

    I put this in a folder called "Editor" and now it's way easier to move around between the different views and align things etc.

    Does anybody know if it's possible to use numpad keys for the shortcuts when defining the Menu Items in code?

    For anyone wondering the same thing I did find a workaround though, if you go to Edit -> Shortcuts and search you can search for each one (e.g. "Camera/Front") and change it to numpad bindings in there. For example NUMPAD1 for front, Ctrl+NUMPAD1 for back etc, like Blender :)
     
    SisusCo likes this.
  18. franciscochong

    franciscochong

    Joined:
    Jul 9, 2015
    Posts:
    30
    Hey guys,
    So this is what I did and it feels more on par with blender.
    Thanks @mattwilson
    upload_2021-5-3_13-33-38.png upload_2021-5-3_13-34-51.png
     
    petrknedlik3 and Whatever560 like this.
  19. amirhosseinzarei151

    amirhosseinzarei151

    Joined:
    Oct 15, 2020
    Posts:
    2
    Guys, I had this completely figured out for Unity 2021 LTS: Here's the code. Just make a script and put it in the "Editor" folder in your project files. It also works for going to the opposite view using Numpad 9. Just like Blender :)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEditor.ShortcutManagement;
    4.  
    5. public static class SceneViewShortcuts
    6. {
    7.     [Shortcut("Scene View Camera - Top view", KeyCode.Keypad7)]
    8.     public static void TopView()
    9.     {
    10.         MakeSceneViewCameraLookAtPivot(Quaternion.Euler(90, 0, 0));
    11.     }
    12.  
    13.     [Shortcut("Scene View Camera - Right view", KeyCode.Keypad3)]
    14.     public static void RightView()
    15.     {
    16.         MakeSceneViewCameraLookAtPivot(Quaternion.Euler(0, -90, 0));
    17.     }
    18.  
    19.  
    20.     [Shortcut("Scene View Camera - Front view", KeyCode.Keypad1)]
    21.     public static void FrontView()
    22.     {
    23.         MakeSceneViewCameraLookAtPivot(Quaternion.Euler(0, 0, 0));
    24.     }
    25.  
    26.     [Shortcut("Scene View Camera - Opposite view", KeyCode.Keypad9)]
    27.     public static void ToggleView()
    28.     {
    29.         SceneView sceneView = SceneView.lastActiveSceneView;
    30.         Camera camera = SceneView.lastActiveSceneView.camera;
    31.         Vector3 currentView = camera.transform.rotation.eulerAngles;
    32.         if (currentView == new Vector3(0, 0, 0))
    33.         {
    34.             MakeSceneViewCameraLookAtPivot(Quaternion.Euler(0, 180, 0));
    35.         }
    36.  
    37.         else
    38.         {
    39.             MakeSceneViewCameraLookAtPivot(Quaternion.Euler(-currentView));
    40.         }
    41.     }
    42.  
    43.  
    44.  
    45.     private static void MakeSceneViewCameraLookAtPivot(Quaternion direction)
    46.     {
    47.         //introduce the scene window that we want to do sth to
    48.         SceneView sceneView = SceneView.lastActiveSceneView;
    49.  
    50.         //if there is no scene window do nothing
    51.         if (sceneView == null) return;
    52.  
    53.         //not a camera on the scene but the scene window camera
    54.         Camera camera = sceneView.camera;
    55.  
    56.         //Get the pivot
    57.         Vector3 pivot = sceneView.pivot;
    58.  
    59.  
    60.         sceneView.LookAt(pivot, direction);
    61.     }
    62. }
     
  20. mmorsi

    mmorsi

    Joined:
    Oct 14, 2021
    Posts:
    9
    Thanks to everyone that contributed to this thread. To extend amirhosseinzarei151's updated answer. To switch between orthographic & perspective cameras simply add this method to the SceneViewShortcuts class above:

    Code (CSharp):
    1. [Shortcut("Scene View Camera - Perspective Switch", KeyCode.Keypad5)]
    2. static void PerspCamera()
    3. {
    4.    SceneView.lastActiveSceneView.orthographic = !SceneView.lastActiveSceneView.orthographic;
    5. }
    And toggle the switch by hitting the 5 key on your keypad