Skip to content

Instantly share code, notes, and snippets.

@shahin-dev
Forked from toxicFork/HighlightHelper.cs
Last active August 29, 2015 14:13
Show Gist options
  • Save shahin-dev/ccb83814f16a121e176b to your computer and use it in GitHub Desktop.
Save shahin-dev/ccb83814f16a121e176b to your computer and use it in GitHub Desktop.

Revisions

  1. @toxicFork toxicFork revised this gist Jan 21, 2015. 1 changed file with 42 additions and 22 deletions.
    64 changes: 42 additions & 22 deletions HighlightHelper.cs
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,10 @@
    using UnityEngine;

    [InitializeOnLoad]
    public class HighlightHelper {
    static HighlightHelper() {
    public class HighlightHelper
    {
    static HighlightHelper()
    {
    EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGUI;

    SceneView.onSceneGUIDelegate += OnSceneGUIDelegate;
    @@ -13,37 +15,43 @@ static HighlightHelper() {
    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) {
    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) {
    if (Event.current.type == EventType.repaint)
    {
    var drawnInstanceIDs = new HashSet<int>();


    Color handleColor = Handles.color;

    Handles.color = DragColor;
    foreach (var objectReference in DragAndDrop.objectReferences) {
    foreach (var objectReference in DragAndDrop.objectReferences)
    {
    var gameObject = objectReference as GameObject;

    if (gameObject) {
    if (gameObject && gameObject.activeInHierarchy)
    {
    DrawObjectBounds(gameObject);

    drawnInstanceIDs.Add(gameObject.GetInstanceID());
    }
    }

    Handles.color = HoverColor;
    if (_hoveredInstance != 0 && !drawnInstanceIDs.Contains(_hoveredInstance)) {
    if (_hoveredInstance != 0 && !drawnInstanceIDs.Contains(_hoveredInstance))
    {
    GameObject sceneGameObject = EditorUtility.InstanceIDToObject(_hoveredInstance) as GameObject;

    if (sceneGameObject) {
    if (sceneGameObject)
    {
    DrawObjectBounds(sceneGameObject);
    }
    }
    @@ -52,9 +60,11 @@ private static void OnSceneGUIDelegate(SceneView sceneView) {
    }
    }

    private static void DrawObjectBounds(GameObject sceneGameObject) {
    private static void DrawObjectBounds(GameObject sceneGameObject)
    {
    var bounds = new Bounds(sceneGameObject.transform.position, Vector3.one);
    foreach (var renderer in sceneGameObject.GetComponents<Renderer>()) {
    foreach (var renderer in sceneGameObject.GetComponents<Renderer>())
    {
    Bounds rendererBounds = renderer.bounds;
    rendererBounds.center = sceneGameObject.transform.position;
    bounds.Encapsulate(renderer.bounds);
    @@ -74,22 +84,31 @@ private static void DrawObjectBounds(GameObject sceneGameObject) {

    private static int _hoveredInstance = 0;

    private static void HierarchyWindowItemOnGUI(int instanceID, Rect selectionRect) {
    private static void HierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
    {
    var current = Event.current;

    switch (current.type) {
    switch (current.type)
    {
    case EventType.repaint:
    if (selectionRect.Contains(current.mousePosition)) {
    if(_hoveredInstance != instanceID) {
    if (selectionRect.Contains(current.mousePosition))
    {
    if (_hoveredInstance != instanceID)
    {
    _hoveredInstance = instanceID;
    if (SceneView.lastActiveSceneView) {
    if (SceneView.lastActiveSceneView)
    {
    SceneView.lastActiveSceneView.Repaint();
    }
    }
    }
    } else {
    if (_hoveredInstance == instanceID) {
    }
    else
    {
    if (_hoveredInstance == instanceID)
    {
    _hoveredInstance = 0;
    if (SceneView.lastActiveSceneView) {
    if (SceneView.lastActiveSceneView)
    {
    SceneView.lastActiveSceneView.Repaint();
    }
    }
    @@ -99,7 +118,8 @@ private static void HierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
    case EventType.DragUpdated:
    case EventType.DragPerform:
    case EventType.DragExited:
    if (SceneView.lastActiveSceneView) {
    if (SceneView.lastActiveSceneView)
    {
    SceneView.lastActiveSceneView.Repaint();
    }
    break;
  2. @toxicFork toxicFork revised this gist Jan 21, 2015. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions HighlightHelper.cs
    Original file line number Diff line number Diff line change
    @@ -80,13 +80,18 @@ private static void HierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
    switch (current.type) {
    case EventType.repaint:
    if (selectionRect.Contains(current.mousePosition)) {
    _hoveredInstance = instanceID;
    if (SceneView.lastActiveSceneView) {
    SceneView.lastActiveSceneView.Repaint();
    if(_hoveredInstance != instanceID) {
    _hoveredInstance = instanceID;
    if (SceneView.lastActiveSceneView) {
    SceneView.lastActiveSceneView.Repaint();
    }
    }
    } else {
    if (_hoveredInstance == instanceID) {
    _hoveredInstance = 0;
    if (SceneView.lastActiveSceneView) {
    SceneView.lastActiveSceneView.Repaint();
    }
    }
    }
    break;
  3. @toxicFork toxicFork created this gist Jan 21, 2015.
    105 changes: 105 additions & 0 deletions HighlightHelper.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,105 @@
    using System.Collections.Generic;
    using UnityEditor;
    using UnityEngine;

    [InitializeOnLoad]
    public class HighlightHelper {
    static HighlightHelper() {
    EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGUI;

    SceneView.onSceneGUIDelegate += OnSceneGUIDelegate;
    }

    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) {
    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.repaint:
    if (selectionRect.Contains(current.mousePosition)) {
    _hoveredInstance = instanceID;
    if (SceneView.lastActiveSceneView) {
    SceneView.lastActiveSceneView.Repaint();
    }
    } else {
    if (_hoveredInstance == instanceID) {
    _hoveredInstance = 0;
    }
    }
    break;
    case EventType.MouseDrag:
    case EventType.DragUpdated:
    case EventType.DragPerform:
    case EventType.DragExited:
    if (SceneView.lastActiveSceneView) {
    SceneView.lastActiveSceneView.Repaint();
    }
    break;
    }

    EditorApplication.RepaintHierarchyWindow();
    }
    }