-
Notifications
You must be signed in to change notification settings - Fork 5
/
HighlightHelper.cs
128 lines (104 loc) · 4.53 KB
/
HighlightHelper.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class HighlightHelper {
private static readonly Type HierarchyWindowType;
static HighlightHelper() {
EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGUI;
EditorApplication.update += EditorUpdate;
SceneView.onSceneGUIDelegate += OnSceneGUIDelegate;
Assembly editorAssembly = typeof (EditorWindow).Assembly;
HierarchyWindowType = editorAssembly.GetType("UnityEditor.SceneHierarchyWindow");
}
private static void EditorUpdate() {
var currentWindow = EditorWindow.mouseOverWindow;
if (currentWindow && currentWindow.GetType() == HierarchyWindowType) {
if (!currentWindow.wantsMouseMove) {
//allow the hierarchy window to use mouse move events!
currentWindow.wantsMouseMove = true;
}
} else {
_hoveredInstance = 0;
}
}
private static readonly Color HoverColor = new Color(1, 1, 1, 0.75f);
private static readonly Color DragColor = new Color(1f, 0, 0, 0.75f);
private static void OnSceneGUIDelegate(SceneView sceneView) {
switch (Event.current.type) {
case EventType.DragUpdated:
case EventType.DragPerform:
case EventType.DragExited:
sceneView.Repaint();
break;
}
if (Event.current.type == EventType.repaint) {
var drawnInstanceIDs = new HashSet<int>();
Color handleColor = Handles.color;
Handles.color = DragColor;
foreach (var objectReference in DragAndDrop.objectReferences) {
var gameObject = objectReference as GameObject;
if (gameObject && gameObject.activeInHierarchy) {
DrawObjectBounds(gameObject);
drawnInstanceIDs.Add(gameObject.GetInstanceID());
}
}
Handles.color = HoverColor;
if (_hoveredInstance != 0 && !drawnInstanceIDs.Contains(_hoveredInstance)) {
GameObject sceneGameObject = EditorUtility.InstanceIDToObject(_hoveredInstance) as GameObject;
if (sceneGameObject) {
DrawObjectBounds(sceneGameObject);
}
}
Handles.color = handleColor;
}
}
private static void DrawObjectBounds(GameObject sceneGameObject) {
var bounds = new Bounds(sceneGameObject.transform.position, Vector3.one);
foreach (var renderer in sceneGameObject.GetComponents<Renderer>()) {
Bounds rendererBounds = renderer.bounds;
rendererBounds.center = sceneGameObject.transform.position;
bounds.Encapsulate(renderer.bounds);
}
float onePixelOffset = HandleUtility.GetHandleSize(bounds.center)*1/64f;
float circleSize = bounds.size.magnitude*0.5f;
Handles.CircleCap(0, bounds.center,
sceneGameObject.transform.rotation, circleSize - onePixelOffset);
Handles.CircleCap(0, bounds.center,
sceneGameObject.transform.rotation, circleSize + onePixelOffset);
Handles.CircleCap(0, bounds.center, sceneGameObject.transform.rotation, circleSize);
}
private static int _hoveredInstance;
private static void HierarchyWindowItemOnGUI(int instanceID, Rect selectionRect) {
var current = Event.current;
switch (current.type) {
case EventType.mouseMove:
if (selectionRect.Contains(current.mousePosition)) {
if (_hoveredInstance != instanceID) {
_hoveredInstance = instanceID;
if (SceneView.lastActiveSceneView) {
SceneView.lastActiveSceneView.Repaint();
}
}
} else {
if (_hoveredInstance == instanceID) {
_hoveredInstance = 0;
if (SceneView.lastActiveSceneView) {
SceneView.lastActiveSceneView.Repaint();
}
}
}
break;
case EventType.MouseDrag:
case EventType.DragUpdated:
case EventType.DragPerform:
case EventType.DragExited:
if (SceneView.lastActiveSceneView) {
SceneView.lastActiveSceneView.Repaint();
}
break;
}
}
}