Version: 2022.2
public Bounds bounds ;

描述

The bounding box of the renderer in world space.

这是完全包裹了世界空间中的对象的轴对齐包围盒。

使用 bounds 可以方便地描述对象位置及其范围的 近似形状。例如,在描述对象中心时, center 属性通常比 Transform.position 更精确, 特别是当对象不对称时。

Mesh.bounds and localBounds are similar but they return the bounds in local space.

You can override the default bounding box by setting your own world space bounding box. This is mostly useful when the renderer uses a shader that does custom vertex deformations, and the default bounding box is not accurate.

When you set custom world bounds, the renderer bounding volume no longer automatically tracks Transform component changes. If there is a local space bounding volume override (localBounds) active at the same time, it is ignored and the custom world space bounds are used. Use ResetBounds to remove the custom bounds override. Note that the custom world bounds value is not saved into scenes or prefabs and has to be set from a script at runtime.

using UnityEngine;

public class DrawRendererBounds : MonoBehaviour { // Draws a wireframe box around the selected object, // indicating world space bounding volume. public void OnDrawGizmosSelected() { var r = GetComponent<Renderer>(); if (r == null) return; var bounds = r.bounds; Gizmos.matrix = Matrix4x4.identity; Gizmos.color = Color.blue; Gizmos.DrawWireCube(bounds.center, bounds.extents * 2); } }