第一次写文章!
unity3d 开发项目适配问题处理,实现的结果是宽高比超出的部分相机直接裁掉留黑,如果要加背景可以再挂一个相机填补黑色,代码以横屏为例
using UnityEngine;
/// <summary>
/// 屏幕适配
/// </summary>
[RequireComponent(typeof(Camera))]
public class ScreenAdapt : MonoBehaviour
{
public float screenHeight;
public float screenWidth;
void Start()
{
UpdateCamera ();
}
void UpdateCamera ()
{
screenHeight = Screen.height;
screenWidth = Screen.width;
Rect r = new Rect();
if(screenHeight / screenWidth > 0.61f)//更方的屏幕 480*800
{
r.width = 1;
r.height = (screenWidth * 0.61f) / screenHeight;
r.x = 0;
r.y = (1 - r.height) / 2f;
}
else if(screenHeight / screenWidth < 0.56f)//更长的屏幕480*854
{
r.width = (screenHeight / 0.56f) / screenWidth;
r.height = 1f;
r.x = (1 - r.width) / 2f;
r.y = 0;
}
else //在可适配区域不做处理480*800 - 480*854之间的
{
r.width = 1;
r.height = 1;
r.x = 0;
r.y = 0f;
}
GetComponent<Camera>().rect = r;
}
}
直接将脚本挂在需要适配的相机上即可,
原理就是修改的相机的视口
这样处理后,即可完美适配,建议pause/resume 后做一次刷新
http://blog.csdn.net/c5138891/article/details/52512881