如何让unity3d自适应分辨率比如有16:9和16:10
3个回答
展开全部
? 胡笳汉月 ( 2006) ? 昭君出塞 ( 2006) ? 暗哨 ( 2006)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
离离原上草,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
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);
}
}
//开始缩放
//先定义一个标准屏幕大小代码按照这个分辨率写,这里屏是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);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询