Unity: Scaling the GUI based on the screen resolution

Unity: Scaling the GUI based on the screen resolution

Unity: Scaling the GUI based on the screen resolution thumbnail

As hinted by other posts, here, you will find how to properly scale the GUI elements based on screen resolution. As one may have noticed, Unity doesn’t scale the GUI elements based on the screen resolution, requiring a script to do the job, which is explained in this post. I will assume that the reader already knows how to create and render GUI elements in Unity using the MonoBehaviour’s OnGUI() function and GUISkin objects.

The best way to explain how to properly scale a GUI element is through an example. That said, for this post, let’s assume that we wanted a yellow rectangle to be rendered at the top left and bottom right corners of the screen, like this:

Yellow squares.

Yellow rectangles at the edges of the screen.

The first thing to have in mind when creating a GUI for multiple resolutions is that there are no absolute pixel values, only relative ones. This means that all GUI elements must be positioned at a certain distance from one (or more) screen edge(s). Since it’s not possible to create relative positions in a graphical editing software, there is the need to define a target distance from the screen edges when it has a specific resolution . In this example, we will define that the two GUI elements must have a distance of 20 pixels from the screen’s edges when the resolution is 1920×1080. This could have been any other distance for any other resolution, it’s just a place to start.

The image must be 20px from the edges. The width and height of the GUI element are going to play an important role in the script.

Finally, here’s the code that properly scales and positions the GUI based on the screen resolution:

using UnityEngine;
using System.Collections;

public class GUIScaleExample : MonoBehaviour
{
	//a GUISkin object to draw the GUI image
	public GUISkin guiSkin;

	//the GUI scale ratio
	private float guiRatio;

	//the screen width
	private float sWidth;

	//create a scale Vector3 with the above ratio
	private Vector3 GUIsF;

	//At this script initialization
	void Awake()
	{
		//get the screen's width
		sWidth = Screen.width;
		//calculate the scale ratio
		guiRatio = sWidth/1920;
		//create a scale Vector3 with the above ratio
		GUIsF = new Vector3(guiRatio,guiRatio,1);
	}

	//Draws GUI elements
	void OnGUI()
	{
		//scale and position the GUI element to draw it at the screen's top left corner
		GUI.matrix = Matrix4x4.TRS(new Vector3(GUIsF.x,GUIsF.y,0),Quaternion.identity,GUIsF);
		//draw GUI on the top left
		GUI.Label(new Rect(20,20,258,89),"",guiSkin.customStyles[0]);

		//scale and position the GUI element to draw it at the screen's bottom right corner
		GUI.matrix = Matrix4x4.TRS(new Vector3(Screen.width - 258*GUIsF.x,Screen.height - 89*GUIsF.y,0),Quaternion.identity,GUIsF);
		//draw GUI on the bottom right
		GUI.Label(new Rect(-20,-20,258,89),"",guiSkin.customStyles[0]);
	}
}

Right at the beginning of the code, a GUISkin object is declared at line 7, to render the yellow square as a GUI element. Thefloat guiRatio variable is where the ratio between the screen’s width and the expected width is stored (line 10). The variablesWidth at line 13 stores the screen’s width. The GUIsFvector will be created using the above ratio, and later on, it’s going to be used to properly scale the GUI elements (line 16).

Inside the Awake() method, all these declared variables are initialized. Line 24 is definitively the most important one. It divides the screen width by the assumed width (in our case1920) and stores it at the guiRatio variable. After that, theGUIsF scale vector is created. Again, 1920 is just a value that was determined as an example, it could have been set as any other. Alternatively, the height of the screen could be divided by 1080 if that made more sense, as a result, all GUI elements would have been scaled according to the screen’s height.

Moving on to the OnGUI() method, a“GUI.matrix = Matrix4x4.TRS “ line can be seen before each of the draw calls. These lines scale and position the wholeGUI system, changing it’s origin and scale by replacing it with a custom4×4 matrix. The Matrix4x4.TRS creates a Transform,Rotate and Translate 4×4 matrix, taking a Vector3, a Quaternion and another Vector3 as parameters.

The first time the GUI’s 4×4 Matrix is redefined (line 33), the GUI system origin is placed at(GUIsF.x,GUIsF.y,0), without any rotation (Quaternion.identity) and the elements below this line are scaled byGUIsF – a Vector3 composed by(guiRatio,guiRatio,1). Then, the GUI element is drawn in line 35 using this new matrix as origin, placing it near the top left corner of the screen.

For anything that is placed near the bottom and/or right edges of the screen, there’s an additional calculation that needs to be done to correctly modify the GUI system matrix. This calculation takes in consideration the width and height of the screen, so as the GUI element’s width and height. Line 38 is a good example. The origin is first translated to the bottom right of the screen and then, it’s is brought upwards and left just enough to draw the rescaled GUI element. Right after this compensation, line 40 draws the element, -20 pixels away from the origin.

The code described above scales the GUI elements and their distances from the screen edge. Therefore, the distance from the screen’s edge will be only 20px when the screen is at 1920x1080px. Smaller resolutions will result in smaller distances. Below, some screenshots of this script in action:

Examples screenshot.

The same script repositioning and resizing the GUI elements at a 4:3 and a 16:9 screen.

Here’s a Unity project with everything explained above, both inJavaScript andC#:

Download


01、数据简介 出口韧性是地级市在面对外部震荡和压力时,能够承受并迅速适应、应对变化的能力。这种能力体现在地级市经济结构的灵活性、创新能力和竞争力,以及地方政府的政策支持和产业调整能力等多个方面。 城市出口韧性对于城市的经济发展、就业稳定、国际贸易地位以及风险抵御能力等方面都具有重要影响。因此,城市应加强出口韧性的建设,提高应对外部冲击的能力,以推动其经济的可持续发展。 数据名称:地级市-城市出口韧性数据 数据年份:2011-2022年 02、相关数据 代码 年份 地区 城市 省份 城市出口韧性 距离港口的最近距离 最终进口额_百万人民币2 最终出口额_百万人民币2 人均道路面积2 年末金融机构各项贷款余额万元2 地区生产总值万元2 科学支出万元2 地方财政一般预算内支出万元2 城镇居民人均可支配收入元2 固定资产投资2 实际使用外商投资额百万美元2 城镇化率2 外贸依存度 出口贸易 年平均汇率 实际使用外商投资额百万人民币2 外资依存度 金融发展水平 财政投资力度 科学技术水平 出口偏离度 x_地区生产总值万元2 x_城镇化率2 x_人均道路面积2 x_外贸依存度 x_出口贸易 x_出口偏离度 x_金融发展水平 x_城镇居民人均可支配收入元2 x_财政投资力度 x_科学技术水平 x_距离港口的最近距离 x_外资依存度 地区生产总值万元2_sum y_地区生产总值万元2 城镇化率2_sum y_城镇化率2 人均道路面积2_sum y_人均道路面积2 外贸依存度_sum y_外贸依存度 出口贸易_sum y_出口贸易 出口偏离度_sum y_出口偏离度 金融发展水平_sum y_金融发展水平 城镇居民人均可支配收入元2_sum y_城镇居民人均可支配收入元2 财政投资力度_sum y_财政投资力度 科学技术水平_sum y_科学技术水平
内容概要:本文档详细介绍了一个基于Matlab实现的无人机空中通信仿真资源包,系统涵盖了无人机通信、三维路径规划、状态估计与多机协同等多个核心技术模块的仿真代码与案例研究。内容聚焦于无人机在复杂环境下的三维路径规划(如基于遗传算法GA、粒子群算法PSO、动态窗口法DWA等)、无人机姿态与轨迹的状态估计算法(如扩展卡尔曼滤波器EKF、UKF、不变扩展卡尔曼滤波IEKF、粒子滤波PF等),以及无人机通信链路建模与优化,并融合智能优化算法对系统性能进行提升。此外,资源包还拓展至微电网优化、MIMO检测、图像融合、信号处理等相关科研领域,构建了一个以无人机技术为核心、多学科交叉融合的综合性仿真研究体系。; 适合人群:具备一定Matlab编程能力与控制系统基础知识,从事无人机系统设计、无线通信、自动化控制、智能优化算法或相关领域研究的科研人员、高校研究生及工程技术人员。; 使用场景及目标:①开展无人机通信系统建模与性能仿真分析;②实现复杂动态环境中无人机三维路径规划与实时避障;③研究基于多源传感器融合的无人机导航与状态估计方法;④结合智能优化算法提升无人机任务执行效率与系统鲁棒性; 阅读建议:建议读者依据资源包提供的模块化结构系统学习,优先掌握Matlab/Simulink基本仿真技能,重点研读路径规划与状态估计部分的算法实现与代码细节,并通过实际调试与二次开发加深对无人机系统集成与优化策略的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值