如何让unity3d自适应分辨率比如有16:9和16:10

 我来答
10猜去
2017-12-06 · 超过10用户采纳过TA的回答
知道答主
回答量:173
采纳率:15%
帮助的人:23.7万
展开全部
? 胡笳汉月 ( 2006) ? 昭君出塞 ( 2006) ? 暗哨 ( 2006)
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
羌无意识肿
2017-10-08 · TA获得超过151个赞
知道答主
回答量:126
采纳率:0%
帮助的人:30.6万
展开全部
离离原上草,
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2014-12-29
展开全部
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);
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 3条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式