1
1
// **************************************************************** //
2
2
//
3
3
// Copyright (c) RimuruDev. All rights reserved.
4
- // Contact me:
5
-
4
+ // Contact me:
5
+
6
6
// - LinkedIn: https://www.linkedin.com/in/rimuru/
7
- // - GitHub: https://github.com/RimuruDev
7
+ // - GitHub: https://github.com/RimuruDev
8
8
//
9
9
// **************************************************************** //
10
10
11
11
using System ;
12
+ using System . Linq ;
12
13
using UnityEngine ;
13
14
14
15
#if UNITY_EDITOR
15
16
using UnityEditor ;
16
- using UnityEditor . DeviceSimulation ;
17
17
#endif
18
18
19
19
namespace RimuruDev
20
20
{
21
- [ Flags ]
22
21
[ Serializable ]
23
22
public enum CurrentDeviceType : byte
24
23
{
25
- WebPC = 0 ,
26
- WebMobile = 2 ,
24
+ None = 0 ,
25
+ WebPC = 2 ,
26
+ WebMobile = 4 ,
27
27
}
28
28
29
29
[ SelectionBase ]
@@ -32,14 +32,31 @@ public enum CurrentDeviceType : byte
32
32
[ HelpURL ( "https://github.com/RimuruDev/Unity-WEBGL-DeviceTypeDetector" ) ]
33
33
public sealed class DeviceTypeDetector : MonoBehaviour
34
34
{
35
+ #if UNITY_EDITOR
36
+ private const string WINDOW_TITLE_SIMULATOR = "Simulator" ;
37
+ private const string WINDOW_TITLE_SIMULATOR_DEVICE = "Simulator Device" ;
38
+ #endif
35
39
[ field: SerializeField ] public CurrentDeviceType CurrentDeviceType { get ; private set ; }
36
40
37
41
#if UNITY_2020_1_OR_NEWER
38
42
[ SerializeField ] private bool enableDeviceSimulator = true ;
39
43
#endif
44
+
40
45
private void Awake ( )
41
46
{
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 ( ) )
43
60
{
44
61
Debug . Log ( "WEBGL -> Mobile" ) ;
45
62
CurrentDeviceType = CurrentDeviceType . WebMobile ;
@@ -49,36 +66,19 @@ private void Awake()
49
66
Debug . Log ( "WEBGL -> PC" ) ;
50
67
CurrentDeviceType = CurrentDeviceType . WebPC ;
51
68
}
69
+ #endif
52
70
}
53
71
54
72
#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 ) ;
60
79
#endif
61
- return false ;
62
- }
63
80
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
82
82
[ System . Runtime . InteropServices . DllImport ( "__Internal" ) ]
83
83
public static extern bool IsMobile ( ) ;
84
84
#endif
0 commit comments