Unity IPhone SafeArea(NGUI)

2 분 소요

NGUI사용시 아이폰X 해상도를 대응해야 한다.

위 mm파일을 프로젝트에 추가한뒤 NGUI코드를 수정한다.

NGUITools.cs

static public Vector2 screenSize
{
    /*...*/
#if UNITY_IOS
        float x, y, w, h;
        GetSafeAreaImpl(out x, out y, out w, out h);

        mGameSize = new Vector2(w, h);
#endif
        return mGameSize;
}

public static Rect SafeArea
{
    get
    {
        return GetSafeArea();
    }
}

public static Rect GetSafeArea()
{
    float x, y, w, h;
#if UNITY_IOS && !UNITY_EDITOR
    GetSafeArea(out x, out y, out w, out h);
#else
    x = 0;
    y = 0;
    w = Screen.width;
    h = Screen.height;
#endif
    return new Rect(x, y, w, h);
}

public static bool Simulate_X
{
    get
    {
        float aspect = screenSize.x / screenSize.y;
        return aspect < 0.5f;
        return false;
    }
}

public static bool Simulate_Y
{
    get
    {
        float aspect = screenSize.x / screenSize.y;
        return aspect < 0.5f;
    }
}

public static float iPhone_X_XScale
{
    get
    {
        if (!Simulate_X) return 1f;
        /*
        iPhone X :2436 x 1125 px
        SafeArea:2172 x 1062 px
        margin:132px
        Home key:63px
        */
        return SafeArea.width / Screen.width;
    }
}

public static float iPhone_X_YScale
{
    get
    {
        if (!Simulate_Y) return 1f;
        /*
        iPhone X :2436 x 1125 px
        SafeArea:2172 x 1062 px
        margin:132px
        Home key:63px
        */
        float height = SafeArea.height;
#if UNITY_EDITOR // 에디터 테스트용
        height = 2172f;
#endif
        return height / Screen.height;
    }
}

[DllImport("__Internal")]
private extern static void GetSafeAreaImpl(out float x, out float y, out float w, out float h);

UIRectEditor.cs

protected virtual void DrawFinalProperties ()
{
    if (!((target as UIRect).canBeAnchored))
    {
        if (NGUIEditorTools.DrawHeader("iPhone X"))
        {
            NGUIEditorTools.BeginContents();
            {
                GUILayout.BeginHorizontal();
                NGUIEditorTools.SetLabelWidth(100f);
                NGUIEditorTools.DrawProperty("ShowInSafeArea", serializedObject, "mShowInSafeArea", GUILayout.Width(120f));
                GUILayout.Label("iphone X");
                GUILayout.EndHorizontal();

            }
            NGUIEditorTools.EndContents();
        }
    }
    if (!NGUISettings.unifiedTransform)
        DrawAnchorTransform();
}

UIRect.cs

[HideInInspector] [SerializeField] public bool mShowInSafeArea = false;

public Vector3[] GetSides (Transform relativeTo)
{
    if (target != null)
    {
        if (rect != null) return rect.GetSides(relativeTo);
#if UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7
        if (target.camera != null) return target.camera.GetSides(relativeTo);
#else
        var cam = target.GetComponent<Camera>();
        if (cam != null) return cam.GetSides(relativeTo, rect.mShowInSafeArea);
#endif
    }
    return null;
}

public virtual Vector3[] GetSides (Transform relativeTo)
{
    if (anchorCamera != null)
    {
        return mCam.GetSides(cameraRayDistance, relativeTo, mShowInSafeArea);
    }
    
    Vector3 pos = cachedTransform.position;
    for (int i = 0; i < 4; ++i)
        mSides[i] = pos;

    if (relativeTo != null)
    {
        for (int i = 0; i < 4; ++i)
            mSides[i] = relativeTo.InverseTransformPoint(mSides[i]);
    }
    return mSides;
}

UIAnchor.cs

void Update ()
{
    /*...*/
    else if (pc != null)
    {
        if (pc.clipping == UIDrawCall.Clipping.None)
        {
            // Panel has no clipping -- just use the screen's dimensions
            float ratio = (mRoot != null) ? (float)mRoot.activeHeight / NGUITools.SafeArea.height * 0.5f : 0.5f;
            mRect.xMin = -NGUITools.SafeArea.width * ratio * NGUITools.iPhone_X_XScale;
            mRect.yMin = -NGUITools.SafeArea.height * ratio * NGUITools.iPhone_X_YScale;
            mRect.xMax = -mRect.xMin;
            mRect.yMax = -mRect.yMin;
        }
        else
        {
            // Panel has clipping -- use it as the mRect
            Vector4 pos = pc.finalClipRegion;
            mRect.x = pos.x - (pos.z * 0.5f);
            mRect.y = pos.y - (pos.w * 0.5f);
            mRect.width = pos.z;
            mRect.height = pos.w;
        }
    }
    /*...*/
}

후에 UIPanel에 SafeArea를 체크하면 해상도 대응할 수 있다.

태그:

카테고리:

업데이트: