我的 Unity2D 屏幕适配

原文链接: http://www.cnblogs.com/daihanlong/p/4655562.html

以下方法纯属我YY,切勿当真!!!

确定一个设计尺寸,比如 devWidth = 960,devHeight = 640, 按照这个尺寸进行设计游戏.

方式一: 不管什么屏幕尺寸,都和设计的尺寸对应.

只需在 Camera上 加上如下脚本:

 

public class ScreenAdapter : MonoBehaviour {

	private Camera mainCamera;

	private static float devWidth = 960f;
	private static float devHeight = 640f;
	private static float devAspect = devWidth / devHeight;

	// Use this for initialization
	void Start () {
		mainCamera = Camera.main;

		mainCamera.aspect = devAspect;
		mainCamera.orthographicSize = devHeight * 0.01f / 2;
	}
}

方式二: 通过调整 Camera.orthographicSize 进行适配.

   将如下代码放置到 MainCamera 上

 

public class ScreenAdapter : MonoBehaviour
{
	 
	private Camera mainCamera;
	 
	private static float devWidth = 9.6f;
	private static float devHeight = 6.4f;
	private static float devAspect = devWidth / devHeight;

	void Awake(){
		
		mainCamera = Camera.main;

		// 1.调整 camera 属性
		if (mainCamera.aspect < devAspect) {
			mainCamera.orthographicSize = devWidth  * 0.5f/mainCamera.aspect;
		}
	}
}

 

 对于上面两种方法,所有的坐标全部按照 设计尺寸去计算,统一了坐标.

对于方式2,可能 上下或者左右有黑边的情况,只需要将背景进行放大一些就可以了.  

所以:推荐使用方式2进行适配.

 

 

           

转载于:https://www.cnblogs.com/daihanlong/p/4655562.html

你可能感兴趣的:(我的 Unity2D 屏幕适配)