Skip to content

Instantly share code, notes, and snippets.

@CheeryLee
Last active January 27, 2022 14:05
Show Gist options
  • Save CheeryLee/e8693948809d6e8b0ed4ecccaa778feb to your computer and use it in GitHub Desktop.
Save CheeryLee/e8693948809d6e8b0ed4ecccaa778feb to your computer and use it in GitHub Desktop.
PlayModeView wrapper for Unity that describes which type of Play Mode is currently active on the screen. Useful to determine between GameView and DeviceSimulator.
#if UNITY_EDITOR
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public static class PlayModeView
{
public enum Type
{
Unknown,
Game,
DeviceSimulator
}
private static readonly Version _version;
private static readonly bool _packageFound;
static PlayModeView()
{
_packageFound = TryGetPackageVersion(out _version);
}
public static Type GetCurrentViewType()
{
if (!_packageFound)
return Type.Game;
var editorAssembly = typeof(EditorWindow).Assembly;
var simulatorAssembly = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(i => i.GetName().Name == "Unity.DeviceSimulator.Editor");
var gameViewType = editorAssembly.GetType("UnityEditor.GameView");
var simulatorWindowType = simulatorAssembly?.GetType(GetSimulatorWindowType());
var gameViewWindow = Resources.FindObjectsOfTypeAll(gameViewType);
var simulatorViewWindow = simulatorAssembly != null ? Resources.FindObjectsOfTypeAll(simulatorWindowType) : null;
if (gameViewWindow.Length > 0)
return Type.Game;
if (simulatorViewWindow != null && simulatorViewWindow.Length > 0)
return Type.DeviceSimulator;
return Type.Unknown;
}
private static string GetSimulatorWindowType()
{
if (!_packageFound)
return string.Empty;
return _version >= new Version(3, 0, 0)
? "UnityEditor.DeviceSimulation.SimulatorWindow"
: "Unity.DeviceSimulator.SimulatorWindow";
}
private static bool TryGetPackageVersion(out Version version)
{
version = new Version();
var packagesPath = $"{Application.dataPath}/../Packages/manifest.json";
var versionGroup = "";
if (!File.Exists(packagesPath))
return false;
try
{
var packageField = File.ReadLines(packagesPath)
.FirstOrDefault(x => x.Contains("com.unity.device-simulator"));
if (string.IsNullOrEmpty(packageField))
return false;
var matches = Regex.Matches(packageField, "\".*?\"");
if (matches.Count == 0)
return false;
versionGroup = matches[matches.Count - 1].Value.Replace("\"", "");
var previewIndex = versionGroup.IndexOf("-preview", StringComparison.Ordinal);
if (previewIndex > -1)
versionGroup = versionGroup.Substring(0, previewIndex);
version = Version.Parse(versionGroup);
}
catch (Exception ex)
{
var message = string.IsNullOrEmpty(versionGroup)
? $"Can't get Device Simulator version (version group is {versionGroup}): {ex.Message}"
: $"Can't get Device Simulator version: {ex.Message}";
Debug.LogError(message);
return false;
}
return true;
}
}
#endif
@CheeryLee
Copy link
Author

Updated to use with Device Simulator 3.0.0 and newer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment