繁体   English   中英

基于屏幕尺寸定位实例化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:

选择您的画布,然后在检查器中选择屏幕尺寸比例,并为您的游戏设置最高分辨率,始终在其上基于GUI,并且在游戏视图中不使用自由宽高比,始终使用您要查看的分辨率,但仍然尝试通过将枢轴拖动到面板的底端或整个画布的底部来手动更改枢轴,这样只会改变高度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM