Skip to content

Commit d5f7dd1

Browse files
authored
Merge pull request #19 from 06Games/patch-4
Partial support of notches + The debug scene displays the simulated information + Some other changes
2 parents 24a5799 + 2e0bcaa commit d5f7dd1

11 files changed

+170
-85
lines changed

DebugScene/DebugRect.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public void PlaceItselfAtScreenRect(Rect screenRect, Color color = default)
2424
RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRect, screenRect.position, Camera.main, out Vector2 localPoint);
2525
RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRect, screenRect.size, Camera.main, out Vector2 localSize);
2626

27-
localSize = localSize + (parentRect.sizeDelta /2);
27+
localSize = localSize + (parentRect.sizeDelta / 2);
2828

2929
debugText.text = $"{screenRect}";
30-
30+
3131
rectTransform.localPosition = localPoint;
3232
rectTransform.sizeDelta = localSize;
33-
33+
3434
}
3535
}

DebugScene/NotchSolutionDebugger.cs

+38-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections;
1+
using E7.NotchSolution;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Reflection;
@@ -20,29 +21,56 @@ void Update()
2021

2122
sb.AppendLine($"<b>-- PLEASE ROTATE THE DEVICE TO GET BOTH ORIENTATION'S DETAILS! --</b>\n");
2223

23-
sb.AppendLine($"Safe area : {Screen.safeArea}\n");
24-
PlaceRect(Screen.safeArea, Color.red);
24+
#if UNITY_EDITOR
25+
var safeArea = RealSize(NotchSolutionUtility.SimulateSafeAreaRelative);
26+
#else
27+
var safeArea = Screen.safeArea;
28+
#endif
29+
PlaceRect(safeArea, Color.red);
30+
if (Screen.orientation != NotchSolutionUtility.GetCurrentOrientation())
31+
safeArea.Set(Screen.width - safeArea.x, Screen.height - safeArea.y, safeArea.width, safeArea.height);
32+
sb.AppendLine($"Safe area : {safeArea}\n");
2533

2634
#if UNITY_2019_2_OR_NEWER
27-
sb.AppendLine($"Cutouts : {string.Join(" / ", Screen.cutouts.Select(x => x.ToString()))} \n");
28-
foreach (Rect r in Screen.cutouts)
35+
#if UNITY_EDITOR
36+
var relativeCutouts = NotchSolutionUtility.SimulateCutoutsRelative;
37+
List<Rect> rectCutouts = new List<Rect>();
38+
foreach (Rect rect in relativeCutouts) rectCutouts.Add(RealSize(rect));
39+
var cutouts = rectCutouts.ToArray();
40+
#else
41+
var cutouts = Screen.cutouts;
42+
#endif
43+
foreach (Rect r in cutouts) PlaceRect(r, Color.blue);
44+
45+
if (Screen.orientation != NotchSolutionUtility.GetCurrentOrientation())
2946
{
30-
PlaceRect(r, Color.blue);
47+
foreach (Rect rect in cutouts) rect.Set(Screen.width - rect.x, Screen.height - rect.y, rect.width, rect.height);
3148
}
49+
sb.AppendLine($"Cutouts : {string.Join(" / ", cutouts.Select(x => x.ToString()))} \n");
3250
#endif
3351

3452
sb.AppendLine($"Current resolution : {Screen.currentResolution}\n");
3553
sb.AppendLine($"All Resolutions : {string.Join(" / ", Screen.resolutions.Select(x => x.ToString()))}\n");
3654
sb.AppendLine($"DPI : {Screen.dpi} WxH : {Screen.width}x{Screen.height} Orientation : {Screen.orientation}\n");
37-
var joinedProps = string.Join( " / ", typeof(SystemInfo).GetProperties(BindingFlags.Public | BindingFlags.Static).Select( x => $"{x.Name} : {x.GetValue(null)}"));
55+
var joinedProps = string.Join(" / ", typeof(SystemInfo).GetProperties(BindingFlags.Public | BindingFlags.Static).Select(x => $"{x.Name} : {x.GetValue(null)}"));
3856
sb.AppendLine(joinedProps);
39-
debugText.text = sb.ToString();
57+
debugText.text = sb.ToString();
58+
}
59+
60+
Rect RealSize(Rect relative)
61+
{
62+
return new Rect(
63+
relative.x * Screen.width,
64+
relative.y * Screen.height,
65+
relative.width * Screen.width,
66+
relative.height * Screen.height
67+
);
4068
}
4169

4270
private List<DebugRect> debugRects = new List<DebugRect>();
4371
public void ClearRects()
4472
{
45-
foreach(var dbr in debugRects)
73+
foreach (var dbr in debugRects)
4674
{
4775
GameObject.Destroy(dbr.gameObject);
4876
}
@@ -55,8 +83,7 @@ public void PlaceRect(Rect rct, Color c)
5583
go.transform.localScale = Vector3.one;
5684
//go.transform.SetAsFirstSibling();
5785
var dbr = go.GetComponent<DebugRect>();
58-
dbr.PlaceItselfAtScreenRect(rct);
86+
dbr.PlaceItselfAtScreenRect(rct, c);
5987
debugRects.Add(dbr);
6088
}
61-
6289
}

Editor/EnumButtonsDrawer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
1818
}
1919
}
2020

21-
21+
2222
}

Editor/NotchSimulator.cs

+30-21
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Linq;
6-
using System.Reflection;
76
using UnityEditor;
87
using UnityEditor.Experimental.SceneManagement;
98
using UnityEditor.SceneManagement;
@@ -16,13 +15,29 @@ namespace E7.NotchSolution
1615
public class NotchSimulator : EditorWindow
1716
{
1817
internal static NotchSimulator win;
18+
Vector2 gameviewResolution;
1919

2020
[MenuItem("Window/General/Notch Simulator")]
2121
public static void ShowWindow()
2222
{
2323
win = (NotchSimulator)EditorWindow.GetWindow(typeof(NotchSimulator));
2424
win.titleContent = new GUIContent("Notch Simulator");
2525
}
26+
27+
[ExecuteInEditMode] private void OnEnable() { EditorApplication.update += RespawnMockup; }
28+
[ExecuteInEditMode] private void OnDisable() { EditorApplication.update -= RespawnMockup; }
29+
void RespawnMockup()
30+
{
31+
//When the game view is changed, the mockup sometimes disappears or isn't scaled correctly
32+
if (gameviewResolution != Handles.GetMainGameViewSize())
33+
{
34+
DestroyHiddenCanvas(); //So we delete the old canvas
35+
UpdateAllMockups(); //And we respawn it
36+
UpdateSimulatorTargets();
37+
gameviewResolution = Handles.GetMainGameViewSize(); //Update the saved game view
38+
}
39+
}
40+
2641
/// <summary>
2742
/// It is currently active only when Notch Simulator tab is present.
2843
/// </summary>
@@ -88,14 +103,16 @@ void OnGUI()
88103
internal static void UpdateSimulatorTargets()
89104
{
90105
var simulatedRect = NotchSimulatorUtility.enableSimulation ? NotchSimulatorUtility.SimulatorSafeAreaRelative : new Rect(0, 0, 1, 1);
106+
var simulatedCutouts = NotchSimulatorUtility.enableSimulation ? NotchSimulatorUtility.SimulatorCutoutsRelative : new Rect[0];
91107

92108
//This value could be used by the component statically.
93109
NotchSolutionUtility.SimulateSafeAreaRelative = simulatedRect;
110+
NotchSolutionUtility.SimulateCutoutsRelative = simulatedCutouts;
94111

95112
var normalSceneSimTargets = GameObject.FindObjectsOfType<UIBehaviour>().OfType<INotchSimulatorTarget>();
96113
foreach (var nst in normalSceneSimTargets)
97114
{
98-
nst.SimulatorUpdate(simulatedRect);
115+
nst.SimulatorUpdate(simulatedRect, simulatedCutouts);
99116
}
100117

101118
//Now find one in the prefab mode scene as well
@@ -105,7 +122,7 @@ internal static void UpdateSimulatorTargets()
105122
var prefabSceneSimTargets = prefabStage.stageHandle.FindComponentsOfType<UIBehaviour>().OfType<INotchSimulatorTarget>();
106123
foreach (var nst in prefabSceneSimTargets)
107124
{
108-
nst.SimulatorUpdate(simulatedRect);
125+
nst.SimulatorUpdate(simulatedRect, simulatedCutouts);
109126
}
110127
}
111128
}
@@ -177,7 +194,7 @@ internal static void UpdateAllMockups()
177194

178195
//Make the editing environment contains an another copy of mockup canvas.
179196
var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
180-
if(prefabStage != null)
197+
if (prefabStage != null)
181198
{
182199
EnsureCanvasAndEventSetup(prefabStage: prefabStage);
183200
}
@@ -202,11 +219,11 @@ internal static void UpdateAllMockups()
202219
{
203220
mockup.Show();
204221
mockup.SetMockupSprite(
205-
sprite: mockupSprite,
206-
orientation: NotchSimulatorUtility.GetGameViewOrientation(),
207-
simulate: enableSimulation,
208-
flipped: NotchSimulatorUtility.flipOrientation
209-
);
222+
sprite: mockupSprite,
223+
orientation: NotchSimulatorUtility.GetGameViewOrientation(),
224+
simulate: enableSimulation,
225+
flipped: NotchSimulatorUtility.flipOrientation
226+
);
210227
}
211228
}
212229
else
@@ -245,7 +262,7 @@ private static void EnsureCanvasAndEventSetup(PrefabStage prefabStage = null)
245262
else
246263
{
247264
var prefabGuids = AssetDatabase.FindAssets(mockupCanvasName);
248-
if(prefabGuids.Length == 0)
265+
if (prefabGuids.Length == 0)
249266
{
250267
return;
251268
}
@@ -266,17 +283,9 @@ private static void EnsureCanvasAndEventSetup(PrefabStage prefabStage = null)
266283
}
267284
}
268285

269-
270-
if (prefabMode)
271-
{
272-
canvasObject.PrefabStage = true;
273-
prefabMockupCanvas = canvasObject;
274-
}
275-
else
276-
{
277-
canvasObject.PrefabStage = false;
278-
mockupCanvas = canvasObject;
279-
}
286+
canvasObject.PrefabStage = prefabMode;
287+
if (prefabMode) prefabMockupCanvas = canvasObject;
288+
else mockupCanvas = canvasObject;
280289

281290
if (eventAdded == false)
282291
{

Editor/NotchSimulatorUtility.cs

+39-16
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,54 @@ internal static Rect SimulatorSafeAreaRelative
1414
get
1515
{
1616
var orientation = GetGameViewOrientation();
17-
var device = SimulationDatabase.db.ContainsKey(selectedDevice) ? selectedDevice : default;
17+
var device = SimulationDatabase.db.ContainsKey(selectedDevice) ? selectedDevice : default;
1818
var safe = orientation == ScreenOrientation.Landscape ? SimulationDatabase.db[device].landscapeSafeArea : SimulationDatabase.db[device].portraitSafeArea;
1919
var screenSize = SimulationDatabase.db[device].screenSize;
20-
if(orientation == ScreenOrientation.Landscape)
20+
if (orientation == ScreenOrientation.Landscape)
2121
{
2222
var swap = screenSize.x;
2323
screenSize.x = screenSize.y;
2424
screenSize.y = swap;
2525
}
26-
var relativeSafeArea = new Rect(safe.xMin / screenSize.x, safe.yMin / screenSize.y, safe.width / screenSize.x, safe.height / screenSize.y);
26+
return GetRelative(safe, screenSize);
27+
}
28+
}
2729

28-
if (NotchSimulatorUtility.flipOrientation)
29-
{
30-
return new Rect(
31-
1 - (relativeSafeArea.width + relativeSafeArea.xMin),
32-
1 - (relativeSafeArea.height + relativeSafeArea.yMin),
33-
relativeSafeArea.width,
34-
relativeSafeArea.height
35-
);
36-
}
37-
else
30+
internal static Rect[] SimulatorCutoutsRelative
31+
{
32+
get
33+
{
34+
var orientation = GetGameViewOrientation();
35+
var device = SimulationDatabase.db.ContainsKey(selectedDevice) ? selectedDevice : default;
36+
var cutouts = orientation == ScreenOrientation.Landscape ? SimulationDatabase.db[device].landscapeCutouts : SimulationDatabase.db[device].portraitCutouts;
37+
if (cutouts is null) return new Rect[0];
38+
var screenSize = SimulationDatabase.db[device].screenSize;
39+
if (orientation == ScreenOrientation.Landscape)
3840
{
39-
return relativeSafeArea;
41+
var swap = screenSize.x;
42+
screenSize.x = screenSize.y;
43+
screenSize.y = swap;
4044
}
45+
46+
System.Collections.Generic.List<Rect> rects = new System.Collections.Generic.List<Rect>();
47+
foreach (var cutout in cutouts) rects.Add(GetRelative(cutout, screenSize));
48+
return rects.ToArray();
49+
}
50+
}
51+
52+
static Rect GetRelative(Rect original, Vector2 screenSize)
53+
{
54+
var relativeCutout = new Rect(original.xMin / screenSize.x, original.yMin / screenSize.y, original.width / screenSize.x, original.height / screenSize.y);
55+
if (flipOrientation)
56+
{
57+
return new Rect(
58+
1 - (relativeCutout.width + relativeCutout.xMin),
59+
1 - (relativeCutout.height + relativeCutout.yMin),
60+
relativeCutout.width,
61+
relativeCutout.height
62+
);
4163
}
64+
else return relativeCutout;
4265
}
4366

4467
internal static ScreenOrientation GetGameViewOrientation()
@@ -57,13 +80,13 @@ internal static Vector2 GetMainGameViewSize()
5780
return (Vector2)argsForOut[0];
5881
}
5982

60-
internal static bool enableSimulation
83+
internal static bool enableSimulation
6184
{
6285
get { return EditorPrefs.GetBool(enableSimulationKey); }
6386
set { EditorPrefs.SetBool(enableSimulationKey, value); }
6487
}
6588

66-
internal static bool flipOrientation
89+
internal static bool flipOrientation
6790
{
6891
get { return EditorPrefs.GetBool(flipOrientationKey); }
6992
set { EditorPrefs.SetBool(flipOrientationKey, value); }

Editor/SafeAreaPaddingDrawer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,5 @@ public override void OnInspectorGUI()
7575
}
7676
}
7777

78-
78+
7979
}

Editor/SafeAreaPaddingSidesDrawer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ public class SafeAreaPaddingModeDrawer : EnumButtonsDrawer { }
99

1010
[CustomPropertyDrawer(typeof(SafeAreaPaddingOrientationType))]
1111
public class SafeAreaPaddingOrientationTypeDrawer : EnumButtonsDrawer { }
12-
12+
1313
}

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Please see the Issue section.
111111
- iPhone X
112112
- iPad Pro
113113

114-
(Contributed by @06Games)
114+
(Contributed by [06Games](https://github.com/06Games))
115115

116116
- Huawei Mate 20 Pro
117117
- OnePlus 6T
@@ -128,4 +128,4 @@ My own other (paid) assets... haha. Asides from code contribution, you could als
128128

129129
The game at the top is [Duel Otters/ตัวนากท้าดวล/かわうそバトル](http://exceed7.com/duel-otters).
130130

131-
One another is an in-development game [Mel Cadence/メルカデンツ](http://exceed7.com/mel-cadence).
131+
One another is an in-development game [Mel Cadence/メルカデンツ](http://exceed7.com/mel-cadence).

Runtime/INotchSimulatorTarget.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ namespace E7.NotchSolution
44
{
55
public interface INotchSimulatorTarget
66
{
7-
void SimulatorUpdate(Rect simulatedSafeArea);
7+
void SimulatorUpdate(Rect simulatedSafeArea, Rect[] simulatedCutouts);
88
}
99
}

0 commit comments

Comments
 (0)