Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [ExecuteInEditMode]
- [RequireComponent(typeof(LineRenderer))]
- public class CircleRenderer : MonoBehaviour
- {
- /// <summary>
- /// The radius of the circle as measured from the origin of this object.
- /// </summary>
- public float radius = 1.0f;
- /// <summary>
- /// <para>
- /// The number of segments to use for representing a full 360 degree circle.
- /// </para>
- /// <para>
- /// For arcs less than 360 degrees, a smaller number of segments will be
- /// used to retain a consistent level of detail. If the number of segments
- /// used was constant, smaller arcs would have significantly greater detail
- /// compared to larger arcs.
- /// </para>
- /// </summary>
- public int segments = 100;
- /// <summary>
- /// The fraction of the arc to render, with 0 being a 0 degree arc and 1
- /// being a full 360 degree arc.
- /// </summary>
- public float arcFraction = 0.5f;
- /// <summary>
- /// <para>
- /// The origin of the arc.
- /// </para>
- /// <para>
- /// This allows you to define where on the arc that Vector2.right points.
- /// At 0.5f, Vector2.right will point directly at the center, whereas at
- /// 0 and 1 it will point at one of the two ends of the arc.
- /// </para>
- /// </summary>
- public float arcOrigin = 0.5f;
- private LineRenderer line;
- private Vector3 positionLastDraw;
- private Quaternion rotationLastDraw;
- private float radiusLastDraw;
- private float segmentsLastDraw;
- private float arcFractionLastDraw;
- private float arcOriginLastDraw;
- private void Awake()
- {
- this.line = this.GetComponent<LineRenderer>();
- }
- public void Update()
- {
- Transform transform = this.transform;
- // Check if our state has changed to try to minimize the number of
- // operations we need to do in Update.
- if (transform.position != this.positionLastDraw
- || transform.rotation != this.rotationLastDraw
- || this.radius != this.radiusLastDraw
- || this.segments != this.segmentsLastDraw
- || this.arcFraction != this.arcFractionLastDraw
- || this.arcOrigin != this.arcOriginLastDraw)
- {
- this.Draw();
- this.positionLastDraw = transform.position;
- this.rotationLastDraw = transform.rotation;
- this.radiusLastDraw = this.radius;
- this.segmentsLastDraw = this.segments;
- this.arcFractionLastDraw = this.arcFraction;
- this.arcOriginLastDraw = this.arcOrigin;
- }
- }
- public void Draw()
- {
- Transform transform = this.transform;
- // Calculate the number of points we need based off the arc fraction.
- // This means the level of detail we get throughout the arc remains
- // consistent, otherwise smaller arcs would have more detail.
- int points = (int)(this.segments * this.arcFraction) + 1;
- Vector3 center = transform.position;
- Quaternion rotation = transform.rotation;
- // We use this to adjust the origin of our arc.
- float originAngle = (Mathf.PI / 2) - (Mathf.PI * this.arcFraction * this.arcOrigin * 2);
- // Go about defining the line verts.
- this.line.positionCount = points;
- for (int i = 0; i < points; i++)
- {
- float angle = (2 * Mathf.PI * i / this.segments) + originAngle;
- Vector3 position = new Vector3(
- center.x + Mathf.Sin(angle) * this.radius,
- center.y + Mathf.Cos(angle) * this.radius
- );
- this.line.SetPosition(i, rotation * position);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement