Last active
June 14, 2025 18:34
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Hi @CheeryLee
Interesting snippet, but what do you use it for?
@AimarGonzalez
Our game supports changing screen orientation. When it runs inside default Unity game view, it's size is fixed to screen setting we set before launch (aspect or resolution). But when we change the game view to device simulator, we get a possibility to rotate the screen. And in this time the snippet comes to work - helps to determine what kind of view is working right now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to use with Device Simulator 3.0.0 and newer.