From 6f025b2dc054bf1b3cc1ebb31eef598306b2d21c Mon Sep 17 00:00:00 2001 From: amirhakimnejad Date: Wed, 1 Mar 2023 17:48:23 +0330 Subject: [PATCH 1/2] Fix problem when you have a numerable active shims This solved an InvalidCast exception in Unity 2022.2.8 --- .../Scripts/Editor/ShimManagerProxy.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Packages/com.jagapippi.auto-screen/Scripts/Editor/ShimManagerProxy.cs b/Packages/com.jagapippi.auto-screen/Scripts/Editor/ShimManagerProxy.cs index 710c520..f4e6c54 100644 --- a/Packages/com.jagapippi.auto-screen/Scripts/Editor/ShimManagerProxy.cs +++ b/Packages/com.jagapippi.auto-screen/Scripts/Editor/ShimManagerProxy.cs @@ -43,7 +43,20 @@ static ShimManagerProxy() ); } - public static object GetActiveScreenShim() => ActiveScreenShimFieldInfo.GetValue(null); + public static object GetActiveScreenShim() + { + var activeScreenShim = ActiveScreenShimFieldInfo.GetValue(null); + if (activeScreenShim is System.Collections.IEnumerable enumerable) + { + var enumerator = enumerable.GetEnumerator(); + while (enumerator.MoveNext()) + { + activeScreenShim = enumerator.Current; + if (activeScreenShim.GetType().Name == "ScreenSimulation") break; + } + } + return activeScreenShim; + } // NOTE: ScreenSimulation#widthの値がおかしい場合があるのでScreenSimulation#Widthを参照する public static int width From 1f6a7124e1afa91fe3fc681a2071dc68e0827f65 Mon Sep 17 00:00:00 2001 From: amirhakimnejad Date: Sun, 12 Mar 2023 13:18:14 +0330 Subject: [PATCH 2/2] Fix InvalidCast on using Game instead of Simulator --- .../Scripts/Editor/ShimManagerProxy.cs | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/Packages/com.jagapippi.auto-screen/Scripts/Editor/ShimManagerProxy.cs b/Packages/com.jagapippi.auto-screen/Scripts/Editor/ShimManagerProxy.cs index f4e6c54..2087858 100644 --- a/Packages/com.jagapippi.auto-screen/Scripts/Editor/ShimManagerProxy.cs +++ b/Packages/com.jagapippi.auto-screen/Scripts/Editor/ShimManagerProxy.cs @@ -13,6 +13,13 @@ namespace Jagapippi.AutoScreen internal static class ShimManagerProxy { private const string AssemblyName = "UnityEditor.DeviceSimulatorModule"; +#if UNITY_2021_2_8_OR_NEWER + private const string WidthKey = "width"; + private const string HeightKey = "height"; +#else + private const string WidthKey = "Width"; + private const string HeightKey = "Height"; +#endif private static readonly Type ShimManagerType = Assembly.Load("UnityEngine.dll").GetType("UnityEngine.ShimManager"); private static readonly FieldInfo ActiveScreenShimFieldInfo = ShimManagerType.GetField("s_ActiveScreenShim", BindingFlags.Static | BindingFlags.NonPublic); @@ -26,21 +33,8 @@ static ShimManagerProxy() .Select(assembly => assembly.GetType("UnityEditor.DeviceSimulation.ScreenSimulation")) .First(); - WidthPropertyInfo = screenSimulationType.GetProperty( -#if UNITY_2021_2_8_OR_NEWER - "width" -#else - "Width" -#endif - ); - - HeightPropertyInfo = screenSimulationType.GetProperty( -#if UNITY_2021_2_8_OR_NEWER - "height" -#else - "Height" -#endif - ); + WidthPropertyInfo = screenSimulationType.GetProperty(WidthKey); + HeightPropertyInfo = screenSimulationType.GetProperty(HeightKey); } public static object GetActiveScreenShim() @@ -65,7 +59,7 @@ public static int width { var activeScreenShim = GetActiveScreenShim(); if (activeScreenShim == null) return UnityScreen.width; - return (int)WidthPropertyInfo.GetValue(activeScreenShim); + return activeScreenShim.GetType().GetProperty(WidthKey).GetValue(activeScreenShim) as int? ?? UnityScreen.width; } } @@ -76,7 +70,7 @@ public static int height { var activeScreenShim = GetActiveScreenShim(); if (activeScreenShim == null) return UnityScreen.height; - return (int)HeightPropertyInfo.GetValue(activeScreenShim); + return activeScreenShim.GetType().GetProperty(HeightKey).GetValue(activeScreenShim) as int? ?? UnityScreen.height; } } }