Article Directory
As follows, there is a character model next to the standard sphere. The width 0.5
and height of the person are visually measured 1
. Next, we will obtain the reality of the person through the program size
.
方法1:Renderer.bounds.size
The result of this value truly reflects MeshRenderer
the 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 scale
the x
changed 2
output is:
x: 1.188043,y: 1.102448
方法2 MeshFilter.mesh.bounds.size
By MeshFilter
obtaining the original model mesh
, the value returned is the original mesh
size.
// 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 bounds
obtained is the outer rectangle of the object,And the outsourcing rectangle is X,Y,Z
consistent 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
// 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
output is as follows:
x: 0.6194265,y: 0.8812351