Get the size of the object in Unity (size)

As follows, there is a character model next to the standard sphere. The width 0.5and height of the person are visually measured 1. Next, we will obtain the reality of the person through the program size.
Insert picture description here

方法1:Renderer.bounds.size

The result of this value truly reflects MeshRendererthe size of the model with this component. No need to multiply localScale.

// transform为人物模型的MeshRenderer的transform
var size = transform.GetComponent<Renderer>().bounds.size;
Debug.Log ("x: " + size.x + ",y: " + size.y);

Output result:

x: 0.5940213,y: 1.102448

At this point we model the character of scalethe xchanged 2
Insert picture description here
output is:

x: 1.188043,y: 1.102448

方法2 MeshFilter.mesh.bounds.size

By MeshFilterobtaining the original model mesh, the value returned is the original meshsize.

// transform为人物模型的MeshFilter的transform
var size = transform.GetComponent<MeshFilter>().mesh.bounds.size;
Debug.Log ("x: " + size.x + ",y: " + size.y);

No matter how the character is zoomed, the output result is the same

x: 0.5940213,y: 1.102448

Method 3 Collider.bounds.size

Add for objects Collider, and then use Collider.bounds.size.But this does not reflect the size of the object very well, What is boundsobtained is the outer rectangle of the object,And the outsourcing rectangle is X,Y,Zconsistent with the world coordinates. Therefore, if the object is rotated, the obtained size cannot reflect the actual size of the object, only the size of its outer rectangle. As follows, there is a capsule collider on the character
Insert picture description here

// transform为人物模型的MeshFilter的transform
var size = transform.GetComponent<Collider>().bounds.size;
Debug.Log ("x: " + size.x + ",y: " + size.y);

The output is as follows:

x: 0.2,y: 1

Now, we tilt the character and the
Insert picture description here
output is as follows:

x: 0.6194265,y: 0.8812351

Guess you like

Origin blog.csdn.net/linxinfa/article/details/107532897