unity 手机分辨率适配

摄像机 的适配

public class UICameraAdjustor : MonoBehaviour
{


// the design size
public float standard_width = 576f;
public float standard_height = 1024f;


// the screen size
float device_width = 0f;
float device_height = 0f;

void Awake ()
{
device_width = Screen.width;
device_height = Screen.height;
SetCameraSize ();
}

private void SetCameraSize ()
{
float adjustor = 0f;
float standard_aspect = standard_width / standard_height;
float device_aspect = device_width / device_height;

if (device_aspect < standard_aspect) {
adjustor = standard_aspect / device_aspect;
camera.orthographicSize = adjustor;
}
}


}


ui适配 


public class UIBackgroundAdjustor : MonoBehaviour
{


// the design size
public float standard_width = 576f;
public float standard_height = 1024f;
void Awake ()
{

SetBackgroundSize ();
}

private void SetBackgroundSize ()
{
float device_width = Screen.width;
float device_height = Screen.height;
if (transform != null) {


float standard_aspect = standard_width / standard_height;
float device_aspect = device_width / device_height;

float scale = 0f;

if (device_aspect > standard_aspect) { //按宽度适配
scale = device_aspect / standard_aspect;


transform.localScale = new Vector3 (scale, 1, 1);
} else { //按高度适配
scale = standard_aspect / device_aspect;


transform.localScale = new Vector3 (1, scale, 1);
}
}
}




} // 


摄像机与  ui配合 使用 即可达到适配效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值