Getting width of 2d object in unity 3d

后端 未结 2 899
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-06 09:36

I\'m using the new 2d tools in Unity3d, and I have a game object with a 2d polygonal collider attached to it. I just want to understand how to get the width of this object! Why

2条回答
  •  没有蜡笔的小新
    2021-02-06 10:01

    There are two ways(at least) to get this.

    • The 1st is to user Renderer.bounds;
    • The 2nd is to use Collider.bounds

    var renderer = gameObject.GetComponent();
    int width = renderer.bounds.size.x;
    

    or


    var collider2D= gameObject.GetComponent();
    var collider = collider2D.collider; //Edit, thanks to Arttu's comment!
    int width = collider.bounds.size.x;
    

    For your 2D case, the former might be more appropriate.

提交回复
热议问题