如何让unity3d自适应分辨率比如有16:9和16:10的各种屏幕,尤其是当使用GUI时候

 我来答
liuwenjiexx
2012-02-02 · 超过14用户采纳过TA的回答
知道答主
回答量:22
采纳率:0%
帮助的人:23.6万
展开全部
public override void GUIDraw (){
//开始缩放
//先定义一个标准屏幕大小代码按照这个分辨率写,这里屏是800*480象素,GUI矩阵再乘以新的缩放矩阵实现GUI的缩放,GUI.matrix*=scaleMatrix;
ScreenScaleUtil.BeginScreenScale ();

//GUI代码
GUI...

//结束缩放
ScreenScaleUtil.EndScreenScale ();
}

ScreenScaleUtil.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public enum ScreenScaleType
{
None,
Center,
Fit,
Full
}

public static class ScreenScaleUtil
{

static ScreenScaleUtil ()
{
scaleRate=1;
scaleType = ScreenScaleType.None;
scaleMatrix = Matrix4x4.identity;
screenSize = new Vector2 (800, 480);

ScaleType=ScreenScaleType.Fit;

lstMats =new List<Matrix4x4>();

}

private static Matrix4x4 scaleMatrix;
private static ScreenScaleType scaleType;
private static Vector2 screenSize;
private static float scaleRate;

public static Matrix4x4 ScaleMatrix {
get {return scaleMatrix; }
}

public static ScreenScaleType ScaleType {
get { return scaleType; }
set {
if (value != scaleType) {

scaleType = value;

scaleMatrix = GetScreenScaleMatrix (screenSize, scaleType,out scaleRate);
}
}
}

public static Vector2 ScreenSize {
get { return screenSize; }
set {
if (value != screenSize) {
screenSize = value;

scaleMatrix = GetScreenScaleMatrix (screenSize, scaleType,out scaleRate);
}

}
}

public static float ScaleRate{
get{
return scaleRate;
}
}

public static void Update(){
scaleMatrix = GetScreenScaleMatrix (screenSize, scaleType,out scaleRate);
}

public static Matrix4x4 GetScreenScaleMatrix (Vector2 screenSize, ScreenScaleType scaleType)
{
float rate;
return GetScreenScaleMatrix(screenSize,scaleType,out rate);
}

public static Matrix4x4 GetScreenScaleMatrix (Vector2 screenSize, ScreenScaleType scaleType,out float rate)
{
Matrix4x4 scaleMatrix;
rate = 1;

if (scaleType == ScreenScaleType.None) {
scaleMatrix = Matrix4x4.identity;

} else {

if ((Screen.width / (float)Screen.height) < (screenSize.x / screenSize.y)) {

if (scaleType == ScreenScaleType.Fit) {
rate = Screen.width / screenSize.x;
} else if (scaleType == ScreenScaleType.Full) {
rate = Screen.height / screenSize.y;
}
} else {
if (scaleType == ScreenScaleType.Fit) {
rate = Screen.height / screenSize.y;
} else if (scaleType == ScreenScaleType.Full) {
rate = Screen.width / screenSize.x;
}
}
scaleMatrix = Matrix4x4.TRS (new Vector3 ((Screen.width - screenSize.x * rate) / 2.0f, (Screen.height - screenSize.y * rate) / 2.0f, 0), Quaternion.identity, new Vector3 (rate, rate, 1));

}
return scaleMatrix;
}

private static List<Matrix4x4> lstMats;

public static void BeginScreenScale(){

if(lstMats.Count>0){
return;
}

lstMats.Add(GUI.matrix);
GUI.matrix*=ScaleMatrix;

}

public static void BeginScreenScale(ScreenScaleType scaleType ){

if(lstMats.Count>0){
return;
}

lstMats.Add(GUI.matrix);
GUI.matrix*=GetScreenScaleMatrix(screenSize,scaleType);

}

public static void EndScreenScale(){
if(lstMats.Count<=0)
return;

GUI.matrix=lstMats[lstMats.Count-1];

lstMats.RemoveAt(lstMats.Count-1);
}

}
东莞大凡
2024-08-07 广告
OpenCV标定板是东莞市大凡光学科技有限公司在相机标定中常用的工具。它通常由黑白格点按一定规则排列在平面上组成,如棋盘格或圆形格等。在相机标定时,将标定板置于不同位置和姿态下拍摄图像,利用OpenCV库中的函数检测标定板上的角点或圆心,进... 点击进入详情页
本回答由东莞大凡提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式