Skip to content

Commit 790a508

Browse files
committed
Fixed GetScreenFittedRect not working on Unity 2022.3.62+, 6.0.49+ and 6.1.0+
1 parent d6a03aa commit 790a508

5 files changed

Lines changed: 36 additions & 23 deletions

File tree

Plugins/AssetUsageDetector/Editor/SearchResultTooltip.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,20 @@ internal static GUIStyle Style
2626

2727
public static void Show( Rect sourcePosition, string tooltip )
2828
{
29+
// Don't lose focus to the previous window
30+
EditorWindow prevFocusedWindow = focusedWindow;
31+
32+
if (!mainWindow)
33+
{
34+
mainWindow = CreateInstance<SearchResultTooltip>();
35+
mainWindow.ShowPopup();
36+
}
37+
2938
Vector2 preferredSize = Style.CalcSize( new GUIContent( tooltip ) ) + Style.contentOffset + new Vector2( Style.padding.horizontal + Style.margin.horizontal, Style.padding.vertical + Style.margin.vertical );
3039
Rect preferredPosition;
3140

3241
Rect positionLeft = new Rect( sourcePosition.position - new Vector2( preferredSize.x, 0f ), preferredSize );
33-
Rect screenFittedPositionLeft = Utilities.GetScreenFittedRect( positionLeft );
42+
Rect screenFittedPositionLeft = Utilities.GetScreenFittedRect(positionLeft, mainWindow);
3443

3544
Vector2 positionOffset = positionLeft.position - screenFittedPositionLeft.position;
3645
Vector2 sizeOffset = positionLeft.size - screenFittedPositionLeft.size;
@@ -39,7 +48,7 @@ public static void Show( Rect sourcePosition, string tooltip )
3948
else
4049
{
4150
Rect positionRight = new Rect( sourcePosition.position + new Vector2( sourcePosition.width, 0f ), preferredSize );
42-
Rect screenFittedPositionRight = Utilities.GetScreenFittedRect( positionRight );
51+
Rect screenFittedPositionRight = Utilities.GetScreenFittedRect(positionRight, mainWindow);
4352

4453
Vector2 positionOffset2 = positionRight.position - screenFittedPositionRight.position;
4554
Vector2 sizeOffset2 = positionRight.size - screenFittedPositionRight.size;
@@ -49,15 +58,6 @@ public static void Show( Rect sourcePosition, string tooltip )
4958
preferredPosition = screenFittedPositionLeft;
5059
}
5160

52-
// Don't lose focus to the previous window
53-
EditorWindow prevFocusedWindow = focusedWindow;
54-
55-
if( !mainWindow )
56-
{
57-
mainWindow = CreateInstance<SearchResultTooltip>();
58-
mainWindow.ShowPopup();
59-
}
60-
6161
SearchResultTooltip.tooltip = tooltip;
6262
mainWindow.minSize = preferredPosition.size;
6363
mainWindow.position = preferredPosition;

Plugins/AssetUsageDetector/Editor/SearchResultTreeView.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ private void OnGUI()
13111311
if( preferredHeight > 10f )
13121312
{
13131313
Vector2 size = position.size;
1314-
position = Utilities.GetScreenFittedRect( new Rect( GUIUtility.GUIToScreenPoint( Event.current.mousePosition ) + new Vector2( size.x * -0.5f, 10f ), size ) );
1314+
position = Utilities.GetScreenFittedRect(new Rect(GUIUtility.GUIToScreenPoint(Event.current.mousePosition) + new Vector2(size.x * -0.5f, 10f), size), this);
13151315

13161316
shouldRepositionSelf = false;
13171317
GUIUtility.ExitGUI();

Plugins/AssetUsageDetector/Editor/Utilities.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public static class Utilities
4343
private static readonly string reflectionNamespace = typeof( Assembly ).Namespace;
4444
private static readonly string nativeCollectionsNamespace = typeof( NativeArray<int> ).Namespace;
4545

46-
private static MethodInfo screenFittedRectGetter;
46+
private static MethodInfo screenFittedRectGetter;
47+
private static FieldInfo editorWindowHostViewGetter;
48+
private static PropertyInfo hostViewContainerWindowGetter;
4749

4850
private static readonly Func<Object, bool, bool> prefabHasAnyOverridesGetter = (Func<Object, bool, bool>) Delegate.CreateDelegate( typeof( Func<Object, bool, bool> ), typeof( PrefabUtility ).GetMethod( "HasObjectOverride", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ) );
4951

@@ -494,14 +496,25 @@ public static void DrawSeparatorLine()
494496
GUILayout.Space( 4f );
495497
}
496498

497-
// Restricts the given Rect within the screen's bounds
498-
public static Rect GetScreenFittedRect( Rect originalRect )
499-
{
500-
if( screenFittedRectGetter == null )
501-
screenFittedRectGetter = typeof( EditorWindow ).Assembly.GetType( "UnityEditor.ContainerWindow" ).GetMethod( "FitRectToScreen", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static );
502-
503-
return (Rect) screenFittedRectGetter.Invoke( null, new object[3] { originalRect, true, true } );
504-
}
499+
/// <summary>
500+
/// Restricts the given Rect within the screen's bounds.
501+
/// </summary>
502+
public static Rect GetScreenFittedRect(Rect originalRect, EditorWindow editorWindow)
503+
{
504+
screenFittedRectGetter ??= typeof(EditorWindow).Assembly.GetType("UnityEditor.ContainerWindow").GetMethod("FitRectToScreen", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
505+
506+
if (screenFittedRectGetter.GetParameters().Length == 3)
507+
return (Rect)screenFittedRectGetter.Invoke(null, new object[] { originalRect, true, true });
508+
else
509+
{
510+
// New version introduced in Unity 2022.3.62f1, Unity 6.0.49f1 and Unity 6.1.0f1.
511+
// Usage example: https://github.com/Unity-Technologies/UnityCsReference/blob/10f8718268a7e34844ba7d59792117c28d75a99b/Editor/Mono/EditorWindow.cs#L1264
512+
editorWindowHostViewGetter ??= typeof(EditorWindow).GetField("m_Parent", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
513+
hostViewContainerWindowGetter ??= typeof(EditorWindow).Assembly.GetType("UnityEditor.HostView").GetProperty("window", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
514+
515+
return (Rect)screenFittedRectGetter.Invoke(null, new object[] { originalRect, originalRect.center, true, hostViewContainerWindowGetter.GetValue(editorWindowHostViewGetter.GetValue(editorWindow), null) });
516+
}
517+
}
505518

506519
// Check if all the objects inside the list are null
507520
public static bool IsEmpty( this List<ObjectToSearch> objectsToSearch )
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
= Asset Usage Detector (v2.5.6) =
1+
= Asset Usage Detector (v2.5.7) =
22

33
Documentation: https://github.com/yasirkula/UnityAssetUsageDetector
44
E-mail: yasirkula@gmail.com

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.yasirkula.assetusagedetector",
33
"displayName": "Asset Usage Detector",
4-
"version": "2.5.6",
4+
"version": "2.5.7",
55
"documentationUrl": "https://github.com/yasirkula/UnityAssetUsageDetector",
66
"changelogUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/releases",
77
"licensesUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/blob/master/LICENSE.txt",

0 commit comments

Comments
 (0)