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屬性為正交模式下的口徑尺寸