您的位置:首页 > 移动开发 > Unity3D

unity中全屏背景图缩放

2015-07-15 16:34 681 查看
using UnityEngine;
using System.Collections;

public class BgPicScript : MonoBehaviour {

// Use this for initialization
public UITexture bg;
void Start () {

Debug.Log("device宽高: " + Screen.width + "_" + Screen.height);

UIRoot root = GameObject.FindObjectOfType<UIRoot>();

if (root != null)
{
//终极大杀器 整体资源缩放比例
float s = (float)root.activeHeight / Screen.height;
int height =  Mathf.CeilToInt(Screen.height * s);
int width = Mathf.CeilToInt(Screen.width * s);

Debug.Log("按设计分辨率屏幕width = " + width + "_height = " + height);

Debug.Log("背景图片的设计分辨率bgWIdth:" + bg.width + "_bgHeight:" + bg.height);

float bgRatio = (float) bg.width / bg.height;
float manualRatio = (float)width / height;

if (bgRatio < manualRatio) //遇到按照屏幕高度缩放水平两边出现黑边的情况,要按照宽度来缩放背景图片
{
//bg.width = width;
//bg.height = Mathf.CeilToInt(width / );

float targetScale = (float)width / bg.width;
bg.transform.localScale = new Vector3(targetScale,targetScale, 1);
}
}

}

// Update is called once per frame
void Update () {

}
}

using UnityEngine;
using System.Collections;

public class UIRootScript : MonoBehaviour {

// Use this for initialization
void Start () {
AdaptiveUI();
}

// Update is called once per frame
void Update () {

}

static private void AdaptiveUI()
{
int ManualWidth = 1024;
int ManualHeight = 768;
UIRoot uiRoot = GameObject.FindObjectOfType<UIRoot>();
if (uiRoot != null)
{
if (System.Convert.ToSingle(Screen.height) / Screen.width > System.Convert.ToSingle(ManualHeight) / ManualWidth)
uiRoot.manualHeight = Mathf.RoundToInt(System.Convert.ToSingle(ManualWidth) / Screen.width * Screen.height);
else
uiRoot.manualHeight = ManualHeight;
}
Debug.Log("uiRoot Height:" + uiRoot.manualHeight);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: