Unity 편집기에서 화면 크기를 iPhoneX와 같은 크기 (1125 × 2436)로 설정하면 Unity Player에서 디버그 할 수도 있습니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
publicclassSafeAreaScreen {#if UNITY_IOS
[DllImport("__Internal")]
privateexternstaticvoidGetSafeAreaImpl(out float x, out float y, out float w, out float h);
#endifstaticpublic Rect GetSafeArea(){
float x, y, w, h;
//初期値設定
x = 0;
y = 0;
w = Screen.width;
h = Screen.height;
#if UNITY_IOS && !UNITY_EDITOR//iOSSafeAreasPluginを呼び出すGetSafeAreaImpl(out x, out y, out w, out h);
#elif UNITY_ANDROID && !UNITY_EDITOR//Androidの縦長端末対応が必要になったときのために#else//UnityエディタでiPhoneXのテストできるように値を設定するif(Screen.width == 1125 && Screen.height == 2436 ){
y = 102;
h = 2202;
}
#endifreturnnewRect(x, y, w, h);
}
staticpublicintGetTopOffsetY(){
int offset = 0;
//画面の高さとセーフエリアの高さの差分
Rect screenSize = GetSafeArea();
if (Screen.height != screenSize.height) {
offset = Screen.height - (int)screenSize.height - (int)screenSize.y;
}
return offset;
}
staticpublicintGetBottomOffsetY(){
int offset = 0;
//セーフエリアのyの位置取得
Rect screenSize = GetSafeArea();
if (Screen.height != screenSize.height) {
offset = (int)screenSize.y;
}
return offset;
}
}
NGUI의 SafeArea
NguiSafeAreaPatch.cs
SafeAreaScreen.cs에서 얻은 안전 영역을 바탕으로 Anchor의 위치를 조정합니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
publicclassNguiSafeAreaPatch : MonoBehaviour {
voidAwake(){
// UIAnchorを使ってAnchorを設定している場合
UIAnchor anchor = gameObject.GetComponent();
if(anchor != null){
//offsetの値を取得するSetUIAnchorOffset(anchor);
return;
}
// UIRectをAnchorを使っている場合
UIRect rect = gameObject.GetComponent();
if (rect != null) {
//現在,NGUIのソースコードを解析中//完成しだい公開予定return;
}
}
privatevoidSetUIAnchorOffset(UIAnchor anchor){
//現在のoffsetを取得する
Vector2 nowOffset = anchor.pixelOffset;
//TOP側の補正値取得int topCorrectionOffset = SafeAreaScreen.GetTopOffsetY ();
//Bottom側の補正値取得int bottomCorrectionOffset = SafeAreaScreen.GetBottomOffsetY ();
switch (anchor.side) {
case UIAnchor.Side.Top:
case UIAnchor.Side.TopLeft:
case UIAnchor.Side.TopRight:
anchor.pixelOffset.Set(nowOffset.x, nowOffset.y - topCorrectionOffset);
break;
case UIAnchor.Side.Bottom:
case UIAnchor.Side.BottomLeft:
case UIAnchor.Side.BottomRight:
anchor.pixelOffset.Set(nowOffset.x, nowOffset.y + bottomCorrectionOffset);
break;
}
}
}
사용법은이 소스 코드를 UIAnchor가 부착되어있는 게임 객체에 첨부 할뿐.
소스에서 무슨 일을하는지는 동작 환경이 iPhoneX 판정되면 스크린과 안전 영역의 높이의 차이를 계산하여 차등 분 게임 오브젝트를 낮 춥니 다.
델타 값은 UIAnchor의 "Pixel Offset"에 할당됩니다.
소스 코드를 반영한 결과
NguiSafeAreaPatch.cs을 UIAnchor에 연결시킨 결과를 기재합니다.
■ 반영 전
■ 반영 후
그 결과 안전 영역을 기준으로 할 수있었습니다.
이것으로 UIAnchor 대해서는 SafeArea의 대처가되었습니다.
그러나, 그 밖에도 「UIRect의 Anchor '에 대해서도 SafeAreas 대응이 있습니다.대응이 끝나는대로 수시 문서에 추가하는 것입니다.
I've decided to create a sticky where I can bookmark topics that others may find useful. I'll keep adding stuff here (and you're welcome to reply to suggest other additions).
GUI 최적화의 핵심은 정적인 UI 들과 동적인 UI 들을 나누는 것이다. 나누는 기준은 패널이다. 예를 들어 다음과 같은 하이어라키를 생각해볼 수 있다.
Panel1
정적인 UI들
Panel2
동적인 UI들
이렇게 하는 이유는 UI 가 갱신될 때마다 UI 가 속한 패널의 지오매트리가 재구성되기 때문이다. 재구성 작업에 많은 양의 가비지가 발생한다. 여기서 주의해야할 점은 위치 이동같은 기본 변환은 '갱신' 에 포함되지 않는다는 것이다. 지오매트리가 변해야 하는 작업들, 예를 들어 SetActive, UILabel 의 text 변경 등이 갱신에 포함된다.
패널의 UI 들중 하나라도 갱신이 되면 패널의 모든 UI 들에 대한 지오매트리 재구성 작업이 수행된다. 따라서 제일 최악의 시나리오는 패널을 루트에 단 하나 만들어두는 것이다. 이러면 UI 의 변경은 곧 씬의 모든 UI 들의 갱신이라는 결과로 작용한다.
동적인 UI 들 역시 하나의 패널로 몰아넣는건 같은 이유로 피해야 한다. 그렇다고 해서 패널의 개수를 너무 많이 늘려서는 안되는데, 패널 단위로 드로우콜이 분리되기 때문에 렌더링 성능은 낮아진다. 따라서 성능 측정을 결과로 패널의 개수를 조절할 필요가 있다.
From public string text { get { return mText; } set { if (string.IsNullOrEmpty(value)) { if (!string.IsNullOrEmpty(mText)) mText = ""; hasChanged = true; } else if (mText != value) { mText = value; <=== Here hasChanged = true; } } }
To public string text { get { return mText; } set { if (string.IsNullOrEmpty(value)) { if (!string.IsNullOrEmpty(mText)) mText = ""; hasChanged = true; } else if (mText != value) { mText = value.Replace("\\n", "\n");<== Here hasChanged = true; } } }