Created
April 26, 2020 21:03
-
-
Save iszn11/b632f27e5cfc9bd0d089f41f5b969aed to your computer and use it in GitHub Desktop.
Proof of concept. Put in Editor folder and toggle in menu (Tools/Toggle XZ flight).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEditor; | |
using UnityEngine; | |
[InitializeOnLoad] | |
public static class SceneViewXZFlight | |
{ | |
private static readonly float SlowSpeed = 0.5f; | |
private static readonly float FastSpeed = 2.0f; | |
private static bool movingCamera = false; | |
private static Vector3 v = Vector3.zero; | |
private static bool Enabled { | |
get => EditorPrefs.GetBool("SceneViewXZFlight", false); | |
set => EditorPrefs.SetBool("SceneViewXZFlight", value); | |
} | |
static SceneViewXZFlight() | |
{ | |
SceneView.duringSceneGui -= OnSceneGUI; | |
SceneView.duringSceneGui += OnSceneGUI; | |
} | |
[MenuItem("Tools/Toggle XZ flight")] | |
public static void Toggle() | |
{ | |
Enabled = !Enabled; | |
} | |
private static void OnSceneGUI(SceneView sceneView) | |
{ | |
if (!Enabled) return; | |
Event e = Event.current; | |
switch (e.type) | |
{ | |
case EventType.MouseDown: | |
OnMouseDown(e); | |
break; | |
case EventType.MouseUp: | |
OnMouseUp(e); | |
break; | |
case EventType.KeyDown: | |
OnKeyDown(e); | |
break; | |
case EventType.KeyUp: | |
OnKeyUp(e); | |
break; | |
case EventType.Repaint: | |
OnRepaint(sceneView, e); | |
break; | |
case EventType.MouseLeaveWindow: | |
OnMouseLeave(e); | |
break; | |
case EventType.MouseDrag: | |
OnMouseDrag(sceneView, e); | |
break; | |
} | |
} | |
private static void OnMouseDown(Event e) | |
{ | |
if (e.button == 1) | |
{ | |
movingCamera = true; | |
v = Vector3.zero; | |
e.Use(); | |
} | |
} | |
private static void OnKeyDown(Event e) | |
{ | |
switch (e.keyCode) | |
{ | |
case KeyCode.W: | |
v.z = 1.0f; | |
break; | |
case KeyCode.S: | |
v.z = -1.0f; | |
break; | |
case KeyCode.A: | |
v.x = -1.0f; | |
break; | |
case KeyCode.D: | |
v.x = 1.0f; | |
break; | |
case KeyCode.Q: | |
v.y = -1.0f; | |
break; | |
case KeyCode.E: | |
v.y = 1.0f; | |
break; | |
} | |
} | |
private static void OnKeyUp(Event e) | |
{ | |
switch (e.keyCode) | |
{ | |
case KeyCode.W: | |
case KeyCode.S: | |
v.z = .0f; | |
break; | |
case KeyCode.A: | |
case KeyCode.D: | |
v.x = 0.0f; | |
break; | |
case KeyCode.Q: | |
case KeyCode.E: | |
v.y = 0.0f; | |
break; | |
} | |
} | |
private static void OnMouseDrag(SceneView sceneView, Event e) | |
{ | |
if (!movingCamera) return; | |
Camera c = sceneView.camera; | |
Transform t = c.transform; | |
Vector3 pivot = sceneView.pivot; | |
Vector3 cameraPosition = t.position; | |
Vector3 cameraToPivot = pivot - cameraPosition; | |
Vector3 cameraRight = t.right; | |
Vector2 mouseD = e.delta; | |
Quaternion qy = Quaternion.Euler(0.0f, mouseD.x * Time.deltaTime, 0.0f); | |
Quaternion qx = Quaternion.AngleAxis(mouseD.y * Time.deltaTime, cameraRight); | |
Quaternion q = qx * qy; | |
cameraToPivot = q * cameraToPivot; | |
Quaternion rotationToPivot = Quaternion.LookRotation(cameraToPivot); | |
pivot = cameraPosition + cameraToPivot; | |
sceneView.pivot = pivot; | |
sceneView.rotation = rotationToPivot; | |
sceneView.Repaint(); | |
} | |
private static void OnRepaint(SceneView sceneView, Event e) | |
{ | |
if (!movingCamera) return; | |
Camera c = sceneView.camera; | |
Transform t = c.transform; | |
Vector3 cameraRight = t.right; | |
Vector3 pivot = sceneView.pivot; | |
Vector3 forward = new Vector3(-cameraRight.z, 0.0f, cameraRight.x); | |
Quaternion q = Quaternion.LookRotation(forward); | |
float speed = e.shift ? FastSpeed : SlowSpeed; | |
pivot += (q * v) * speed * Time.deltaTime; | |
sceneView.pivot = pivot; | |
sceneView.Repaint(); | |
} | |
private static void OnMouseUp(Event e) | |
{ | |
if (e.button == 1) | |
{ | |
movingCamera = false; | |
e.Use(); | |
} | |
} | |
private static void OnMouseLeave(Event e) | |
{ | |
movingCamera = false; | |
e.Use(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment