0
\$\begingroup\$

Simple question. When I prototype game ideas I like to create objects without having to attach sprites. Is it possible to create a Rigidbody 2D in unity, set its size and render its wire frame? I am using a unity in 2D mode.

Thanks!

\$\endgroup\$
2
  • \$\begingroup\$ The wire frame of what? Do you want to see the collider? \$\endgroup\$ Sep 29 '14 at 2:50
  • \$\begingroup\$ The Rigidbody isn't viewable… It's the collider attached. \$\endgroup\$
    – Savlon
    Sep 29 '14 at 4:04
1
\$\begingroup\$

To answer your question directly, it is possible to set the scene/game windows to enable gismos for your colliders but they will only be displayed when that gameobject (or a parent) is selected in the hierarchy.

This can be improved by attaching a script that implements OnDrawGizmos(), as an example here is an implementation that would visualize a BoxCollider2D:

using UnityEngine;

public class DisplayBoxCollider : MonoBehaviour {
    public Color LineColor = Color.green;
    private BoxCollider2D _boxCollider;
    private Transform _transform;

    public void Start () {
        _transform = GetComponent<Transform>();
        _boxCollider = GetComponent<BoxCollider2D>();
    }

    public void OnDrawGizmos() {
        if (_boxCollider) {
            DrawBox(_transform.position, _boxCollider.size, LineColor);    
        }
    }

    private void DrawBox(Vector3 origin, Vector3 size, Color color) {
        Vector2 halfSize = size/2;

        float top = origin.y + halfSize.y;
        float bottom = origin.y - halfSize.y;
        float left = origin.x - halfSize.x;
        float right = origin.x + halfSize.x;

        Gizmos.color = color;
        Gizmos.DrawLine(new Vector3(left, top), new Vector3(right, top));
        Gizmos.DrawLine(new Vector3(right, top), new Vector3(right, bottom));
        Gizmos.DrawLine(new Vector3(right, bottom), new Vector3(left, bottom));
        Gizmos.DrawLine(new Vector3(left, bottom), new Vector3(left, top));
    }
}

However this approach leads to a fair bit of work if you require support for all of the collider types.

Instead I take the following approach:

  • Use an image editor to create a set of small white png files. (e.g. 8x8, 8x16, 16x8 to cover a few basic senarios)
  • Import these images into the unity project (Texture Type: Sprite, Sprite Mode: Single, Pixels to Units: 8)
  • Attach a sprite to a GameObject, then customize the SpriteRenderer's Color to disambiguate between different types.

Using these placeholder sprites results in a minimalist representation of each object and the space it occupies, suitable for basic prototyping. By having a couple of sizes I can have sprites 1x1, 1x2, 2x1 units in size without having modify to the scale.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.