根据屏幕尺寸定位实例化 UI 预制件的代码?
Code to position instantiated UI prefab based on screen size?
我正在为如何根据屏幕尺寸定位实例化预制件而苦苦挣扎,我的以下代码是这样的:=
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UPStand : MonoBehaviour
{
public GameObject SaleButtonPrefab;
public GameObject ParentButton;
public int
void Start()
{
//To loop all the list
for(int i = 0; i < 3; i++)
{
//Instantiate the button prefab and make a parent
GameObject nu = Instantiate(SaleButtonPrefab) as GameObject;
nu.transform.SetParent(ParentButton.transform, false);
//To set the position and function
nu.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, (i*-289.5f) );// <-- This positioning does not position with screen the size
}
}
}
当我在 720x1280 的分辨率下 运行 时,效果完美,如下图所示:
但是当我运行这个分辨率为900x1100时,高度的定位与下图所示相当不同:
我尝试将锚点位置设置为图像的角,但它在缩放方面有效,但在定位方面无效,有什么想法吗?
提前致谢 c:
Select 你的 canvas 并在检查器中选择屏幕大小的比例并输入你想要的游戏的最高分辨率,始终将你的 GUI 基于它并且不要在游戏视图总是使用你想要看到的分辨率,但仍然尝试通过将它们拖动到面板的向下端或整个 canvas 的底部来手动更改枢轴,这样只会改变高度。
我正在为如何根据屏幕尺寸定位实例化预制件而苦苦挣扎,我的以下代码是这样的:=
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UPStand : MonoBehaviour
{
public GameObject SaleButtonPrefab;
public GameObject ParentButton;
public int
void Start()
{
//To loop all the list
for(int i = 0; i < 3; i++)
{
//Instantiate the button prefab and make a parent
GameObject nu = Instantiate(SaleButtonPrefab) as GameObject;
nu.transform.SetParent(ParentButton.transform, false);
//To set the position and function
nu.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, (i*-289.5f) );// <-- This positioning does not position with screen the size
}
}
}
当我在 720x1280 的分辨率下 运行 时,效果完美,如下图所示:
但是当我运行这个分辨率为900x1100时,高度的定位与下图所示相当不同:
我尝试将锚点位置设置为图像的角,但它在缩放方面有效,但在定位方面无效,有什么想法吗?
提前致谢 c:
Select 你的 canvas 并在检查器中选择屏幕大小的比例并输入你想要的游戏的最高分辨率,始终将你的 GUI 基于它并且不要在游戏视图总是使用你想要看到的分辨率,但仍然尝试通过将它们拖动到面板的向下端或整个 canvas 的底部来手动更改枢轴,这样只会改变高度。