Skip to content

Instantly share code, notes, and snippets.

@toxicFork
Forked from toxicFork/HighlightHelper.cs
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toxicFork/762c5b08f9a3de0e27bb to your computer and use it in GitHub Desktop.
Save toxicFork/762c5b08f9a3de0e27bb to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class HighlightHelper
{
private static 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 && !currentWindow.wantsMouseMove)
{
//allow the hierarchy window to use mouse move events!
currentWindow.wantsMouseMove = true;
}
}
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 = 0;
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment