您的位置:首页 > 移动开发 > Unity3D

unity 手机分辨率适配

2017-06-06 18:40 155 查看
摄像机 的适配

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配合 使用 即可达到适配效果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: