在场景中的Camera挂上CameraSize.cs脚本:
using UnityEngine;
//Platform:Android
public class CameraSize : MonoBehaviour
{
private float defaultcamSize; //初始时设置的Camera的Size
//默认状态下的宽与高
private float defaultScreenWidth = 800f;
private float defaultScreenHeight = 480f;
private float w;
private float h;
private float newSize;
void Awake()
{
defaultcamSize = this.camera.orthographicSize;
if (Screen.currentResolution.width != defaultScreenWidth || Screen.currentResolution.height != defaultScreenHeight)
{
//print(Screen.width + "," + Screen.height);
w = defaultScreenWidth / Screen.width;
h = defaultScreenHeight / Screen.height;
newSize = w / h;
this.camera.orthographicSize = defaultcamSize * newSize;
//print(newSize);
}
}
}
在场景中设置了裁剪区域的Panel上挂上panelClipRangeResize.cs脚本:
using UnityEngine;
//Panel面板的裁剪区域的Y轴的自适应
public class panelClipRangeResize : MonoBehaviour
{
public Camera uicamera;
//private int defaultScreenWidth = 800;
//private int defaultScreenHeight = 480;
//private float w;
//private float h;
/// <summary>
/// w修改宽度:x-z
/// h修改高度:y-w
/// </summary>
void Awake()
{
//w = defaultScreenWidth / Screen.width;
//h = defaultScreenHeight / Screen.height;
Vector4 clipRange = this.GetComponent<UIPanel>().clipRange;
this.GetComponent<UIPanel>().clipRange = new Vector4(clipRange.x, clipRange.y+clipRange.w*(1-uicamera.orthographicSize)/2, clipRange.z, clipRange.w * uicamera.orthographicSize);
}
}