Skip to content

Instantly share code, notes, and snippets.

@garzaa
Last active June 5, 2019 01:12
Show Gist options
  • Save garzaa/30f0507978db3fd7c78879c9a394298f to your computer and use it in GitHub Desktop.
Save garzaa/30f0507978db3fd7c78879c9a394298f to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
[RequireComponent(typeof(LineRenderer))]
public class CircleRenderer : Monobehaviour {
public int segments;
[Range(0, 1)]
public float arcFraction = 1f;
public float radius = .10f;
int segmentsLastFrame;
float radiusLastFrame;
float arcFractionLastFrame;
LineRenderer line;
override protected void Start() {
line = GetComponent<LineRenderer>();
DrawCircle(segments);
}
void LateUpdate () {
if (Changed() && segments > 0) {
DrawCircle(segments);
}
segmentsLastFrame = segments;
radiusLastFrame = radius;
arcFractionLastFrame = arcFraction;
}
bool Changed() {
return segments != segmentsLastFrame || radius != radiusLastFrame || arcFraction != arcFractionLastFrame;
}
void DrawCircle(int segments) {
Vector3[] points = new Vector3[segments];
int actualSegments = (int) ((float) segments * arcFraction);
line.positionCount = actualSegments;
for (int i=0; i<actualSegments; i++) {
float angle = ((float) i/segments) * Mathf.PI*2.0f;
points[i] = new Vector3(
Mathf.Sin(angle)*radius,
Mathf.Cos(angle)*radius,
0
);
}
line.SetPositions(points);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment