Getting height/width from a unity gameobject

时间:2015-10-30 22:09:17

标签: c# unity3d unity3d-5

I'm working on a version of Conway's Game of Life in Unity, and here is my setup for making the grid:

I've created a prefab of an individual cell responding to mouse click that will be the basis for creating the cell grid. I have a Empty GameObject to act as the controller to create the grid. I'm putting it in the code for the controller like so, pointing my prefab to the field:

[SerializeField]
private GameObject Cell;

private Camera _camera;

My idea was to get the dimensions of the Cell and instantiate it into a grid, with _camera pointing to the Main camera to get boundaries. However, I'm not sure how to get the height/width from GameObject. What's the best way to find this out?

1 个答案:

答案 0 :(得分:5)

我不知道你是否找到答案,但最常见的方法是使用Collider(如果你有一个,但鼠标点击需要它)或渲染器(如果你有网格)使用:

GetComponent<Collider>().bounds.size
GetComponent<Renderer>().bounds.size

生活的游戏非常好,我已经写了关于它的单身汉研讨论文。玩得开心!