11// **************************************************************** //
22//
33// Copyright (c) RimuruDev. All rights reserved.
4- // Contact me:
5- // - Gmail: rimuru.dev@gmail.com
4+ // Contact me:
5+ // - Gmail: rimuru.dev@gmail.com
66// - LinkedIn: https://www.linkedin.com/in/rimuru/
7- // - GitHub: https://github.com/RimuruDev
7+ // - GitHub: https://github.com/RimuruDev
88//
99// **************************************************************** //
1010
1111using System ;
12+ using System . Linq ;
1213using UnityEngine ;
1314
1415#if UNITY_EDITOR
1516using UnityEditor ;
16- using UnityEditor . DeviceSimulation ;
1717#endif
1818
1919namespace RimuruDev
2020{
21- [ Flags ]
2221 [ Serializable ]
2322 public enum CurrentDeviceType : byte
2423 {
25- WebPC = 0 ,
26- WebMobile = 2 ,
24+ None = 0 ,
25+ WebPC = 2 ,
26+ WebMobile = 4 ,
2727 }
2828
2929 [ SelectionBase ]
@@ -32,14 +32,31 @@ public enum CurrentDeviceType : byte
3232 [ HelpURL ( "https://github.com/RimuruDev/Unity-WEBGL-DeviceTypeDetector" ) ]
3333 public sealed class DeviceTypeDetector : MonoBehaviour
3434 {
35+ #if UNITY_EDITOR
36+ private const string WINDOW_TITLE_SIMULATOR = "Simulator" ;
37+ private const string WINDOW_TITLE_SIMULATOR_DEVICE = "Simulator Device" ;
38+ #endif
3539 [ field: SerializeField ] public CurrentDeviceType CurrentDeviceType { get ; private set ; }
3640
3741#if UNITY_2020_1_OR_NEWER
3842 [ SerializeField ] private bool enableDeviceSimulator = true ;
3943#endif
44+
4045 private void Awake ( )
4146 {
42- if ( IsMobile ( ) && enableDeviceSimulator )
47+ #if UNITY_EDITOR
48+ if ( IsSimulatorWindowOpen ( ) && enableDeviceSimulator )
49+ {
50+ Debug . Log ( "WEBGL -> Mobile" ) ;
51+ CurrentDeviceType = CurrentDeviceType . WebMobile ;
52+ }
53+ else
54+ {
55+ Debug . Log ( "WEBGL -> PC" ) ;
56+ CurrentDeviceType = CurrentDeviceType . WebPC ;
57+ }
58+ #else
59+ if ( IsMobile ( ) )
4360 {
4461 Debug . Log ( "WEBGL -> Mobile" ) ;
4562 CurrentDeviceType = CurrentDeviceType . WebMobile ;
@@ -49,36 +66,19 @@ private void Awake()
4966 Debug . Log ( "WEBGL -> PC" ) ;
5067 CurrentDeviceType = CurrentDeviceType . WebPC ;
5168 }
69+ #endif
5270 }
5371
5472#if UNITY_EDITOR
55- public static bool IsMobile ( )
56- {
57- #if UNITY_2020_1_OR_NEWER
58- if ( DeviceSimulatorExists ( ) && IsDeviceSimulationActive ( ) )
59- return true ;
73+ private static bool IsSimulatorWindowOpen ( ) =>
74+ Resources
75+ . FindObjectsOfTypeAll < EditorWindow > ( )
76+ . Any ( window => window . titleContent . text
77+ is WINDOW_TITLE_SIMULATOR
78+ or WINDOW_TITLE_SIMULATOR_DEVICE ) ;
6079#endif
61- return false ;
62- }
6380
64- private static bool DeviceSimulatorExists ( )
65- {
66- var simulatorType = typeof ( Editor ) . Assembly . GetType ( "UnityEditor.DeviceSimulation.DeviceSimulator" ) ;
67- return simulatorType != null ;
68- }
69-
70- private static bool IsDeviceSimulationActive ( )
71- {
72- var simulatorType = typeof ( Editor ) . Assembly . GetType ( "UnityEditor.DeviceSimulation.DeviceSimulator" ) ;
73- if ( simulatorType != null )
74- {
75- var simulatorInstance = simulatorType . GetProperty ( "instance" , System . Reflection . BindingFlags . Static | System . Reflection . BindingFlags . Public ) ? . GetValue ( null ) ;
76- var isDeviceActive = simulatorType . GetProperty ( "isDeviceActive" , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) ? . GetValue ( simulatorInstance ) ;
77- return ( bool ) isDeviceActive ;
78- }
79- return false ;
80- }
81- #else
81+ #if ! UNITY_EDITOR
8282 [ System . Runtime . InteropServices . DllImport ( "__Internal" ) ]
8383 public static extern bool IsMobile ( ) ;
8484#endif
0 commit comments