
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 the PolygonCollider2D
.
4.Loop over the points and convert each one from local to world space with the TransformPoint
function..
5.Set the SetVertexCount
of the LineRenderer
to be the Length
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.
LineRenderer.SetPosition(currentLoop, pointsArray[currentLoop]);
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.
LineRenderer.SetPosition(pointsArray.Length, pointsArray[0]);
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 the size
and color
of it.
using UnityEngine;
using System.Collections;
public class LineDrawerTest : MonoBehaviour
{
public GameObject myGameObject;
private PolygonCollider2D pColider;
protected void Start()
{
pColider = myGameObject.GetComponent<PolygonCollider2D>();
highlightAroundCollider(pColider, Color.yellow, Color.red, 0.1f);
}
void highlightAroundCollider(Component cpType, Color beginColor, Color endColor, float hightlightSize = 0.3f)
{
//1. Create new Line Renderer
LineRenderer lineRenderer = gameObject.GetComponent<LineRenderer>();
if (lineRenderer == null)
{
lineRenderer = cpType.gameObject.AddComponent<LineRenderer>();
}
//2. Assign Material to the new Line Renderer
lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
float zPos = 10f;//Since this is 2D. Make sure it is in the front
if (cpType is PolygonCollider2D)
{
//3. Get the points from the PolygonCollider2D
Vector2[] pColiderPos = (cpType as PolygonCollider2D).points;
//Set color and width
lineRenderer.SetColors(beginColor, endColor);
lineRenderer.SetWidth(hightlightSize, hightlightSize);
//4. Convert local to world points
for (int i = 0; i < pColiderPos.Length; i++)
{
pColiderPos[i] = cpType.transform.TransformPoint(pColiderPos[i]);
}
//5. Set the SetVertexCount of the LineRenderer to the Length of the points
lineRenderer.SetVertexCount(pColiderPos.Length + 1);
for (int i = 0; i < pColiderPos.Length; i++)
{
//6. Draw the line
Vector3 finalLine = pColiderPos[i];
finalLine.z = zPos;
lineRenderer.SetPosition(i, finalLine);
//7. Check if this is the last loop. Now Close the Line drawn
if (i == (pColiderPos.Length - 1))
{
finalLine = pColiderPos[0];
finalLine.z = zPos;
lineRenderer.SetPosition(pColiderPos.Length, finalLine);
}
}
}
//Not Implemented. You can do this yourself
else if (cpType is BoxCollider2D)
{
}
}
void Update()
{
}
}