0
\$\begingroup\$

I currently use Unity gizmos for many debugging purposes, but I'd like almost all of them to be visible to the player in-game (when the game is built).

For example, when a player is dragging to place a wall along a grid, I use Gizmos.DrawCube.

I know of a couple of options:

  1. ALINE - an asset on Unity asset store, but currently doesn't work with 2D Experimental URP
  2. Line renderer - seems very manual and tedious to do, and I'm unsure how I could modify it using code
  3. Placing pictures as UI elements over selected tiles (seems pretty expensive to do?)

Any suggestions are appreciated!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ I don't think you'll find an objective "best" here. All the ways you can describe and more can work and perform adequately. The real question is which one you prefer working with. \$\endgroup\$
    – DMGregory
    Commented May 21, 2021 at 12:53

1 Answer 1

1
\$\begingroup\$

Gizmos are for development, not for playing. The usual approach to implement a gizmo would be to create a gameObject representing said gizmo which you activate and deactivate as needed.

So instead of:

if (isDragging) {
     Gizmos.DrawCube(dragPosition, dragSize);
}

You write:

if (isDragging) {
     myGizmo.SetActive(true);
     myGizmo.transform.position = dragPosition;
     myGizmo.transform.scale = dragSize;
} else {
     myGizmo.SetActive(false);
}

While this seems a bit more convoluted, think of all the possibilities you have when your gizmos are full-fledged gameObjects which can have any kind of visual and logical components you want.

\$\endgroup\$
4
  • \$\begingroup\$ Would the gizmo GameObject represent an image? How would I go about achieving something similar to this (recorded in Rimworld): gifyu.com/image/DuJ5 Would a selection box for each square be a separate GO, or is there a way to repeat the texture only using one GO? \$\endgroup\$
    – caleidon
    Commented May 21, 2021 at 13:07
  • \$\begingroup\$ @franticabyss You could probably do that with one sprite renderer which uses Draw Mode: Tiled. In that case you would not change transform.scale to control the size but instead change the width and height of the SpriteRenderer component. \$\endgroup\$
    – Philipp
    Commented May 21, 2021 at 13:47
  • \$\begingroup\$ Thanks! I've got a good general idea of how to do this now - just one more question. How would I make it so that if I'm dragging a line of where a wall is supposed to be placed, and some part of that wall is over an obstacle, I either don't draw the squares or draw them with red color? Using a single sprite renderer wouldn't work for that. Have you maybe got any ideas? \$\endgroup\$
    – caleidon
    Commented May 21, 2021 at 19:53
  • 1
    \$\begingroup\$ @franticabyss In that case you would have to use multiple game objects. Or perhaps you could pull it off with some shader magic. But when you never wrote a shader before, better stick to game objects, \$\endgroup\$
    – Philipp
    Commented May 21, 2021 at 22:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .