Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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;
}
}

Expand All @@ -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;
}
}
}
Expand Down