Unity Bound

Bound:在SpriteRenderer,BoxCollider ,BoxCollider2D,以及mesh中都存在。

其中在SpriteRenderer,BoxCollider ,BoxCollider2D中Bound的Center返回的是Bound的世界坐标。

extents:

1.在BoxCollider和BoxCollider2D返回的是scale值

public class Example : MonoBehaviour
{
    Collider m_ObjectCollider;
    public Vector3 m_MyScale;

    void Start()
    {
        //Fetch the GameObject's collider (make sure they have a Collider component)
        m_ObjectCollider = gameObject.GetComponent<Collider>();
        //Output the GameObject's Collider Bound extents
        Debug.Log("extents : " + m_ObjectCollider.bounds.extents);
    }

    //Detect when the user clicks the GameObject
    void OnMouseDown()
    {
        //Change the scale of the GameObject to the size you define in the Inspector
        transform.localScale = m_MyScale;
        //Output the extents of the Bounds after clicking the GameObject. Extents change to half of the scale.
        Debug.Log("extents : " + m_ObjectCollider.bounds.extents);
    }
}

下面代码可以测试出来。

猜你喜欢

转载自www.cnblogs.com/huangzhenxiong/p/9941942.html