简体   繁体   中英

How to fix a Unity 2d object to the edge of a screen?

I am making a game in Unity, where Objects are spawned at the awake state into a grid type pattern. I want the first object to spawn on the left edge of the screen and the others to follow. And I have got it working more or less correctly, when I test in unity's built in game tester. I even try it in different resolutions and it still looks ok.
However, when I build the project and then test it, the objects are all off camera and it looks nothing like the preview build.
I have tried many various suggestions offered by what I could find on google, and they don't seem to be any better

The way I am instantiating the first object is to first get the X value of the edge of the screen by using

int ix = -(Camera.main.scaledPixelWidth / 2)

And then I will add half the width of the object, using

var renderer = clone.GetComponent<Renderer>();
float width = renderer.bounds.size.x / 2f;
ix += (int) width;

I've also tried using the screen.width and Camera.main.PixelWidth values, and all are the same... They look fine in all the aspect ratios on the built in game preview for Unity, but when I do a build for Windows or WebGL the objects are starting offscreen and only the last few will render on screen.
Does any one have any suggestions for a way to get objects to render exactly on the edge no matter the resolution?

I realise it may work better if the objects are manually laid out, but the objects need to be rendered at the start of the level as the idea is to reuse one scene and load in assets depending on a parsed xml file, as it's a puzzle game and I want users to be able to create their own puzzles as well ultimately.

Since you already are speaking of UI elements inside a Canvas all you need to do is set the anchors and pivot points correctly.

Either do this already in your prefab

在此处输入图像描述

or later via code eg

var rectTransform = clone.GetComponent<RectTransform>();
var anchorsVector = new Vector2(0,0.5f);
rectTransform.pivot = anchorsVector;
rectTransform.anchorMin = anchorsVector;
rectTransform.acnhorMax = anchorsVector;

now all you need to do is set this object to a position x=0 to "attach" it to the left edge. With this current setting y=0 means centered vertically. You can of course change this to your needs.

rectTransform.anchoredPosition = new Vector2(0, 0);

As in this little demo

public class Example : MonoBehaviour
{
    [ContextMenu("Test")]
    private void Test()
    {
        var rectTransform = GetComponent<RectTransform>();
        var anchorsVector = new Vector2(0,0.5f);
        rectTransform.pivot = anchorsVector;
        rectTransform.anchorMin = anchorsVector;
        rectTransform.anchorMax = anchorsVector;

        rectTransform.anchoredPosition = new Vector2(0, 0);
    }
}

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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