@@ -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 )
0 commit comments