4
\$\begingroup\$

I've been trying to get my head around how to create a selection box for a selected unit in a 2D top-down type game. When you click on a unit, a small box would appear around that unit based on some sort of box-size.

enter image description here

I do not know/understand how to either calculate the size that box needs to be, draw the box (have an object under the game object that is sized appropriately with the selection box texture?) or how to make the selection box adjust it's size/shape like in this gif: http://i.imgur.com/xY7XW6C.gif

I don't expect anyone here to hand feed me what I need to use, but some idea of how to go about doing this in Unity (starting from the smallest, and most simple thing, like just making the box) would be very much appreciated.

Thanks!

\$\endgroup\$
2
\$\begingroup\$

I have done something like this just today. The key idea here is to use the new Unity UI and a world space canvas. Here are the general steps needed to make this work:

  1. Create a new canvas, set it to world space. (The size should not matter, but I have only started using the new UI system recently, so I am not 100% sure of that. It works with a size of 0 though.)
  2. Create an UI image as a child of the canvas. Make sure that its source image is set to 'Background' (i.e. the background image coming with Unity's new UI. If this is not listed, first create a UI panel. That should add that texture). Now set the image type to 'sliced', uncheck 'Fill Center' and set the color to whatever color you need. You should store this image as a prefab, let's call it 'Selection Box'.
  3. Whenever a unit is created, create a new selection box in the world space canvas and set it to be inactive. Store a reference to this selection box in the unit. (Okay, this is the most straightforward approach to this, but associating each unit with its own box may be wasteful. You can do this any other way, just make sure to have a Selection Box available when the unit is selected.)
  4. For as long as the unit is selected, set its selection box to be enabled. Update the size of the selection box by getting the RectTransform component and setting the 'sizeDelta' value. I myself am using the 'bounds' field of a Collider2D to get the extents of the rotated sprite, but you can also use the 'bounds' field of the SpriteRenderer.

Good Luck!

\$\endgroup\$
2
\$\begingroup\$

Make the texture that you want and attach it to the game object you want it to appear on, size it and everything so it looks how you want. Then uncheck its renderer.

Then in the code for the game object add

public GameObject selectionBox;

and then drag and drop the box onto the area in the script of the parent so you have a reference to it.

Then in your code for the parent just enable or disable it through code using the reference.

This is essentially the same as Justin Markwell's answer except his doesn't use a direct reference. That will be fine if the selection box is the only sprite renderer, but if you have multiple it might not select the correct one.

\$\endgroup\$
  • \$\begingroup\$ Makes sense, though how would I expand that to create an effect similar to the one in the .gif I linked? That's probably the hardest thing I'm running into, I can attach a texture to an object, but I really don;t know how to do it in such a way that would allow me to have it reshape/re-size itself based on how the unit is rotated. \$\endgroup\$ – Douglas Gaskell Feb 5 '15 at 20:49
  • \$\begingroup\$ Just wanted to clarify, thank you for your comment and suggestion. I appreciate it, though if you have suggestions on how to expand that I'm all ears! If not that's fine, thanks for taking the time to make your suggestion. \$\endgroup\$ – Douglas Gaskell Feb 5 '15 at 21:10
  • \$\begingroup\$ I'm not sure exactly what they're doing there, but it looks like they're just changing the X and Y scale of the texture depending on the angle of the object. This might help: docs.unity3d.com/ScriptReference/Material.SetTextureScale.html \$\endgroup\$ – Ian Winter Feb 5 '15 at 21:59
1
\$\begingroup\$

Make a texture like this in Paint or Photoshop or something. you can turn this into a sprite and add it as a child of your unit, size it and uncheck spriterenderer then prefab the unit and then add something like this to a OnSelected or OnMouseDown event.

SelectedUnit.gameObject.GetComponentInChildren<SpriteRenderer>().enabled=true;

there is a bunch of ways to accomplish but this is a very simple way hope it helps

note that the image is just a shape with a transparent center enter image description here

\$\endgroup\$
  • \$\begingroup\$ To turn the texture into a sprite just select it and in the inspector change the texturetype to Sprite(2D and UI) apply the change then you will be able to set it up or resize around the gameobject how you want \$\endgroup\$ – Justin Markwell Feb 6 '15 at 0:02

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.