Unity to ios Notch And Safe Are Problems

18,247

Solution 1

About the iPhone X notch, you can use this:

Screen.safeArea

It is a convenient way to determine the screen actual "Safe Area". Read more about it in this thread.

About the character cutting in half, this is probably something you need to take care of manually based on your game logic. By getting the Screen.width - you should be able to either adjust the Camera (zoom out) or limit the character movement in a way that it will not get past the screen edge.

Solution 2

For the iPhone X and other notched phones you can use the generic Screen.safeArea provided by Unity 2017.2.1+. Attach the script below to a full screen UI panel (Anchor 0,0 to 1,1; Pivot 0.5,0.5) and it will shape itself to the screen safe.

Also recommended to have your Canvas set to "Scale With Screen Size" and "Match (Width-Height)" = 0.5.

public class SafeArea : MonoBehaviour
{
    RectTransform Panel;
    Rect LastSafeArea = new Rect (0, 0, 0, 0);

    void Awake ()
    {
        Panel = GetComponent<RectTransform> ();
        Refresh ();
    }

    void Update ()
    {
        Refresh ();
    }

    void Refresh ()
    {
        Rect safeArea = GetSafeArea ();

        if (safeArea != LastSafeArea)
            ApplySafeArea (safeArea);
    }

    Rect GetSafeArea ()
    {
        return Screen.safeArea;
    }

    void ApplySafeArea (Rect r)
    {
        LastSafeArea = r;

        Vector2 anchorMin = r.position;
        Vector2 anchorMax = r.position + r.size;
        anchorMin.x /= Screen.width;
        anchorMin.y /= Screen.height;
        anchorMax.x /= Screen.width;
        anchorMax.y /= Screen.height;
        Panel.anchorMin = anchorMin;
        Panel.anchorMax = anchorMax;
    }
}

For a more in depth breakdown, I've written a detailed article with screenshots here: https://connect.unity.com/p/updating-your-gui-for-the-iphone-x-and-other-notched-devices. Hope it helps!

Share:
18,247

Related videos on Youtube

Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I have tried creating a game in unity and build it on ios on any apple devices there were no problem except on iphone x. Screenshot is below.enter image description here. It was covered by the iphone x notch and then when when the character is on the left or the right side it was cut it half.Is there any other solution or a plugin we can use to solve the issue ?. Is there a unity settins or xcode settings to that ? .Thank You

  • Admin
    Admin over 5 years
    thanks for idea man , what I did is that i created a code that will be able to detect the device type which is (iphonex) and if its true then it will automatically adjust the camera , i think that is the best way to do it . and also on other parts like canvas same thing detect device type and then adjust manually.
  • Eyal Biran
    Eyal Biran over 5 years
    @DulfKoher That is also a way to solve it :) Just notice that more devices with notchs are going to come out soon (there are actually others out there now). When using Screen.safeArea - you can provide a solution for all cases (if Unity team will keep it alive as they should) - While providing a specific solution for iPhone X is good - it is not very robust.
  • Firemaw
    Firemaw over 5 years
    @CthulhuJon Currently the Screen.safeArea only works in 2017.2.1+. For Android notch support, a Unity dev has stated that it's currently in progress for a v2018.3 or above release and likely won't be backported to 2017.4. forum.unity.com/threads/538924/#post-3816202. Given this info, it seems unlikely the system will be backported to 5.6 unfortunately.
  • Firemaw
    Firemaw over 5 years
    If iOS.DeviceGeneration could return the X series values in 5.6 then you could possibly hardcode a manual safe area system similar to the way I create a simulation safe area in the article, but I don't believe they have or will patch those new device enums into 5.6. If there is a way to detect iPhone X on 5.6 using a plugin or some other means you could do it.
  • john316
    john316 over 4 years
    one thing to be aware of is that Screen.safeArea returns actual safeArea only once Unity view is initialized. I tried calling from Awake() of a -90 priority script first, and it would return just full screen area there... moving it to Start() fixed the problem.