Created
July 16, 2018 12:00
-
-
Save stopiccot/46c2c73576d94c10458b88c7b7bdd41a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
#if UNITY_IOS | |
using UnityEngine.iOS; | |
#endif | |
public class IPhoneXCheck : MonoBehaviour { | |
protected bool IPhoneX() { | |
#if UNITY_LUNA | |
var ratio = Bridge.Script.Get<float>("window.devicePixelRatio || 1"); | |
var w = Bridge.Script.Get<float>("window.screen.width * ratio"); | |
var h = Bridge.Script.Get<float>("window.screen.height * ratio"); | |
if (Bridge.Script.Get<bool>("navigator.userAgent.indexOf('iPhone') >= 0")) { | |
if (w == 1125 && h == 2436) { | |
return true; | |
} | |
} | |
return false; | |
#elif UNITY_IOS | |
return Device.generation == DeviceGeneration.iPhoneX; | |
#else | |
return false; | |
#endif | |
} | |
public enum Mode { | |
Position = 0, | |
PreferredHeight = 1, | |
TopPadding = 2, | |
BottomPadding = 3 | |
} | |
public Mode mode = Mode.Position; | |
public float padding = 0; | |
[ContextMenu("ForceApply")] | |
public void ForceApply() { | |
foreach (var o in FindObjectsOfType<iPhoneXCheck>()) { | |
o.ApplyAdjusment(); | |
} | |
} | |
protected void ApplyAdjusment() { | |
if (mode == Mode.Position) { | |
var rt = GetComponent<RectTransform>(); | |
var pos = rt.anchoredPosition; | |
pos.y += padding; | |
rt.anchoredPosition = pos; | |
} else if (mode == Mode.PreferredHeight) { | |
var le = GetComponent<UnityEngine.UI.LayoutElement>(); | |
le.preferredHeight = le.preferredHeight + padding; | |
} else if (mode == Mode.TopPadding) { | |
var rt = GetComponent<RectTransform>(); | |
rt.offsetMax = new Vector2(rt.offsetMax.x, rt.offsetMax.y + padding); | |
} else if (mode == Mode.BottomPadding) { | |
var rt = GetComponent<RectTransform>(); | |
rt.offsetMin = new Vector2(rt.offsetMin.x, rt.offsetMin.y + padding); | |
} | |
} | |
private void Start() { | |
if (IsIPhoneX()) { | |
ApplyAdjusment(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment