Advertisement
Chrisdbhr

Mouse hover highlight for project window working with old Input System

May 5th, 2023 (edited)
1,163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 KB | None | 0 0
  1. #if UNITY_EDITOR
  2.  
  3. using System;
  4. using UnityEditor;
  5. using UnityEngine;
  6.  
  7. #if ENABLE_INPUT_SYSTEM
  8. using UnityEngine.InputSystem;
  9. #endif
  10.  
  11. namespace Utility.EditorUtilities
  12. {
  13.     public static class ProjectWindowHoverUtility
  14.     {
  15.         private static Type ProjectWindowType = null;
  16.         private static Vector2 CachedMousePosition = default;
  17.         private static double LastUpdateTime = -3d;
  18.  
  19.         private static Color RectOutlineColour = new Color(1, 1, 1, 0.06f);
  20.  
  21.         private const string Project_Window_Type_String = "ProjectBrowser";
  22.         private const double Update_Frequency_Time = 2d;
  23.  
  24.         [InitializeOnLoadMethod]
  25.         private static void DisplayRectOverProjectWindowHoveredItem()
  26.         {
  27.             EditorApplication.update += HandleEditorUpdate;
  28.             EditorApplication.projectWindowItemOnGUI += DrawRectOverFileInProjectWindow;
  29.         }
  30.  
  31.         private static void HandleEditorUpdate()
  32.         {
  33.             if (ProjectWindowType != null)
  34.             {
  35.                 #if ENABLE_INPUT_SYSTEM
  36.                 Mouse mouse = Mouse.current;
  37.                 if (mouse == null) return;
  38.                 Vector2 mousePosition = mouse.position.value;
  39.                 #else
  40.                 Vector2 mousePosition = Input.mousePosition;
  41.                 #endif
  42.  
  43.                 if (Mathf.Approximately(mousePosition.x, CachedMousePosition.x)
  44.                     && Mathf.Approximately(mousePosition.y, CachedMousePosition.y)) return;
  45.                
  46.                 CachedMousePosition = mousePosition;
  47.  
  48.                 EditorWindow mouseOverWindow = EditorWindow.mouseOverWindow;
  49.                 if (mouseOverWindow == null) return;
  50.                 if (mouseOverWindow.GetType() != ProjectWindowType) return;
  51.  
  52.                 mouseOverWindow.Repaint();
  53.             }
  54.             else if (EditorApplication.timeSinceStartup > LastUpdateTime + Update_Frequency_Time)
  55.             {
  56.                 LastUpdateTime = EditorApplication.timeSinceStartup;
  57.                 foreach (EditorWindow window in Resources.FindObjectsOfTypeAll<EditorWindow>())
  58.                 {
  59.                     if (!window.GetType().Name.EndsWith(Project_Window_Type_String)) continue;
  60.  
  61.                     ProjectWindowType = window.GetType();
  62.                     break;
  63.                 }
  64.             }
  65.         }
  66.  
  67.         private static void DrawRectOverFileInProjectWindow(string guid, Rect selectionRect)
  68.         {
  69.             selectionRect.width += selectionRect.x;
  70.             selectionRect.x = 0;
  71.  
  72.             Vector2 mousePosition = Event.current.mousePosition;
  73.  
  74.             if (!selectionRect.Contains(mousePosition)) return;
  75.  
  76.             EditorGUI.DrawRect(selectionRect, RectOutlineColour);
  77.         }
  78.     }
  79. }
  80.  
  81. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement