牛骨文教育服务平台(让学习变的简单)
博文笔记

unity 手机分辨率适配

创建时间:2017-06-06 投稿人: 浏览次数:773

摄像机 的适配

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配合 使用 即可达到适配效果

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。