Unity gets Object Size

Source: Internet
Author: User

?? Get the size of an object in a Unity3d

Take the x direction of size as an example

1:gameobject.renderer.bounds.size.x;//The result of this value really reflects the size of the model that has meshrenderer this component. There is no need to multiply localscale.x.


2:gameobject.getcomponent<meshfilter> (). mesh.bounds.size.x;//obtains the mesh of the original model through Meshfilter , The result returned by this value is the size of the original mesh.

To get the size of the model you also need to multiply the model's localscale.x.

namely:gameobject.getcomponent<meshfilter> (). mesh.bounds.size.x*gameobject.transform.localscale.x;


3: Add collider to the object, then use XXX.collider.bounds.size;

This is not necessarily a very good response to the size of the object, bounds obtained is the object of the outsourcing rectangle.

And this outsourced rectangle has the same x, Y, z and world coordinates. Therefore, if the object is rotated, the resulting size cannot reflect the true size of the object, just the size of its outsourced rectangle ...

such as: Get the size of the terrain

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

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

Terrainheight = Terrain.collider.bounds.size.y;

4: Code to achieve the size of complex objects (such as the root node does not have a Meshfilter,meshrenderer component, the object is composed of very many complex irregular small mesh sub-objects)
Such as:





Aperture size of camera

When the projection type is perspective, the Fieldofview property represents the degree of the aperture. Range is [1,179]

When the projection type is orthgraphic, the Orthographicsize property is the aperture size in orthogonal mode

Unity gets Object Size

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.