unity3d中獲得物體的size

2021-09-06 16:15:40 字數 933 閱讀 1882

以size的x方向為例

1:gameobject.renderer.bounds.size.x;

//這個值的結果真實反應出有meshrenderer這個元件的模型的尺寸。不須要再乘以localscale.x。

2:gameobject.getcomponent().mesh.bounds.size.x;//通過meshfilter獲得原始模型的mesh,該值返回的結果是原始mesh的尺寸。

若要獲得模型的尺寸大小還須要乘以模型的localscale.x。

即:gameobject.getcomponent().mesh.bounds.size.x*gameobject.transform.localscale.x;

3:為物體加入�collider,然後使用***.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屬性為正交模式下的口徑尺寸

unity3d中獲得物體的size

以size的x方向為例 1 gameobject.renderer.bounds.size.x 這個值的結果真實反應出有meshrenderer這個元件的模型的尺寸。不需要再乘以localscale.x。2 gameobject.getcomponent mesh.bounds.size.x 通過m...

unity3d中獲得物體的size

以size的x方向為例 1 gameobject.renderer.bounds.size.x 這個值的結果真實反應出有meshrenderer這個元件的模型的尺寸。不需要再乘以localscale.x。2 gameobject.getcomponent mesh.bounds.size.x 通過m...

Unity3D中物體的移動方式

在unity3d中,有多種方式可以改變物體的座標,實現移動的目的.總結來說,分為以下幾種,這裡對於這幾種方式分別做闡述.通過transform元件移動物體 transform 元件用於描述物體在空間中的狀態,它包括 位置 position 旋轉 rotation 縮放 scale 其實所有的移動都會...