Skip to content

Instantly share code, notes, and snippets.

@WooshiiDev
Created August 17, 2020 13:06
Show Gist options
  • Save WooshiiDev/9e0d237334d27cf370c0ca4ad49c2f2a to your computer and use it in GitHub Desktop.
Save WooshiiDev/9e0d237334d27cf370c0ca4ad49c2f2a to your computer and use it in GitHub Desktop.
Simple Blender Like Camera Controls in the Scene View
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public static class SceneViewCameraHandler
{
public class SceneInfo
{
public SceneView view;
public Camera camera;
}
private static Event e;
private static float rotValue = 15f;
private static SceneInfo scene = new SceneInfo();
private static SceneView view => scene.view;
static SceneViewCameraHandler()
{
SceneView.duringSceneGui -= AddCameraControls;
SceneView.duringSceneGui += AddCameraControls;
}
private static void AddCameraControls(SceneView view)
{
// Set to curr view
scene.view = view;
scene.camera = view.camera;
//Get Event
e = Event.current;
if (e.type == EventType.KeyDown)
{
//Rotation
if (e.keyCode == KeyCode.Keypad5)
{
view.orthographic = !view.orthographic;
e.Use ();
}
//Snaps
DoKeyRotate (KeyCode.Keypad1, Quaternion.Euler (0, 0, 0));
DoKeyRotate (KeyCode.Keypad3, Quaternion.Euler (0, 90, 0));
DoKeyRotate (KeyCode.Keypad7, Quaternion.Euler (90, 0, 0));
DoKeyRotateRelative (KeyCode.Keypad9, Quaternion.Euler (0, 180, 0));
DoKeyRotateRelative (KeyCode.Keypad4, Quaternion.Euler (0, rotValue, 0));
DoKeyRotateRelative (KeyCode.Keypad6, Quaternion.Euler (0, -rotValue, 0));
DoKeyRotateRelative (KeyCode.Keypad8, Quaternion.Euler (rotValue, 0, 0));
DoKeyRotateRelative (KeyCode.Keypad2, Quaternion.Euler (-rotValue, 0, 0));
}
}
private static void DoKeyRotate(KeyCode keyCode, Quaternion rotation)
{
if (e.keyCode == keyCode)
view.LookAt (view.pivot, rotation);
}
private static void DoKeyRotateRelative(KeyCode keyCode, Quaternion rotation)
{
if (e.keyCode == keyCode)
view.LookAt (view.pivot, view.rotation * rotation);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment