ãUnityãããã¤ã¹ã縦é·ã»æ¨ªé·ã®æã«é»æ ã表示ãããããCameraã
Unity 2019.2.0f1 Personal(2019å¹´8æ)
ä»åã¯Unityã®Cameraã®å¤ãããã£ã¦ã
ããã¤ã¹ã縦é·ï¼iPhoneXãªã©ï¼ã»æ¨ªé·ï¼ã¿ãã¬ãããªã©ï¼ã®æã«é»æ ã表示ããã
ãã¤ã§ã16:9ã®ç»é¢ãæ ãåºãããã«ãã¦ããã¾ãï¼
ç»é¢ã®ä¾ï¼Portraitï¼
ç»é¢ã®ä¾ï¼Landscapeï¼
ã¹ã¯ãªãã
using System.Collections; using System.Collections.Generic; using UnityEngine; public class DeviceSize : MonoBehaviour { void Start() { SetCameraSize(); } void SetCameraSize() { float deviceHeight = Screen.height; // 端æ«ç¸¦å¹ float deviceWidth = Screen.width; // 端æ«æ¨ªå¹ float baseRatio = (deviceHeight >= deviceWidth) ? (16f / 9f) : (9f / 16f); // åºæºç»é¢ãµã¤ãºå²å(16:9) float deviceRatio = deviceHeight / deviceWidth; // å®éã®ç«¯æ«ãµã¤ãºå²å float resultRatio = baseRatio / deviceRatio; // è£æ£ç¨ç«¯æ«ãµã¤ãºå²å if (resultRatio <= 1.0f) // resultRatioã1以ä¸ãªã横é·(ã¿ãã¬ããç³») { // ã«ã¡ã©ã®æç»ç¯å²Heightãçãããã®1/2ä¸å¤®ã«å¯ããã float rectY = (1.0f - resultRatio) / 2f; Camera.main.rect = new Rect(0, rectY, 1f, resultRatio); } else // resultRatioã1ããä¸ãªã縦é·(ã¹ããç³») { // ã«ã¡ã©ã®æç»ç¯å²Widthãçãããã®1/2ä¸å¤®ã«å¯ããã float rectX = (resultRatio - 1.0f) / 2f; Camera.main.rect = new Rect(rectX, 0f, (2f - resultRatio), 1f); } } }
DeviceSize.cs
baseRatioã¯ã¢ã¹ãã¯ãæ¯16:9ã®æ¨ªå¹
ã«å¯¾ãã縦å¹
ã®å²åã
deviceRatioã¯å®æ©ã®æ¨ªå¹
ã«å¯¾ãã縦å¹
ã®å²åãè¨ç®ãã¦ãã¾ãã
resultRatioã¯ã«ã¡ã©ãè£æ£ããéã«ä½¿ãã¾ãã
resultRatioã1以ä¸ãªã横é·ï¼ã¿ãã¬ãããªã©ï¼ã1以ä¸ãªã縦é·ï¼iPhoneXãªã©ï¼ãªã®ã§ã
ããã«åããã¦Cameraã®Rectã調æ´ãã¾ãã
以ä¸ã§ãããããã¨ããããã¾ããã