-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDeviceTypeDetector.cs
86 lines (78 loc) · 2.37 KB
/
DeviceTypeDetector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// **************************************************************** //
//
// Copyright (c) RimuruDev. All rights reserved.
// Contact me:
// - Gmail: rimuru.dev@gmail.com
// - LinkedIn: https://www.linkedin.com/in/rimuru/
// - GitHub: https://github.com/RimuruDev
//
// **************************************************************** //
using System;
using System.Linq;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace RimuruDev
{
[Serializable]
public enum CurrentDeviceType : byte
{
None = 0,
WebPC = 2,
WebMobile = 4,
}
[SelectionBase]
[DisallowMultipleComponent]
[DefaultExecutionOrder(-100)]
[HelpURL("https://github.com/RimuruDev/Unity-WEBGL-DeviceTypeDetector")]
public sealed class DeviceTypeDetector : MonoBehaviour
{
#if UNITY_EDITOR
private const string WINDOW_TITLE_SIMULATOR = "Simulator";
private const string WINDOW_TITLE_SIMULATOR_DEVICE = "Simulator Device";
#endif
[field: SerializeField] public CurrentDeviceType CurrentDeviceType { get; private set; }
#if UNITY_2020_1_OR_NEWER
[SerializeField] private bool enableDeviceSimulator = true;
#endif
private void Awake()
{
#if UNITY_EDITOR
if (IsSimulatorWindowOpen() && enableDeviceSimulator)
{
Debug.Log("WEBGL -> Mobile");
CurrentDeviceType = CurrentDeviceType.WebMobile;
}
else
{
Debug.Log("WEBGL -> PC");
CurrentDeviceType = CurrentDeviceType.WebPC;
}
#else
if (IsMobile())
{
Debug.Log("WEBGL -> Mobile");
CurrentDeviceType = CurrentDeviceType.WebMobile;
}
else
{
Debug.Log("WEBGL -> PC");
CurrentDeviceType = CurrentDeviceType.WebPC;
}
#endif
}
#if UNITY_EDITOR
private static bool IsSimulatorWindowOpen() =>
Resources
.FindObjectsOfTypeAll<EditorWindow>()
.Any(window => window.titleContent.text
is WINDOW_TITLE_SIMULATOR
or WINDOW_TITLE_SIMULATOR_DEVICE);
#endif
#if !UNITY_EDITOR
[System.Runtime.InteropServices.DllImport("__Internal")]
public static extern bool IsMobile();
#endif
}
}