Skip to content

Instantly share code, notes, and snippets.

@romainPechot
Created August 19, 2015 14:37
Show Gist options
  • Save romainPechot/772f286f0092cebeb402 to your computer and use it in GitHub Desktop.
Save romainPechot/772f286f0092cebeb402 to your computer and use it in GitHub Desktop.
Circle Line Renderer
[System.Serializable]
public class CirclelLineRenderer
{
[SerializeField]
private LineRenderer lineRenderer;
public LineRenderer LINE_RENDERER
{
get { return lineRenderer; }
set
{
lineRenderer = value;
Refresh();
}
}
public bool HAS_RENDERER { get { return lineRenderer != null; } }
[SerializeField]
private float radius = 1f;
public float RADIUS
{
get { return radius; }
set
{
radius = value;
Refresh();
}
}
[SerializeField]
private bool overStep = false;
[SerializeField]
[Range(0f, 360f)]
private float startPosition = 0f;
public float START_POSITION
{
get { return startPosition; }
set
{
startPosition = Mathf.Clamp(value, 0f, 360f);
}
}
[SerializeField]
private float lineWidthStart = 0.25f;
public float LINE_WIDTH_START
{
get { return lineWidthStart; }
set
{
lineWidthStart = value;
if(overStep) lineWidthEnd = value;
}
}
[SerializeField]
private float lineWidthEnd = 0.25f;
public float LINE_WIDTH_END
{
get { return lineWidthEnd; }
set
{
lineWidthEnd = value;
if(overStep) lineWidthStart = value;
}
}
public float GLOBAL_WIDTH
{
set
{
LINE_WIDTH_END = LINE_WIDTH_START = value;
}
}
private const int stepMin = 1;
private const int stepMax = 32;
[Range(stepMin, stepMax)]
[SerializeField]
private int step = stepMin;
public int STEP
{
get { return step; }
set
{
step = Mathf.Clamp(value, stepMin, stepMax);
}
}
private int stepTot = 10;
private Vector3 vertPos = Vector3.zero;
public void Refresh()
{
if(lineRenderer == null) return;
// compute total step
stepTot = (step * 4) + 1;
// inverse step 1/totstep
float iStep = 1f;
iStep /= (float)(stepTot - 1);
if(overStep)
{
// line width
lineWidthEnd = lineWidthStart;
// tot step
stepTot++;
}
lineRenderer.SetVertexCount(stepTot);
lineRenderer.SetWidth(lineWidthStart, lineWidthEnd);
for(int i = 0; i < stepTot; i++)
{
vertPos.x = Mathf.Cos((startPosition + 360f * i * iStep) * Mathf.Deg2Rad) * radius;
vertPos.y = Mathf.Sin((startPosition + 360f * i * iStep) * Mathf.Deg2Rad) * radius;
lineRenderer.SetPosition(i, vertPos);
}// for()
}// Refresh()
}// CirclelLineRenderer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment