diff --git a/Plugins/AssetUsageDetector/Editor/SearchResultTooltip.cs b/Plugins/AssetUsageDetector/Editor/SearchResultTooltip.cs index 9785717..ec6fa1d 100644 --- a/Plugins/AssetUsageDetector/Editor/SearchResultTooltip.cs +++ b/Plugins/AssetUsageDetector/Editor/SearchResultTooltip.cs @@ -26,11 +26,20 @@ namespace AssetUsageDetectorNamespace public static void Show( Rect sourcePosition, string tooltip ) { + // Don't lose focus to the previous window + EditorWindow prevFocusedWindow = focusedWindow; + + if (!mainWindow) + { + mainWindow = CreateInstance(); + mainWindow.ShowPopup(); + } + Vector2 preferredSize = Style.CalcSize( new GUIContent( tooltip ) ) + Style.contentOffset + new Vector2( Style.padding.horizontal + Style.margin.horizontal, Style.padding.vertical + Style.margin.vertical ); Rect preferredPosition; Rect positionLeft = new Rect( sourcePosition.position - new Vector2( preferredSize.x, 0f ), preferredSize ); - Rect screenFittedPositionLeft = Utilities.GetScreenFittedRect( positionLeft ); + Rect screenFittedPositionLeft = Utilities.GetScreenFittedRect(positionLeft, mainWindow); Vector2 positionOffset = positionLeft.position - screenFittedPositionLeft.position; Vector2 sizeOffset = positionLeft.size - screenFittedPositionLeft.size; @@ -39,7 +48,7 @@ namespace AssetUsageDetectorNamespace else { Rect positionRight = new Rect( sourcePosition.position + new Vector2( sourcePosition.width, 0f ), preferredSize ); - Rect screenFittedPositionRight = Utilities.GetScreenFittedRect( positionRight ); + Rect screenFittedPositionRight = Utilities.GetScreenFittedRect(positionRight, mainWindow); Vector2 positionOffset2 = positionRight.position - screenFittedPositionRight.position; Vector2 sizeOffset2 = positionRight.size - screenFittedPositionRight.size; @@ -49,15 +58,6 @@ namespace AssetUsageDetectorNamespace preferredPosition = screenFittedPositionLeft; } - // Don't lose focus to the previous window - EditorWindow prevFocusedWindow = focusedWindow; - - if( !mainWindow ) - { - mainWindow = CreateInstance(); - mainWindow.ShowPopup(); - } - SearchResultTooltip.tooltip = tooltip; mainWindow.minSize = preferredPosition.size; mainWindow.position = preferredPosition; diff --git a/Plugins/AssetUsageDetector/Editor/SearchResultTreeView.cs b/Plugins/AssetUsageDetector/Editor/SearchResultTreeView.cs index 21fe8fa..5388686 100644 --- a/Plugins/AssetUsageDetector/Editor/SearchResultTreeView.cs +++ b/Plugins/AssetUsageDetector/Editor/SearchResultTreeView.cs @@ -1311,7 +1311,7 @@ namespace AssetUsageDetectorNamespace if( preferredHeight > 10f ) { Vector2 size = position.size; - position = Utilities.GetScreenFittedRect( new Rect( GUIUtility.GUIToScreenPoint( Event.current.mousePosition ) + new Vector2( size.x * -0.5f, 10f ), size ) ); + position = Utilities.GetScreenFittedRect(new Rect(GUIUtility.GUIToScreenPoint(Event.current.mousePosition) + new Vector2(size.x * -0.5f, 10f), size), this); shouldRepositionSelf = false; GUIUtility.ExitGUI(); diff --git a/Plugins/AssetUsageDetector/Editor/Utilities.cs b/Plugins/AssetUsageDetector/Editor/Utilities.cs index 388f088..f6f4105 100644 --- a/Plugins/AssetUsageDetector/Editor/Utilities.cs +++ b/Plugins/AssetUsageDetector/Editor/Utilities.cs @@ -43,7 +43,9 @@ namespace AssetUsageDetectorNamespace private static readonly string reflectionNamespace = typeof( Assembly ).Namespace; private static readonly string nativeCollectionsNamespace = typeof( NativeArray ).Namespace; - private static MethodInfo screenFittedRectGetter; + private static MethodInfo screenFittedRectGetter; + private static FieldInfo editorWindowHostViewGetter; + private static PropertyInfo hostViewContainerWindowGetter; private static readonly Func prefabHasAnyOverridesGetter = (Func) Delegate.CreateDelegate( typeof( Func ), typeof( PrefabUtility ).GetMethod( "HasObjectOverride", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ) ); @@ -494,14 +496,25 @@ namespace AssetUsageDetectorNamespace GUILayout.Space( 4f ); } - // Restricts the given Rect within the screen's bounds - public static Rect GetScreenFittedRect( Rect originalRect ) - { - if( screenFittedRectGetter == null ) - screenFittedRectGetter = typeof( EditorWindow ).Assembly.GetType( "UnityEditor.ContainerWindow" ).GetMethod( "FitRectToScreen", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ); + /// + /// Restricts the given Rect within the screen's bounds. + /// + public static Rect GetScreenFittedRect(Rect originalRect, EditorWindow editorWindow) + { + screenFittedRectGetter ??= typeof(EditorWindow).Assembly.GetType("UnityEditor.ContainerWindow").GetMethod("FitRectToScreen", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); - return (Rect) screenFittedRectGetter.Invoke( null, new object[3] { originalRect, true, true } ); - } + if (screenFittedRectGetter.GetParameters().Length == 3) + return (Rect)screenFittedRectGetter.Invoke(null, new object[] { originalRect, true, true }); + else + { + // New version introduced in Unity 2022.3.62f1, Unity 6.0.49f1 and Unity 6.1.0f1. + // Usage example: https://github.com/Unity-Technologies/UnityCsReference/blob/10f8718268a7e34844ba7d59792117c28d75a99b/Editor/Mono/EditorWindow.cs#L1264 + editorWindowHostViewGetter ??= typeof(EditorWindow).GetField("m_Parent", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + hostViewContainerWindowGetter ??= typeof(EditorWindow).Assembly.GetType("UnityEditor.HostView").GetProperty("window", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + + return (Rect)screenFittedRectGetter.Invoke(null, new object[] { originalRect, originalRect.center, true, hostViewContainerWindowGetter.GetValue(editorWindowHostViewGetter.GetValue(editorWindow), null) }); + } + } // Check if all the objects inside the list are null public static bool IsEmpty( this List objectsToSearch ) diff --git a/Plugins/AssetUsageDetector/README.txt b/Plugins/AssetUsageDetector/README.txt index ef2380d..c63aa0a 100644 --- a/Plugins/AssetUsageDetector/README.txt +++ b/Plugins/AssetUsageDetector/README.txt @@ -1,4 +1,4 @@ -= Asset Usage Detector (v2.5.6) = += Asset Usage Detector (v2.5.7) = Documentation: https://github.com/yasirkula/UnityAssetUsageDetector E-mail: yasirkula@gmail.com \ No newline at end of file diff --git a/package.json b/package.json index dd355e7..e79681e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.yasirkula.assetusagedetector", "displayName": "Asset Usage Detector", - "version": "2.5.6", + "version": "2.5.7", "documentationUrl": "https://github.com/yasirkula/UnityAssetUsageDetector", "changelogUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/releases", "licensesUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/blob/master/LICENSE.txt",