Blender Camera Controls

From Unify Community Wiki
Revision as of 17:18, 29 January 2013 by Nighteyes (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Author

Marc Kusters (Nighteyes)

Description

This adds the default Blender numpad camera controls to Unity. Initial idea from [1].

Usage

To start using this script just place it in de editor folder of your project. It is prerequisite to select any object, otherwise the script won't run (this I will hope to fix someday).

Numpad1 = Front view
Control + Numpad1 = Rear view 
Numpad2 = Rotate view down
Numpad3 = Right view
Control + Numpad3 = Left view 
Numpad4 = Rotate view left
Numpad5 = Switch between orthographic and perspective
Numpad6 = Rotate view right
Numpad7 = Top view
Control + Numpad7 = Down view
Numpad8 = Move view up
Numpad. = Center view on object(s) Note:only works on objects that have the CanEditMultipleObjects property

UnityScript - BlenderCameraControls.cs

// BlenderCameraControls.cs
// by Marc Kusters (Nighteyes)
//
// This is just a helper object, it draws a fake Axis gizmo.
// Usage: Select any object to use the camera hotkeys. 
//
 
using UnityEngine;
using UnityEditor;
using System.Collections;
 
[CustomEditor(typeof(Transform)), CanEditMultipleObjects]
public class BlenderCameraControls : Editor
{
    UnityEditor.SceneView sceneView;
 
    private int tick = 0;
    private Vector3 eulerAngles;
    private Event current;
    private Quaternion rotHelper;
 
    public void OnSceneGUI()
    {
 
        current = Event.current;
        sceneView = UnityEditor.SceneView.lastActiveSceneView;
        eulerAngles = sceneView.camera.transform.rotation.eulerAngles;
        rotHelper = sceneView.camera.transform.rotation;
 
        if (current.isKey)
            if (current.type == EventType.keyDown)
            {
                switch (current.keyCode)
                {
                    case KeyCode.Keypad1:
                        if (current.control == false)
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(0f, 360f, 0f)));
                        else
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(0f, 180f, 0f)));
                        break;
                    case KeyCode.Keypad2:
                        if (tick > 8)
                        {
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, rotHelper * Quaternion.Euler(new Vector3(15f, 0f, 0f)));
                            tick = 0;
                        }
                        break;
                    case KeyCode.Keypad3:
                        if (current.control == false)
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(0f, 270f, 0f)));
                        else
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(0f, 90f, 0f)));
                        break;
                    case KeyCode.Keypad4:
                        if (tick > 8)
                        {
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(eulerAngles.x, eulerAngles.y + 15f, eulerAngles.z)));
                            tick = 0;
                        }
                        break;
                    case KeyCode.Keypad5:
                        if (tick > 8)
                        {
                            if (sceneView.orthographic == true)
                                sceneView.orthographic = false;
                            else if (sceneView.orthographic == false)
                                sceneView.orthographic = true;
                            tick = 0;
                        }
                        break;
                    case KeyCode.Keypad6:
                        if (tick > 8)
                        {
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(eulerAngles.x, eulerAngles.y - 15f, eulerAngles.z)));
                            tick = 0;
                        }
                        break;
                    case KeyCode.Keypad7:
                        if (current.control == false)
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(90f, 0f, 0f)));
                        else
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(270f, 0f, 0f)));
                        break;
                    case KeyCode.Keypad8:
                        if (tick > 8)
                        {
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, rotHelper * Quaternion.Euler(new Vector3(-15f, 0f, 0f)));
                            tick = 0;
                        }
                        break;
                    case KeyCode.KeypadPeriod:
                        if (Selection.transforms.Length == 1)
                            sceneView.LookAtDirect(Selection.activeTransform.position, sceneView.camera.transform.rotation);
                        else if (Selection.transforms.Length > 1)
                        {
                            Vector3 tempVec = new Vector3();
                            for (int i = 0; i < Selection.transforms.Length; i++)
                            {
                                tempVec += Selection.transforms[i].position;
                            }
                            sceneView.LookAtDirect((tempVec / Selection.transforms.Length), sceneView.camera.transform.rotation);
                        }
                        break;
                }
            }
 
        tick++;
 
    }
}

Back to editor scripts

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox