My question is as titled. I’m playing around with unity to get back into it to return to school, and I’ve decided to make a little risk type game.
Basically I have my map, and when I click one of my territories (which I have a polygon collider around, with an on click method attached that currently just toggles a small box sprite to test) I’d like to highlight the edge around the territory I’ve selected (the edge of witch the polygon collider covers). So I’m wondering if its possible to make a highlight effect based around the collider, or something like that.
My current plan is to just Photoshop the edges of all the territories into sprites and toggle them like the test sprite in order to create the effect, but if I can do it in a much simpler time effective way that would be great!
Thanks for the help if you can, and ask if you need more info!
4
Answers
While this wont outline the collider (unless it’s a mesh collider), this shader will outline the mesh with a chosen color:
I did not write this shader; it’s one of many variations that you can find floating around.
You can apply it to your gameobject by setting the shader for the material, so something like this:
gameObject.renderer.material.shader = SelectedShader;
Where SelectedShader is a public field (not property), such that you can bind the outline shader to that variable through the inspector. Be sure to keep a reference to the previous shader so you can go back when unselected.
You can choose the color of the outline by setting it on the material:
gameObject.renderer.material.SetColor("_OutlineColor", outlineColor);
Hopefully this is close enough!
I did very similar thing months ago. It can be done with LineRenderer and PolygonCollider2D. You have to know how do read the documentation as a new user because it has a-lot of information you don’t know about.
Steps on how to do this:
1.Create new
LineRenderer
from code.2.Assign Material to the new Line Renderer, set the width and color.
3.Get the points from the
PolygonCollider2D
which returns arrays of the points in thePolygonCollider2D
.4.Loop over the points and convert each one from local to world space with the
TransformPoint
function..5.Set the
SetVertexCount
of theLineRenderer
to be theLength
of the points plus 1. We need that extra 1 in order to close what we are drawing.DRAW LINES:
6.Finally, Loop over the points and draw the Line by changing the position of the LineRenderer with the
SetPosition
function.Since this is 2D, you may want to modify the Z axis of the pointsArray to make sure that the Object is displayed in front of every GameObject.
7.Close the line by drawing a new Line from the last(
pointsArray.Length
) point position to the first( pointsArray[0]) points.Below is a function that can do this completely. You can extend it to support other 2D Colliders as-well. Just create a script and cop the code inside it. Drag the Sprite that has PolygonCollider2D attached to it to the myGameObject slot then click Play. It will draw line on that sprite.
The
highlightAroundCollider(pColider, Color.yellow, Color.red, 0.1f);
function gives you option when calling it such as setting thesize
andcolor
of it.Here is a solution I use. It uses Line Renderer, witch will finally be beatifull in Unity 5.5.
I got object that i use as button. It have PoligonCollider2D, a LineRenderer and this sript. Also line renderer has to redraw itself after resolution changes. So I configure my poligon colliders at the specific resolution, that you can config in Game window. In my script I use 689*500, that is 16/9 resolution. Canvas is Screen Space – Camera and have Aspect Ratio Fitter with 1.7777778 value.
Thats all looks like this:
Example
I think, this way is mush more simple