「Unity/端末サイズ」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→Screen.SafeAreaの使い方) |
|||
| (同じ利用者による、間の27版が非表示) | |||
| 行1: | 行1: | ||
==サンプル== | ==サンプル== | ||
| + | #CameraオブジェクトのProjectionをorthographicへ変更しておく。 | ||
| + | #CanvasのRenderModeをWorldSpaceに変更 | ||
void Awake() | void Awake() | ||
{ | { | ||
| − | TermSize. | + | TermSize.OrthographicSizeAuto(); |
} | } | ||
TermSize.cs | TermSize.cs | ||
public class TermSize { | public class TermSize { | ||
| − | public static void | + | public static void OrthographicSizeAuto () { |
// 縦画面 iPhone6 | // 縦画面 iPhone6 | ||
float developAspect = 750.0f / 1334.0f; | float developAspect = 750.0f / 1334.0f; | ||
| − | // 横画面 | + | // 横画面 iPhone6 |
| − | float developAspect = 1334.0f / 750.0f; | + | // float developAspect = 1334.0f / 750.0f; |
float deviceAspect = (float)Screen.width / (float)Screen.height; | float deviceAspect = (float)Screen.width / (float)Screen.height; | ||
float scale = deviceAspect / developAspect; | float scale = deviceAspect / developAspect; | ||
| 行17: | 行19: | ||
float deviceSize = mainCamera.orthographicSize; | float deviceSize = mainCamera.orthographicSize; | ||
float deviceScale = 1.0f / scale; | float deviceScale = 1.0f / scale; | ||
| + | if (scale > 1) { | ||
| + | Debug.Log("scale変更不要"); | ||
| + | return; | ||
| + | } | ||
mainCamera.orthographicSize = deviceSize * deviceScale; | mainCamera.orthographicSize = deviceSize * deviceScale; | ||
} | } | ||
| 行22: | 行28: | ||
参考:http://www.project-unknown.jp/entry/2017/01/05/212837 | 参考:http://www.project-unknown.jp/entry/2017/01/05/212837 | ||
| + | |||
| + | ==ipad判定== | ||
| + | <pre> | ||
| + | public static bool IsIPad { | ||
| + | get { | ||
| + | return SystemInfo.deviceModel.Contains ("iPad"); | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | 参考:http://westhillapps.blog.jp/archives/35736621.html | ||
| + | ===縦ipad判定=== | ||
| + | <pre> | ||
| + | public static bool IsIPadDeviceAspect() { | ||
| + | float deviceAspect = (float)Screen.width / (float)Screen.height; // 2048 / 2732 = 0.74963397 | ||
| + | if (deviceAspect > 0.749f && deviceAspect <= 0.751f) { | ||
| + | return true; | ||
| + | } | ||
| + | return false; | ||
| + | } | ||
| + | </pre> | ||
| + | ==CanvasScalerを使った自動フィット== | ||
| + | #CanvasのRenderModeをScreenSpace-Cameraに変更 | ||
| + | #CanvasのInspectorのCanvasScalerを"Constant Pixel Size "から"Scale With Screen Size"に変更して | ||
| + | #CanvasScalerのReferenceResolutionを1080x1920(CanvasのRectTransformの値と同じ)へ | ||
| + | |||
| + | padのときは、調整が必要なので、以下CanvasScalerを使ったautofitを使うとうまくはまる | ||
| + | <pre> | ||
| + | TermSize.OrthographicSizeAutoCanvasScaler(GameObject.Find("/Canvas").GetComponent<CanvasScaler>()); | ||
| + | public static void OrthographicSizeAutoCanvasScaler(CanvasScaler canvasScaler) | ||
| + | { | ||
| + | Vector2 developValue = new Vector2(1080f, 1920f); | ||
| + | float developAspect = developValue.x / developValue.y; | ||
| + | float deviceAspect = (float)Screen.width / (float)Screen.height; | ||
| + | float scale = deviceAspect / developAspect; | ||
| + | if (scale < 1) | ||
| + | { | ||
| + | canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; | ||
| + | canvasScaler.referenceResolution = developValue; | ||
| + | return; | ||
| + | } | ||
| + | float width = developValue.y * (float)Screen.width / (float)Screen.height; | ||
| + | canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; | ||
| + | canvasScaler.referenceResolution = new Vector2(width, developValue.y); | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | ==Screen.SafeAreaの使い方== | ||
| + | ===Screen.SafeAreaとは=== | ||
| + | 上部カメラの切り欠きなどを除外した、適切に表示できる四角エリアの範囲 | ||
| + | |||
| + | <pre> | ||
| + | GameObject image = GameObject.Find("Canvas/Image"); | ||
| + | ApplySafeArea(image.GetComponent<RectTransform>()); | ||
| + | void ApplySafeArea(RectTransform rectTransform) | ||
| + | { | ||
| + | var safeArea = Screen.safeArea; | ||
| + | var resolition = Screen.currentResolution; | ||
| + | rectTransform.sizeDelta = Vector2.zero; | ||
| + | rectTransform.anchorMax = new Vector2(safeArea.xMax / resolition.width, safeArea.yMax / resolition.height); | ||
| + | rectTransform.anchorMin = new Vector2(safeArea.xMin / resolition.width, safeArea.yMin / resolition.height); | ||
| + | } | ||
| + | </pre> | ||
| + | 縦型で、実行すると、RectTransformのAnchorsのminとmaxのy値が変更される。 | ||
| + | |||
| + | 参考:https://tsubakit1.hateblo.jp/entry/2019/10/30/235150 | ||
| + | |||
| + | ==AutoScreenライブラリ== | ||
| + | 端末画面サイズ判定として、AutoScreenがある。 | ||
| + | |||
| + | https://github.com/su10/AutoScreen-for-Unity2021 | ||
| + | |||
| + | 内部で、Screen.safeArea;を使ってる。 | ||
| + | |||
| + | ==SafeAreaHelperライブラリ== | ||
| + | 端末画面サイズ判定として、SafeAreaHelperがある。 | ||
| + | |||
| + | [[Unity/おすすめアセット/SafeAreaHelper]] [ショートカット] | ||
| + | |||
| + | 内部で、Screen.safeArea;を使ってる。 | ||
2024年10月7日 (月) 01:32時点における最新版
目次
サンプル
- CameraオブジェクトのProjectionをorthographicへ変更しておく。
- CanvasのRenderModeをWorldSpaceに変更
void Awake()
{
TermSize.OrthographicSizeAuto();
}
TermSize.cs
public class TermSize {
public static void OrthographicSizeAuto () {
// 縦画面 iPhone6
float developAspect = 750.0f / 1334.0f;
// 横画面 iPhone6
// float developAspect = 1334.0f / 750.0f;
float deviceAspect = (float)Screen.width / (float)Screen.height;
float scale = deviceAspect / developAspect;
Camera mainCamera = Camera.main;
float deviceSize = mainCamera.orthographicSize;
float deviceScale = 1.0f / scale;
if (scale > 1) {
Debug.Log("scale変更不要");
return;
}
mainCamera.orthographicSize = deviceSize * deviceScale;
}
}
参考:http://www.project-unknown.jp/entry/2017/01/05/212837
ipad判定
public static bool IsIPad {
get {
return SystemInfo.deviceModel.Contains ("iPad");
}
}
参考:http://westhillapps.blog.jp/archives/35736621.html
縦ipad判定
public static bool IsIPadDeviceAspect() {
float deviceAspect = (float)Screen.width / (float)Screen.height; // 2048 / 2732 = 0.74963397
if (deviceAspect > 0.749f && deviceAspect <= 0.751f) {
return true;
}
return false;
}
CanvasScalerを使った自動フィット
- CanvasのRenderModeをScreenSpace-Cameraに変更
- CanvasのInspectorのCanvasScalerを"Constant Pixel Size "から"Scale With Screen Size"に変更して
- CanvasScalerのReferenceResolutionを1080x1920(CanvasのRectTransformの値と同じ)へ
padのときは、調整が必要なので、以下CanvasScalerを使ったautofitを使うとうまくはまる
TermSize.OrthographicSizeAutoCanvasScaler(GameObject.Find("/Canvas").GetComponent<CanvasScaler>());
public static void OrthographicSizeAutoCanvasScaler(CanvasScaler canvasScaler)
{
Vector2 developValue = new Vector2(1080f, 1920f);
float developAspect = developValue.x / developValue.y;
float deviceAspect = (float)Screen.width / (float)Screen.height;
float scale = deviceAspect / developAspect;
if (scale < 1)
{
canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
canvasScaler.referenceResolution = developValue;
return;
}
float width = developValue.y * (float)Screen.width / (float)Screen.height;
canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
canvasScaler.referenceResolution = new Vector2(width, developValue.y);
}
Screen.SafeAreaの使い方
Screen.SafeAreaとは
上部カメラの切り欠きなどを除外した、適切に表示できる四角エリアの範囲
GameObject image = GameObject.Find("Canvas/Image");
ApplySafeArea(image.GetComponent<RectTransform>());
void ApplySafeArea(RectTransform rectTransform)
{
var safeArea = Screen.safeArea;
var resolition = Screen.currentResolution;
rectTransform.sizeDelta = Vector2.zero;
rectTransform.anchorMax = new Vector2(safeArea.xMax / resolition.width, safeArea.yMax / resolition.height);
rectTransform.anchorMin = new Vector2(safeArea.xMin / resolition.width, safeArea.yMin / resolition.height);
}
縦型で、実行すると、RectTransformのAnchorsのminとmaxのy値が変更される。
参考:https://tsubakit1.hateblo.jp/entry/2019/10/30/235150
AutoScreenライブラリ
端末画面サイズ判定として、AutoScreenがある。
https://github.com/su10/AutoScreen-for-Unity2021
内部で、Screen.safeArea;を使ってる。
SafeAreaHelperライブラリ
端末画面サイズ判定として、SafeAreaHelperがある。
Unity/おすすめアセット/SafeAreaHelper [ショートカット]
内部で、Screen.safeArea;を使ってる。
