Search Unity

Blender like Camera Orientation Shortcuts

Discussion in 'Scripting' started by SarperS, Oct 1, 2012.

  1. SarperS

    SarperS

    Joined:
    Mar 10, 2009
    Posts:
    824
    I'm trying to come up with an editor script that will let me change the scene view camera orientation with keypad keys (7 for top, 1 for front and 3 for right views) just like in Blender.

    I'm able to get the right camera and get the correct keyboard events, but when I change the rotation of the scene view camera, nothing happens.

    Is there a way to do this or better yet a class to access the top right gizmo in the scene view that we use to manipulate the camera orientation and render mode(perspective/orthographic)

    Below is my code so far.

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomEditor(typeof(Transform))]
    5. public class BlenderCamControl : Editor {
    6.     public void OnSceneGUI() {
    7.         //TODO If no transform selected, select first transform in hierarchy
    8.  
    9.         Event keyboardEvent = Event.current;
    10.  
    11.         if(keyboardEvent.isKey) {
    12.             switch(keyboardEvent.keyCode) {
    13.                 case KeyCode.Keypad1:
    14.                     Debug.Log(Camera.current.transform.eulerAngles);
    15.                     Camera.current.transform.rotation = Quaternion.Euler(new Vector3(0f, 180f, 0f));
    16.                     break;
    17.  
    18.                 case KeyCode.Keypad3:
    19.                     Camera.current.transform.rotation = Quaternion.Euler(new Vector3(0f, 270f, 0f));
    20.                     break;
    21.  
    22.                 case KeyCode.Keypad7:
    23.                     Camera.current.transform.rotation = Quaternion.Euler(new Vector3(90f, 0f, 0f));
    24.                     break;
    25.             }
    26.         }
    27.     }
    28. }
     
    Last edited: Oct 2, 2012
  2. SarperS

    SarperS

    Joined:
    Mar 10, 2009
    Posts:
    824
    Solved it, as it turns out there was a class named SceneView in UnityEditor namespace and it's not documented. I had to change SceneView.lasActiveSceneView.camera and it works.
     
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,445
    Sorry for dredging up such an old thread, but I found your script on the Unify wiki. Have you figured out a way to make this work even when no object is selected in the Scene?
     
  4. SarperS

    SarperS

    Joined:
    Mar 10, 2009
    Posts:
    824
    Hey, well I actually never even finished the script, someone else did it picking up where I left off. So it would be better to contact him I guess as I didn't work on it since then.