-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSceneViewPanner.cs
74 lines (65 loc) · 2.05 KB
/
SceneViewPanner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public class SceneViewPanner
{
static SceneViewPanner()
{
SceneView.duringSceneGui += OnSceneGUI;
}
static Vector2 lastMousePosition = Vector2.zero;
static bool isPanning = false;
static void OnSceneGUI(SceneView sceneView)
{
if (Event.current != null)
{
Event e = Event.current;
if (e.type == EventType.MouseDown && e.button == 2)
{
isPanning = true;
lastMousePosition = e.mousePosition;
e.Use();
}
else if (e.type == EventType.MouseUp && e.button == 2)
{
isPanning = false;
e.Use();
}
else if (e.type == EventType.MouseLeaveWindow)
{
isPanning = false;
e.Use();
}
if (isPanning)
{
Vector2 currentMousePosition = Event.current.mousePosition;
Vector2 delta = currentMousePosition - lastMousePosition;
delta.y = -delta.y;
lastMousePosition = currentMousePosition;
if (sceneView!= null)
{
PanSceneView(sceneView, delta);
}
}
}
}
static void PanSceneView(SceneView sceneView, Vector3 delta)
{
Camera cam = sceneView.camera;
float panSpeed = 0.004f;
if (cam.orthographic)
{
// Orthographic mode
Vector3 movement = cam.transform.right * -delta.x + cam.transform.up * -delta.y;
sceneView.pivot += movement * panSpeed * cam.orthographicSize;
}
else
{
// Perspective mode
float distance = Vector3.Distance(sceneView.pivot, cam.transform.position);
Vector3 movement = cam.transform.right * -delta.x + cam.transform.up * -delta.y;
sceneView.pivot += movement * panSpeed * distance;
}
sceneView.Repaint();
}
}