public void ScreenUISelfAdptation(Transform scaleUI)
{
float widthrate = UnityEngine.Screen.width / 1920.0f;
float heightrate = UnityEngine.Screen.height / 1080.0f;
float postion_x = scaleUI.GetComponent<RectTransform>().anchoredPosition.x * widthrate;
float postion_y = scaleUI.GetComponent<RectTransform>().anchoredPosition.y * heightrate;
scaleUI.localScale = new Vector3(widthrate, heightrate, 1);
scaleUI.GetComponent<RectTransform>().anchoredPosition = new Vector2(postion_x, postion_y);
}
UnityEngine.Screen.width和UnityEngine.Screen.height是你要发布的屏幕分辨率大小。
1920.0f; x 1080.0f;是你制作UI时Game视图中的分辨率大小。
本文介绍了一种在Unity中实现UI元素自适应不同屏幕分辨率的方法。通过调整UI元素的位置和大小,确保在不同设备上都能保持良好的显示效果。具体实现包括根据目标分辨率计算缩放比例,并据此调整UI元素的位置。
2921

被折叠的 条评论
为什么被折叠?



