如何在侧面摄像机视图中使用网格实例化预制件

时间:2019-05-07 04:35:45

标签: unity3d unity-container

我想用摄像机视图内部的预制件创建像网格这样的视图,但是我无法这样做。预制件即将退出相机视野。谁能帮我解决这个问题?我想将预制件放置为网格和9 * 6网格。

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;

            }
        }
}

1 个答案:

答案 0 :(得分: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是相机和物体之间的距离。