diff --git a/Packages/com.jagapippi.auto-screen/Scripts/Editor/ShimManagerProxy.cs b/Packages/com.jagapippi.auto-screen/Scripts/Editor/ShimManagerProxy.cs index 710c520..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,24 +33,24 @@ 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() => 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 @@ -52,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; } } @@ -63,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; } } }