牛骨文教育服务平台(让学习变的简单)
博文笔记

UGUI研究院之设置全屏图

创建时间:2016-09-25 投稿人: 浏览次数:1325
rectTransform.sizeDelta = new Vector2(Screen.width,Screen.height);

在UGUI中想设置一张全屏的背景图,但是直接设置 screen.width和screen.height后发现在有些分辨率下还是不能全屏。


把如下脚本挂在需要全屏的Image对象上即可。

using UnityEngine;
using System.Collections;
 
public class UIMaskLayer : MonoBehaviour
{
	void Start()
	{
		int width = Screen.width;
		int height = Screen.height;
		int designWidth = 960;//开发时分辨率宽
		int designHeight = 640;//开发时分辨率高
		float s1 = (float)designWidth / (float)designHeight;
		float s2 = (float)width / (float)height;
		if(s1 < s2) {
			designWidth = (int)Mathf.FloorToInt(designHeight * s2);
		} else if(s1 > s2) {
			designHeight = (int)Mathf.FloorToInt(designWidth / s2);
		}
		float contentScale = (float)designWidth/(float)width;
		RectTransform rectTransform = this.transform as RectTransform;
		if(rectTransform != null){
			rectTransform.sizeDelta = new Vector2(designWidth,designHeight);
		}
	}
}


声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。