unity 获取物体尺寸



unity3d中获得物体的size

以size的x方向为例

1:gameObject.renderer.bounds.size.x;//这个值的结果真实反应出有MeshRenderer这个组件的模型的尺寸。不须要再乘以localScale.x。


2:gameObject.GetComponent<MeshFilter>().mesh.bounds.size.x;//通过MeshFilter获得原始模型的mesh,该值返回的结果是原始mesh的尺寸。

若要获得模型的尺寸大小还须要乘以模型的localScale.x。

即:gameObject.GetComponent<MeshFilter>().mesh.bounds.size.x*gameObject.transform.localScale.x;


3:为物体加入Collider,然后使用XXX.collider.bounds.size;

这个不一定能非常好的反应物体的大小,bounds获得的是物体的外包矩形。

并且这个外包矩形的X,Y,Z和世界坐标一致。因此,若物体有旋转,获得的尺寸就不能反应出物体的真实大小,仅仅是其外包矩形的大小。。。

如:获得terrain的尺寸

        terrainWidth = terrain.collider.bounds.size.x;

        terrainLength = terrain.collider.bounds.size.z;

        terrainHeight = terrain.collider.bounds.size.y;

4:代码实现获得复杂物体的尺寸(诸如根节点没有MeshFilter,MeshRenderer组件,物体是由非常多复杂不规则的小mesh子物体组成的)
如:





Camera的口径Size

当投影类型为Perspective时,fieldOfView属性表示口径的度数。范围为[1,179]

当投影类型为Orthgraphic,     orthographicSize属性为正交模式下的口径尺寸

原文地址:https://www.cnblogs.com/jzssuanfa/p/6955179.html