-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOSDeviceData.cs
More file actions
173 lines (162 loc) · 6.03 KB
/
Copy pathIOSDeviceData.cs
File metadata and controls
173 lines (162 loc) · 6.03 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System.Text.RegularExpressions;
using System;
using UnityEngine;
using System.Linq;
using SystemInfo = UnityEngine.Device.SystemInfo;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class IOSDeviceData {
const string SEPARATOR = ",";
const string PATTERN = SEPARATOR + "(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))";
const string DEVICE_NAME = "Device Name";
const string MODEL_NAMES = "Model Names";
const string RESOLUTION = "Resolution";
const string SCALE_FACTOR = "Scale Factor";
const string CORNER_RADIUS = "Corner Radius";
const string NOTCH_X = "Notch X";
const string NOTCH_Y = "Notch Y";
const string NOTCH_WIDTH = "Notch W";
const string NOTCH_HEIGHT = "Notch H";
const string NOTCH_CORNER_RADIUS = "Notch Corner Radius";
string deviceModel;
string deviceName;
bool deviceSupported;
Vector2Int resolution;
int cornerRadius;
Rect notchRect;
int notchCornerRadius;
static IOSDeviceData instance;
public static IOSDeviceData Instance {
get {
instance ??= new IOSDeviceData();
return instance;
}
}
public static string Name => Instance.deviceName;
public static int NotchHeight => (int) Instance.notchRect.yMax;
public static bool DeviceSupported => Instance.deviceSupported;
public static Vector2Int Resolution => Instance.resolution;
public static int CornerRadius => Instance.cornerRadius;
public static Rect NotchRect => Instance.notchRect;
public static int NotchCornerRadius => Instance.notchCornerRadius;
public IOSDeviceData() {
LoadData();
#if UNITY_EDITOR
EditorApplication.playModeStateChanged -= OnPlayModeChanged;
EditorApplication.playModeStateChanged += OnPlayModeChanged;
EditorApplication.update -= Update;
EditorApplication.update += Update;
#endif
}
#if UNITY_EDITOR
private void OnPlayModeChanged(PlayModeStateChange change) {
// Reset when exiting play mode
if (change != PlayModeStateChange.ExitingPlayMode) return;
EditorApplication.playModeStateChanged -= OnPlayModeChanged;
EditorApplication.update -= Update;
instance = null;
}
void Update() {
// Device model changed in simulator
if (SystemInfo.deviceModel != deviceModel) {
LoadData();
}
}
#endif
void LoadData() {
deviceModel = SystemInfo.deviceModel;
// If the device is not on the list, mark it as unsopported,
// in that case the data given by unity should be used instead
deviceSupported = false;
var textAsset = Resources.Load<TextAsset>("iOSDeviceData/DeviceData");
var csvParser = new Regex(PATTERN);
var rows = textAsset.text.Split('\n', '\r');
// Find indices for titles
var titles = csvParser.Split(rows[0]);
var deviceNameInd = 0;
var modelNamesInd = 1;
var resolutionInd = 2;
var scaleFactorInd = 3;
var cornerRadiusInd = 4;
var notchXInd = 5;
var notchYInd = 6;
var notchWInd = 7;
var notchHInd = 8;
var notchCornerRadiusInd = 9;
int maxIndex = 0;
for (int i = 0; i < titles.Length; i++) {
var value = titles[i];
switch (value) {
case DEVICE_NAME:
deviceNameInd = i;
maxIndex = i;
break;
case MODEL_NAMES:
modelNamesInd = i;
maxIndex = i;
break;
case RESOLUTION:
resolutionInd = i;
maxIndex = i;
break;
case SCALE_FACTOR:
scaleFactorInd = i;
maxIndex = i;
break;
case CORNER_RADIUS:
cornerRadiusInd = i;
maxIndex = i;
break;
case NOTCH_X:
notchXInd = i;
maxIndex = i;
break;
case NOTCH_Y:
notchYInd = i;
maxIndex = i;
break;
case NOTCH_WIDTH:
notchWInd = i;
maxIndex = i;
break;
case NOTCH_HEIGHT:
notchHInd = i;
maxIndex = i;
break;
case NOTCH_CORNER_RADIUS:
notchCornerRadiusInd = i;
maxIndex = i;
break;
}
}
// Look for current model
for (int r = 1; r < rows.Length; r++) {
if (!csvParser.IsMatch(rows[r])) continue;
var fields = csvParser.Split(rows[r]);
if (fields.Length <= maxIndex) continue;
var models = fields[modelNamesInd].Trim('"').Split(';');
if (models.Contains(SystemInfo.deviceModel)) {
deviceName = fields[deviceNameInd];
int.TryParse(fields[notchXInd], out var notchX);
int.TryParse(fields[notchYInd], out var notchY);
int.TryParse(fields[notchWInd], out var notchW);
int.TryParse(fields[notchHInd], out var notchH);
notchRect = new Rect(notchX, notchY, notchW, notchH);
var resolutionStr = fields[resolutionInd].Split('x', StringSplitOptions.RemoveEmptyEntries);
int.TryParse(resolutionStr[0], out var resX);
int.TryParse(resolutionStr[1], out var resY);
resolution = new Vector2Int(resX, resY);
int.TryParse(fields[cornerRadiusInd], out cornerRadius);
int.TryParse(fields[notchCornerRadiusInd], out notchCornerRadius);
deviceSupported = true;
#if UNITY_EDITOR
Debug.Log($"Device model: {deviceName} ({SystemInfo.deviceModel})");
#endif
break;
}
}
// Unload asset
Resources.UnloadAsset(textAsset);
}
}