如何实例化预制件,并将视图作为侧摄像头视图中的网格

问题描述 投票:0回答:1

我想在相机视图内创建一个带有预制件的类似视图的网格,但我无法这样做。预制件正在超出摄像机视图。

我想将预制件放置为 9x6 网格。

这是实例化预制件的代码:

public GameObject tilePrefab;
Vector2 mapSize;

void Start()
{
    createGrid();
    Debug.Log("Screen Width : " + Screen.width);
    Debug.Log("Screen Height : " + Screen.height);
    mapSize = new Vector2(Screen.width/9,Screen.height/12);
    Debug.Log("mapSize : " + mapSize);
}

void createGrid()
{
    for (int x = 0; x < 9; x++)
    {
        for (int y = 0; y < 6; y++)
        {
            Vector3 tilePosition = new Vector3(-mapSize.x+0.5f + x,-mapSize.y + 0.5f+y ,0 );
               
            GameObject ballclone = (GameObject)Instantiate(tilePrefab,tilePosition,Quaternion.identity);
            ballclone.transform.parent = transform;
        }
    }
}
c# unity-game-engine
1个回答
1
投票

您可以使用ViewportToWorldPoint

for (int x = 0; x < 9; x++)
    for (int y = 0; y < 6; y++)
         xx.position = camera.ViewportToWorldPoint(new Vector3(1f/9*x, 1f/6*y, distance));

distance
是相机和物体之间的距离。

© www.soinside.com 2019 - 2024. All rights reserved.