Search Unity

Made script to control editor camera with keyboard

Discussion in 'Assets and Asset Store' started by Doireth, Apr 28, 2018.

  1. Doireth

    Doireth

    Joined:
    Jul 3, 2012
    Posts:
    11
    I made this script to make it easier to move the camera in the editor through use of the keyboard. Thought some of you might want it also. If you have an object selected, the camera will align to it, if you have multiple objects selected, the camera will align to a median point of all selected objects and if you have nothing selected, the camera will align to a point just in front of it.

    Must be placed in a folder called "Editor" anywhere inside your assets folder.

    The controls are Alt+1/2/3/4/5. 5 toggles orthographic.

    EDIT: Updated code

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. [InitializeOnLoad]
    6. public static class CameraKeyboardControlsEDITOR
    7. {
    8.     enum CameraView
    9.     {
    10.         ISOMETRIC, ABOVE, FRONT, RIGHT
    11.     }
    12.  
    13.     [MenuItem("Camera/Top &1")]
    14.     static void MenuSetCameraFront()
    15.     {
    16.         MenuSetCameraForBoard(CameraView.FRONT);
    17.     }
    18.  
    19.     [MenuItem("Camera/Right &2")]
    20.     static void MenuSetCameraRight()
    21.     {
    22.         MenuSetCameraForBoard(CameraView.RIGHT);
    23.     }
    24.  
    25.     [MenuItem("Camera/Above &3")]
    26.     static void MenuSetCameraAbove()
    27.     {
    28.         MenuSetCameraForBoard(CameraView.ABOVE);
    29.     }
    30.  
    31.     [MenuItem("Camera/Isometric &4")]
    32.     static void MenuSetCameraInGame()
    33.     {
    34.         MenuSetCameraForBoard(CameraView.ISOMETRIC);
    35.     }
    36.  
    37.     [MenuItem("Camera/Toggle Orthographic &5")]
    38.     static void MenuSetCameraOrthographic()
    39.     {
    40.         var scene_view = UnityEditor.SceneView.lastActiveSceneView;
    41.         scene_view.orthographic = !scene_view.orthographic;
    42.     }
    43.  
    44.     static void MenuSetCameraForBoard(CameraView view)
    45.     {
    46.         bool usedTemp = false, gotMedianForMultipleObjects = false;
    47.         GameObject go = null;
    48.         var scene_view = UnityEditor.SceneView.lastActiveSceneView;
    49.  
    50.         if (Selection.gameObjects.Length == 1)
    51.         {
    52.             go = Selection.gameObjects[0];
    53.         }
    54.         else if (Selection.gameObjects.Length > 1)
    55.         {
    56.             Vector3 pos = GetMedianPointOfSelection();
    57.  
    58.             // create a dummy to act as focus point
    59.             go = new GameObject("<temp>");
    60.             go.transform.position = pos;
    61.             usedTemp = true;
    62.             gotMedianForMultipleObjects = true;
    63.         }
    64.  
    65.         if (go == null)
    66.         {
    67.             // create a dummy to act as focus point
    68.             go = new GameObject("<temp>");
    69.             go.transform.position = scene_view.camera.transform.position + (scene_view.camera.transform.forward * 5f);
    70.             usedTemp = true;
    71.         }
    72.  
    73.         // aligning view in perspective mode makes the camera zoom in way to far.
    74.         // To fix, switch to ortho temporarily.
    75.         bool ortho = scene_view.orthographic;
    76.         scene_view.orthographic = true;
    77.  
    78.         SetCameraView(go.transform.position, view);
    79.  
    80.         scene_view.orthographic = ortho; // reset
    81.  
    82.         if (usedTemp)
    83.         {
    84.             GameObject.DestroyImmediate(go);
    85.             if (!gotMedianForMultipleObjects)
    86.             {
    87.                 Selection.activeGameObject = null; // clear selection
    88.             }
    89.         }
    90.     }
    91.  
    92.     static Vector3 GetMedianPointOfSelection()
    93.     {
    94.         GameObject[] gos = Selection.gameObjects;
    95.         var bounds = new Bounds(gos[0].transform.position, Vector3.zero);
    96.         for (var i = 1; i < gos.Length; i++)
    97.             bounds.Encapsulate(gos[i].transform.position);
    98.         return bounds.center;
    99.     }
    100.  
    101.     static void SetCameraView(Vector3 cam_position, CameraView view)
    102.     {
    103.         EditorApplication.ExecuteMenuItem("Window/Scene");
    104.  
    105.         var scene_view = UnityEditor.SceneView.lastActiveSceneView;
    106.         if (scene_view != null)
    107.         {
    108.             var target = scene_view.camera;
    109.             target.transform.position = cam_position;
    110.  
    111.             if (view == CameraView.ABOVE)
    112.             {
    113.                 target.transform.rotation = Quaternion.LookRotation(Vector3.down);
    114.             }
    115.             else if (view == CameraView.FRONT)
    116.             {
    117.                 target.transform.rotation = Quaternion.LookRotation(Vector3.forward);
    118.             }
    119.             else if (view == CameraView.RIGHT)
    120.             {
    121.                 target.transform.rotation = Quaternion.LookRotation(Vector3.left);
    122.             }
    123.             else if (view == CameraView.ISOMETRIC)
    124.             {
    125.                 target.transform.rotation = Quaternion.Euler(new Vector3(45f, -45f, 0));
    126.             }
    127.  
    128.             scene_view.AlignViewToObject(target.transform);
    129.         }
    130.     }
    131. }
    132.  
    133.  
     
    Last edited: Apr 28, 2018
    Aeroxima likes this.
  2. Aeroxima

    Aeroxima

    Joined:
    Jul 19, 2016
    Posts:
    15
    This is actually pretty useful. I spent a while simplifying the code (and testing it and fixing it and simplifying more), and I rather like how it came out. I believe it does essentially the same thing, but puts them as shortcuts you can bind under Scene View, rather than menu items.

    (I guess I changed the order and default keys too, I didn't like isometric so close to orthographic, I hit it by accident.)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEditor.ShortcutManagement;
    4.  
    5. #pragma warning disable IDE0051 // Stop warnings about unused functions, they are actually used because of Unity attributes
    6.  
    7. public static class FocusCam
    8. {
    9.     private static Vector3 isometric = new Vector3(-1f, -1f, 1f).normalized;
    10.  
    11.     [Shortcut("Scene View/Look at selection, isometric", KeyCode.Alpha1, ShortcutModifiers.Alt)]
    12.     static void SetCameraIsometric()
    13.     {
    14.         SetCameraView(isometric);
    15.     }
    16.  
    17.     [Shortcut("Scene View/Look at selection, from behind", KeyCode.Alpha2, ShortcutModifiers.Alt)]
    18.     static void SetCameraBehind()
    19.     {
    20.         SetCameraView(Vector3.forward);
    21.     }
    22.  
    23.     [Shortcut("Scene View/Look at selection, from top", KeyCode.Alpha3, ShortcutModifiers.Alt)]
    24.     static void SetCameraTop()
    25.     {
    26.         SetCameraView(Vector3.down);
    27.     }
    28.  
    29.     [Shortcut("Scene View/Look at selection, from right side", KeyCode.Alpha4, ShortcutModifiers.Alt)]
    30.     static void SetCameraRight()
    31.     {
    32.         SetCameraView(-Vector3.right);
    33.     }
    34.  
    35.     [Shortcut("Scene View/Toggle Orthographic", KeyCode.Alpha5, ShortcutModifiers.Alt)]
    36.     static void ToggleOrthographic()
    37.     {
    38.         SceneView.lastActiveSceneView.orthographic = !SceneView.lastActiveSceneView.orthographic;
    39.     }
    40.  
    41.     static void SetCameraView(Vector3 viewFromDirection)
    42.     {
    43.         var view = SceneView.lastActiveSceneView;
    44.         if (view != null)
    45.         {
    46.             view.LookAt(view.pivot, Quaternion.LookRotation(viewFromDirection));    //gets the angle right (without teleporting there)
    47.             view.FrameSelected(false);      //frames the selection and gets the distance right, order on these is important, unless lockView == true, in which case buggy
    48.         }
    49.     }
    50. }
     
    Last edited: Dec 7, 2019