-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoxelEditor
More file actions
50 lines (45 loc) · 2.14 KB
/
VoxelEditor
File metadata and controls
50 lines (45 loc) · 2.14 KB
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
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Transform))]
public class VoxelEditor : Editor
{
// Called when you click any Transform in the scene
void OnEnable ()
{
// remove a possibly existing listener
SceneView.duringSceneGui -= OnScene;
// add a listener
SceneView.duringSceneGui += OnScene;
}
// In examples it is recommended to uninstall the delegate listener
// But we need [CustomEditor(typeof(Transform))] and this means Unity will call our Script whenever a Transform
// is getting selected. If we want to detect clicks anywhere (so that this listens to ALL OnScene calls) we simply
// keep the listener installed. This won't overflow because we uninstall listeners before installing new ones.
// But if your Editor ever crashes or throws errors this miiight be related. But I think it's pretty secure.
// And if you are fine with clicking on Transforms only then uncomment the code in OnDisable below.
// If you do, clicks in empty scene-areas are not detected.
private void OnDisable()
{
// SceneView.duringSceneGui -= OnScene;
}
private static void OnScene(SceneView sceneview)
{
// uncomment if you want to make sure it runs:
// Debug.Log("OnScene !");
if (Event.current.type == EventType.MouseDown)
{
// This kind of works, but results in wrong coordinates, maybe because of the Window offset or something.
//Ray ray = sceneview.camera.ScreenPointToRay(Event.current.mousePosition);
// This works fine as it gets you the correct coordinates
Vector2 mousePosition = Event.current.mousePosition;
Ray ray = HandleUtility.GUIPointToWorldRay(mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Debug.Log(hit.point.x.ToString());
// Draw a pointing upwards where we clicked. Just to make sure we SEE that the coordinates are correct.
Debug.DrawLine(hit.point, hit.point + Vector3.up * 0.3f, Color.red, 0.1f);
}
}
}
}