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