簡體   English   中英

Unity將預制件對齊到左上方(2D游戲)

[英]Unity Align prefab to top left (2D Game)

我正在制作我的第一款游戲,其中腳本將障礙物(預制件)置於場景中。 它們在2D環境中的大小均不同。 我在下面使用此代碼放置它們

Instantiate(normal1, new Vector3(x, y , 0), Quaternion.identity);

這很完美,除了我需要將所有障礙物從左上方定位。 因此,如果我的x和y分別為0、0,則僅障礙物的角將覆蓋0、0的位置,而不是整個對象。 從我使用GUI元素的經驗來看,您可以根據自己的喜好進行調整。 預制件是否一樣?如果是,怎么做? 還是可以以某種方式移動對象的原點?

我假設您正在談論非UI元素。

最簡單的方法是為您的對象提供一個父GameObject並以某種方式排列它們,以使父GameObject在您想要的位置(左上角)已經具有軸心。 為此,您首先要正確定位(未來)父對象,然后將子對象簡單地拖入層次結構中(同時保持其當前位置)。

在此處輸入圖片說明

然后在腳本中,您必須使用Camera.ScreenToWorldPoint才能找到3D世界中屏幕左上角的位置,例如

// an optional offset from the top-left corner in pixels
Vector2 PixelOffsetFromTopLeft;

// Top Left pixel is at 
// x = 0
// y = Screen height

// and than add the desired offset
var spawnpos = new Vector2(0 + PixelOffsetFromTopLeft.x, Screen.height - PixelOffsetFromTopLeft.y);

// as z we want a given distance to camera (e.g. 2 Units in front)
spawnedObject = Instantiate(Prefab, camera.ScreenToWorldPoint(new Vector3(spawnpos.x, spawnpos.y, 2f)), Quaternion.identity);

我以完整腳本為例

public class Spawner : MonoBehaviour
{
    public GameObject Prefab;

    public Vector2 PixelOffsetFromTopLeft = Vector2.zero;

    private GameObject spawnedObject;
    private Camera camera;

    private void Start()
    {
        camera = Camera.main;
    }

    private void Update()
    {
        if (Input.anyKeyDown)
        {
            // you don't need this I just didn't want to mess up the scene
            // so I just destroy the last spawned object before placing a new one
            if (spawnedObject)
            {
                Destroy(spawnedObject);
            }

            // Top Left pixel is at 
            // x = 0
            // y = Screen height

            // and than add the desired offset
            var spawnpos = new Vector2(0 + PixelOffsetFromTopLeft.x, Screen.height - PixelOffsetFromTopLeft.y);

            // as z we want a given distance to camera (e.g. 2 Units in front)
            spawnedObject = Instantiate(Prefab, camera.ScreenToWorldPoint(new Vector3(spawnpos.x, spawnpos.y, 2f)), Quaternion.identity);
        }
    }
}

在此處輸入圖片說明

現在,這將始終生成錨定在左上方的對象。 如果相機移動或調整窗口大小,則不會保持這種方式。


如果您的問題是在調整窗口大小時也要保持該位置,則可以使用相同的代碼,例如在Update方法中(出於效率的考慮,您僅應在真正需要時/實際調整窗口大小時調用它),例如

public class StickToTopLeft : MonoBehaviour
{
    private Camera camera;

    public Vector2 PixelOffsetFromTopLeft = Vector2.zero;

    private void Start()
    {
        camera = Camera.main;
    }

    // Update is called once per frame
    private void Update()
    {
        var spawnpos = new Vector2(0 + PixelOffsetFromTopLeft.x, Screen.height - PixelOffsetFromTopLeft.y);

        // This time simply stay at the current distance to camera
        transform.position = camera.ScreenToWorldPoint(new Vector3(spawnpos.x, spawnpos.y, Vector3.Distance(camera.transform.position, transform.position)));
    }
}

現在,在調整窗口大小之后,這也使對象始終固定在左上角,並允許隨后調整其偏移量。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM