我想在相机视图内创建一个带有预制件的类似视图的网格,但我无法这样做。预制件正在超出摄像机视图。
我想将预制件放置为 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;
}
}
}
您可以使用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
是相机和物体之间的距离。