Last active
August 29, 2015 14:10
-
-
Save nutti/60258b86d54d71334b38 to your computer and use it in GitHub Desktop.
[Unity] 複数の端末の解像度に対応させる1 - Sceneに表示される解像度を固定化する ref: http://qiita.com/nutti/items/733f296c5a09b6867baa
This file contains hidden or 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
var offset : Vector2 = Vector2(0.0f, 0.0f); // 中心からのずれ | |
var scale : Vector2 = Vector2(1.0f, 1.0f); // カメラの拡大率 | |
var cam : Camera = null; // アスペクト比を固定化するカメラ | |
private var width : float = 640.0f; // 横幅 (目標解像度) | |
private var height : float = 960.0f; // 高さ (目標解像度) | |
var targetAspect : float; // 目標のアスペクト比 | |
var curAspect : float; // 補正前の「Scene」のアスペクト比 | |
var ratio : float; // 補正前の「Scene」のアスペクト比 ÷ 目標のアスペクト比 | |
targetAspect = width / height; | |
curAspect = Screen.width * 1.0f / Screen.height; // 補正前の「Scene」の横幅・縦幅はSceneのメンバ変数から取得可能 | |
ratio = curAspect / targetAspect; | |
// 表示領域の横幅・高さ・左上のXY座標をセット | |
// 横長の場合 | |
if (1.0f > ratio) { | |
cam.rect.x = 0.0f + offset.x; | |
cam.rect.width = 1.0f; | |
cam.rect.y = (1.0f - ratio) / 2.0f + offset.y; | |
cam.rect.height = ratio; | |
cam.orthographicSize = Screen.width / (2.0f * scale.x); | |
} | |
// 縦長の場合 | |
else { | |
ratio = 1.0f / ratio; | |
cam.rect.x = (1.0f - ratio) / 2.0f + offset.x; | |
cam.rect.width = ratio; | |
cam.rect.y = 0.0f + offset.y; | |
cam.rect.height = 1.0f; | |
cam.orthographicSize = Screen.height / (2.0f * scale.y); | |
} |
This file contains hidden or 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
// Aspect Ratio Controller Ver 1.0 | |
#pragma strict | |
#if UNITY_EDITOR | |
var resolution : Vector2 = Vector2(Screen.width, Screen.height); | |
var targetDevice : iPhoneGeneration; | |
#endif | |
var enable : boolean = true; // enable fixed aspect ratio | |
var offset : Vector2 = Vector2(0.0f, 0.0f); // offset | |
var scale : Vector2 = Vector2(1.0f, 1.0f); // scaling | |
var cam : Camera = null; // 'Camera' object | |
private var width : float = 640.0f; // width (actual resolution) | |
private var height : float = 960.0f; // height (actual resolution) | |
private var camRect : Rect; // camera's rectangle | |
function Start() | |
{ | |
SetActualResolution(); | |
} | |
function Update() | |
{ | |
var targetAspect : float; | |
var curAspect : float; | |
var ratio : float; | |
if (!enable) { return; } | |
if (!cam) { return; } | |
#if UNITY_EDITOR // for Unity editor | |
SetEditorActualResolution(targetDevice); | |
#endif | |
targetAspect = width / height; | |
curAspect = Screen.width * 1.0f / Screen.height; | |
ratio = curAspect / targetAspect; | |
if (1.0f > ratio) { | |
cam.rect.x = 0.0f + offset.x; | |
cam.rect.width = 1.0f; | |
cam.rect.y = (1.0f - ratio) / 2.0f + offset.y; | |
cam.rect.height = ratio; | |
cam.orthographicSize = Screen.width / (2.0f * scale.x); | |
} | |
else { | |
ratio = 1.0f / ratio; | |
cam.rect.x = (1.0f - ratio) / 2.0f + offset.x; | |
cam.rect.width = ratio; | |
cam.rect.y = 0.0f + offset.y; | |
cam.rect.height = 1.0f; | |
cam.orthographicSize = Screen.height / (2.0f * scale.y); | |
} | |
camRect = cam.rect; | |
} | |
private function SetActualResolution() | |
{ | |
#if UNITY_EDITOR // for Unity editor | |
SetEditorActualResolution(targetDevice); | |
#elif UNITY_IPHONE // for iPhone/iPad | |
SetiPhoneActualResolution(iPhone.generation); | |
#elif UNITY_ANDROID // for Android | |
SetAndroidActualResolution(); | |
#endif | |
} | |
private function SetEditorActualResolution(device : iPhoneGeneration) | |
{ | |
if (device == iPhoneGeneration.Unknown) { | |
width = resolution.x; | |
height = resolution.y; | |
} | |
else{ | |
SetiPhoneActualResolution(device); | |
} | |
} | |
private function SetiPhoneActualResolution(generation : iPhoneGeneration) | |
{ | |
switch (generation) { | |
case iPhoneGeneration.iPhone4S: | |
width = 640.0f; | |
height = 960.0f; | |
break; | |
case iPhoneGeneration.iPhone5: | |
case iPhoneGeneration.iPhone5C: | |
case iPhoneGeneration.iPhone5S: | |
width = 640.0f; | |
height = 1136.0f; | |
break; | |
case iPhoneGeneration.iPad1Gen: | |
case iPhoneGeneration.iPad2Gen: | |
width = 768.0f; | |
height = 1024.0f; | |
break; | |
case iPhoneGeneration.iPad3Gen: | |
case iPhoneGeneration.iPad4Gen: | |
case iPhoneGeneration.iPad5Gen: | |
case iPhoneGeneration.iPadMini2Gen: | |
width = 1536.0f; | |
height = 2048.0f; | |
break; | |
default: | |
width = 640.0f; | |
height = 960.0f; | |
break; | |
} | |
} | |
private function SetAndroidActualResolution() | |
{ | |
height = 1280.0f; | |
width = 800.0f; | |
} | |
// get actual resolution width | |
function GetWidth() : float | |
{ | |
return width; | |
} | |
// get actual resolution height | |
function GetHeight() : float | |
{ | |
return height; | |
} | |
// get camera rect | |
function GetCameraRect() : Rect | |
{ | |
return camRect; | |
} | |
@script AddComponentMenu( "Colorful Pico/Lib/Cross Platform/Aspect Ratio Controller" ) |
This file contains hidden or 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
#pragma strict | |
var cam : Camera = null; // アスペクト比を固定化するカメラ | |
var width : float = 640.0f; // 横幅 (目標解像度) | |
var height : float = 960.0f; // 高さ (目標解像度) | |
var camRect : Rect; | |
function Update() | |
{ | |
var targetAspect : float; // 目標のアスペクト比 | |
var curAspect : float; // 補正前の「Scene」のアスペクト比 | |
var ratio : float; // 補正前の「Scene」のアスペクト比 ÷ 目標のアスペクト比 | |
targetAspect = width / height; | |
// 補正前の「Scene」の横幅・縦幅はSceneのメンバ変数から取得可能 | |
curAspect = Screen.width * 1.0f / Screen.height; | |
ratio = curAspect / targetAspect; | |
// 表示領域の横幅・高さ・左上のXY座標をセット | |
// 横長の場合 | |
if (1.0f > ratio) { | |
cam.rect.x = 0.0f; | |
cam.rect.width = 1.0f; | |
cam.rect.y = (1.0f - ratio) / 2.0f; | |
cam.rect.height = ratio; | |
cam.orthographicSize = Screen.width / 2.0f; | |
} | |
// 縦長の場合 | |
else { | |
ratio = 1.0f / ratio; | |
cam.rect.x = (1.0f - ratio) / 2.0f; | |
cam.rect.width = ratio; | |
cam.rect.y = 0.0f; | |
cam.rect.height = 1.0f; | |
cam.orthographicSize = Screen.height / 2.0f; | |
} | |
camRect = cam.rect; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment