版本: Unity 6 (6000.0)
语言英语
  • C#

BoxCollider.size

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交内容,但我们会阅读用户提出的每项更改建议,并在适当情况下进行更新。

关闭

提交失败

由于某种原因,您的更改建议无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

切换到手册
public Vector3 size;

描述

盒子的尺寸,以物体局部空间为单位测量。

使用此方法返回或设置游戏对象的 BoxCollider 组件的大小。Unity 在游戏对象的局部空间中测量大小。更改 BoxCollider 大小将根据 Transform 的缩放比例进行缩放。

//Attach this script to a GameObject. Make sure the GameObject has a BoxCollider component (Unity Cubes have these automatically as long as they weren’t removed).
//Create three Sliders ( Create>UI>Slider). These are for manipulating the x, y, and z values of the size.
//Click on the GameObject and attach each of the Sliders to the fields in the Inspector.
//In Play Mode, click the GameObject and enable Gizmos to visualize the BoxCollider.

using UnityEngine; using UnityEngine.UI;

public class Example : MonoBehaviour { //Make sure there is a BoxCollider component attached to your GameObject BoxCollider m_Collider; float m_ScaleX, m_ScaleY, m_ScaleZ; public Slider m_SliderX, m_SliderY, m_SliderZ;

void Start() { //Fetch the Collider from the GameObject m_Collider = GetComponent<BoxCollider>(); //These are the starting sizes for the Collider component m_ScaleX = 1.0f; m_ScaleY = 1.0f; m_ScaleZ = 1.0f;

//Set all the sliders max values to 20 so the size values don't go above 20 m_SliderX.maxValue = 20; m_SliderY.maxValue = 20; m_SliderZ.maxValue = 20;

//Set all the sliders min values to 1 so the size don't go below 1 m_SliderX.minValue = 1; m_SliderY.minValue = 1; m_SliderZ.minValue = 1; }

void Update() { m_ScaleX = m_SliderX.value; m_ScaleY = m_SliderY.value; m_ScaleZ = m_SliderZ.value;

m_Collider.size = new Vector3(m_ScaleX, m_ScaleY, m_ScaleZ); Debug.Log("Current BoxCollider Size : " + m_Collider.size); } }