internal static Rect[] CalculateSimulatorCutoutsRelative(SimulationDevice device)
        {
            var orientation = GetGameViewOrientation();
            var cutouts     = device.Screens.FirstOrDefault().orientations[orientation].cutouts;

            if (cutouts is null)
            {
                return(new Rect[0]);
            }
            var screenSize = new Vector2(device.Screens.FirstOrDefault().width, device.Screens.FirstOrDefault().height);

            if (orientation == ScreenOrientation.Landscape)
            {
                var swap = screenSize.x;
                screenSize.x = screenSize.y;
                screenSize.y = swap;
            }

            System.Collections.Generic.List <Rect> rects = new System.Collections.Generic.List <Rect>();
            foreach (var cutout in cutouts)
            {
                rects.Add(GetRectRelativeToScreenSize(cutout, screenSize));
            }
            return(rects.ToArray());
        }
Example #2
0
        //TODO : Game view related reflection methods should be cached.

        private void UpdateMockup(SimulationDevice simDevice)
        {
            bool       enableSimulation = NotchSimulatorUtility.enableSimulation;
            GameObject mockupCanvas     = GameObject.Find(mockupCanvasName);

            if (enableSimulation)
            {
                //Landscape has an alias that turns ToString into LandscapeLeft lol
                var orientationString = NotchSimulatorUtility.GetGameViewOrientation() == ScreenOrientation.Landscape ? nameof(ScreenOrientation.Landscape) : nameof(ScreenOrientation.Portrait);
                var name  = $"{prefix}-{simDevice.ToString()}-{orientationString}";
                var guids = AssetDatabase.FindAssets(name);
                var first = guids.FirstOrDefault();
                if (first == default(string))
                {
                    throw new InvalidOperationException($"No mockup image named {name} in NotchSolution/Editor/Mockups folder!");
                }
                Sprite mockupSprite = AssetDatabase.LoadAssetAtPath <Sprite>(AssetDatabase.GUIDToAssetPath(first));
                if (mockupCanvas == null)
                {
                    var        prefabGuids        = AssetDatabase.FindAssets(mockupCanvasName);
                    GameObject mockupCanvasPrefab = AssetDatabase.LoadAssetAtPath <GameObject>(AssetDatabase.GUIDToAssetPath(prefabGuids.First()));
                    mockupCanvas           = (GameObject)PrefabUtility.InstantiatePrefab(mockupCanvasPrefab);
                    mockupCanvas.hideFlags = HideFlags.HideAndDontSave | HideFlags.NotEditable | HideFlags.HideInInspector;
                }
                var mc = mockupCanvas.GetComponent <MockupCanvas>();

                mc.SetMockupSprite(mockupSprite, NotchSimulatorUtility.GetGameViewOrientation(), simulate: enableSimulation, flipped: NotchSimulatorUtility.flipOrientation);
            }
            else
            {
                GameObject.DestroyImmediate(mockupCanvas);
            }
        }
Example #3
0
        internal static void UpdateAllMockups()
        {
            //When building, the scene may open-close multiple times and brought back the mockup canvas,
            //which combined with bugs mentioned at https://github.com/5argon/NotchSolution/issues/11,
            //will fail the build. This `if` prevents mockup refresh while building.
            if (BuildPipeline.isBuildingPlayer)
            {
                return;
            }

            EnsureCanvasAndEventSetup();

            //Make the editing environment contains an another copy of mockup canvas.
            var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();

            if (prefabStage != null)
            {
                EnsureCanvasAndEventSetup(prefabStage: prefabStage);
            }

            bool enableSimulation = NotchSimulatorUtility.enableSimulation;

            if (enableSimulation)
            {
                //Landscape has an alias that turns ToString into LandscapeLeft lol
                var orientationString      = NotchSimulatorUtility.GetGameViewOrientation() == ScreenOrientation.Landscape ? nameof(ScreenOrientation.Landscape) : nameof(ScreenOrientation.Portrait);
                SimulationDevice simDevice = NotchSimulatorUtility.selectedDevice;
                var name  = $"{prefix}-{simDevice.ToString()}-{orientationString}";
                var guids = AssetDatabase.FindAssets(name);
                var first = guids.FirstOrDefault();

                if (first == default(string))
                {
                    throw new InvalidOperationException($"No mockup image named {name} in NotchSolution/Editor/Mockups folder!");
                }
                Sprite mockupSprite = AssetDatabase.LoadAssetAtPath <Sprite>(AssetDatabase.GUIDToAssetPath(first));

                foreach (var mockup in AllMockupCanvases)
                {
                    mockup.Show();
                    mockup.SetMockupSprite(
                        sprite: mockupSprite,
                        orientation: NotchSimulatorUtility.GetGameViewOrientation(),
                        simulate: enableSimulation,
                        flipped: NotchSimulatorUtility.flipOrientation
                        );
                }
            }
            else
            {
                foreach (var mockup in AllMockupCanvases)
                {
                    mockup.Hide();
                }
            }
        }
        internal static Rect CalculateSimulatorSafeAreaRelative(SimulationDevice device)
        {
            var orientation = GetGameViewOrientation();
            var safe        = device.Screens.FirstOrDefault().orientations[orientation].safeArea;
            var screenSize  = new Vector2(device.Screens.FirstOrDefault().width, device.Screens.FirstOrDefault().height);

            if (orientation == ScreenOrientation.Landscape)
            {
                var swap = screenSize.x;
                screenSize.x = screenSize.y;
                screenSize.y = swap;
            }
            return(GetRectRelativeToScreenSize(safe, screenSize));
        }
Example #5
0
        internal static void UpdateAllMockups()
        {
            EnsureCanvasAndEventSetup();

            //Make the editing environment contains an another copy of mockup canvas.
            var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();

            if (prefabStage != null)
            {
                EnsureCanvasAndEventSetup(prefabStage: prefabStage);
            }

            bool enableSimulation = NotchSimulatorUtility.enableSimulation;

            if (enableSimulation)
            {
                //Landscape has an alias that turns ToString into LandscapeLeft lol
                var orientationString      = NotchSimulatorUtility.GetGameViewOrientation() == ScreenOrientation.Landscape ? nameof(ScreenOrientation.Landscape) : nameof(ScreenOrientation.Portrait);
                SimulationDevice simDevice = NotchSimulatorUtility.selectedDevice;
                var name  = $"{prefix}-{simDevice.ToString()}-{orientationString}";
                var guids = AssetDatabase.FindAssets(name);
                var first = guids.FirstOrDefault();

                if (first == default(string))
                {
                    throw new InvalidOperationException($"No mockup image named {name} in NotchSolution/Editor/Mockups folder!");
                }
                Sprite mockupSprite = AssetDatabase.LoadAssetAtPath <Sprite>(AssetDatabase.GUIDToAssetPath(first));

                foreach (var mockup in AllMockupCanvases)
                {
                    mockup.Show();
                    mockup.SetMockupSprite(
                        sprite: mockupSprite,
                        orientation: NotchSimulatorUtility.GetGameViewOrientation(),
                        simulate: enableSimulation,
                        flipped: NotchSimulatorUtility.flipOrientation
                        );
                }
            }
            else
            {
                foreach (var mockup in AllMockupCanvases)
                {
                    mockup.Hide();
                }
            }
        }
Example #6
0
        private static void UpdateMockup()
        {
            bool enableSimulation = NotchSimulatorUtility.enableSimulation;

            //Create the hidden canvas if not already.
            if (canvasObject == null)
            {
                //Find existing in the case of assembly reload
                canvasObject = GameObject.Find(mockupCanvasName);
                if (canvasObject != null)
                {
                    //Debug.Log($"Found existing");
                    mockupCanvas = canvasObject.GetComponent <MockupCanvas>();
                }
                else
                {
                    //Debug.Log($"Creating canvas");
                    var        prefabGuids        = AssetDatabase.FindAssets(mockupCanvasName);
                    GameObject mockupCanvasPrefab = AssetDatabase.LoadAssetAtPath <GameObject>(AssetDatabase.GUIDToAssetPath(prefabGuids.First()));
                    canvasObject           = (GameObject)PrefabUtility.InstantiatePrefab(mockupCanvasPrefab);
                    mockupCanvas           = canvasObject.GetComponent <MockupCanvas>();
                    canvasObject.hideFlags = overlayCanvasFlag;

                    if (Application.isPlaying)
                    {
                        DontDestroyOnLoad(canvasObject);
                    }
                }

                if (eventAdded == false)
                {
                    eventAdded = true;

                    //Add clean up event.
                    EditorApplication.playModeStateChanged += PlayModeStateChangeAction;
                    // EditorSceneManager.sceneClosing += (a, b) =>
                    // {
                    //     Debug.Log($"Scene closing {a} {b}");
                    // };
                    // EditorSceneManager.sceneClosed += (a) =>
                    // {
                    //     Debug.Log($"Scene closed {a}");
                    // };
                    // EditorSceneManager.sceneLoaded += (a, b) =>
                    //  {
                    //      Debug.Log($"Scene loaded {a} {b}");
                    //  };
                    // EditorSceneManager.sceneUnloaded += (a) =>
                    //  {
                    //      Debug.Log($"Scene unloaded {a}");
                    //  };
                    EditorSceneManager.sceneOpening += (a, b) =>
                    {
                        //Debug.Log($"Scene opening {a} {b}");
                        DestroyHiddenCanvas();
                    };
                    EditorSceneManager.sceneOpened += (a, b) =>
                    {
                        //Debug.Log($"Scene opened {a} {b}");
                        UpdateMockup();
                    };

                    void PlayModeStateChangeAction(PlayModeStateChange state)
                    {
                        //Debug.Log($"Changed state PLAY {EditorApplication.isPlaying} PLAY or WILL CHANGE {EditorApplication.isPlayingOrWillChangePlaymode}");
                        switch (state)
                        {
                        case PlayModeStateChange.EnteredEditMode:
                            //Debug.Log($"Entered Edit {canvasObject}");
                            AddOverlayInPlayMode();     //For when coming back from play mode.
                            break;

                        case PlayModeStateChange.EnteredPlayMode:
                            //Debug.Log($"Entered Play {canvasObject}");
                            break;

                        case PlayModeStateChange.ExitingEditMode:
                            //Debug.Log($"Exiting Edit {canvasObject}");
                            DestroyHiddenCanvas();    //Clean up the DontSave canvas we made in edit mode.
                            break;

                        case PlayModeStateChange.ExitingPlayMode:
                            //Debug.Log($"Exiting Play {canvasObject}");
                            DestroyHiddenCanvas();    //Clean up the DontSave canvas we made in play mode.
                            break;
                        }
                    }
                }
            }

            if (enableSimulation)
            {
                //Landscape has an alias that turns ToString into LandscapeLeft lol
                var orientationString      = NotchSimulatorUtility.GetGameViewOrientation() == ScreenOrientation.Landscape ? nameof(ScreenOrientation.Landscape) : nameof(ScreenOrientation.Portrait);
                SimulationDevice simDevice = NotchSimulatorUtility.selectedDevice;
                var name  = $"{prefix}-{simDevice.ToString()}-{orientationString}";
                var guids = AssetDatabase.FindAssets(name);
                var first = guids.FirstOrDefault();

                if (first == default(string))
                {
                    throw new InvalidOperationException($"No mockup image named {name} in NotchSolution/Editor/Mockups folder!");
                }
                Sprite mockupSprite = AssetDatabase.LoadAssetAtPath <Sprite>(AssetDatabase.GUIDToAssetPath(first));

                mockupCanvas.Show();
                mockupCanvas.SetMockupSprite(mockupSprite, NotchSimulatorUtility.GetGameViewOrientation(), simulate: enableSimulation, flipped: NotchSimulatorUtility.flipOrientation);
            }
            else
            {
                mockupCanvas.Hide();
            }
        }