Advertisement
Ryoh

CircleRenderer

Jan 13th, 2020
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [ExecuteInEditMode]
  6. [RequireComponent(typeof(LineRenderer))]
  7. public class CircleRenderer : MonoBehaviour
  8. {
  9.     /// <summary>
  10.     /// The radius of the circle as measured from the origin of this object.
  11.     /// </summary>
  12.     public float radius = 1.0f;
  13.  
  14.     /// <summary>
  15.     /// <para>
  16.     /// The number of segments to use for representing a full 360 degree circle.
  17.     /// </para>
  18.     /// <para>
  19.     /// For arcs less than 360 degrees, a smaller number of segments will be
  20.     /// used to retain a consistent level of detail. If the number of segments
  21.     /// used was constant, smaller arcs would have significantly greater detail
  22.     /// compared to larger arcs.
  23.     /// </para>
  24.     /// </summary>
  25.     public int segments = 100;
  26.  
  27.     /// <summary>
  28.     /// The fraction of the arc to render, with 0 being a 0 degree arc and 1
  29.     /// being a full 360 degree arc.
  30.     /// </summary>
  31.     public float arcFraction = 0.5f;
  32.  
  33.     /// <summary>
  34.     /// <para>
  35.     /// The origin of the arc.
  36.     /// </para>
  37.     /// <para>
  38.     /// This allows you to define where on the arc that Vector2.right points.
  39.     /// At 0.5f, Vector2.right will point directly at the center, whereas at
  40.     /// 0 and 1 it will point at one of the two ends of the arc.
  41.     /// </para>
  42.     /// </summary>
  43.     public float arcOrigin = 0.5f;
  44.  
  45.     private LineRenderer line;
  46.  
  47.     private Vector3 positionLastDraw;
  48.     private Quaternion rotationLastDraw;
  49.     private float radiusLastDraw;
  50.     private float segmentsLastDraw;
  51.     private float arcFractionLastDraw;
  52.     private float arcOriginLastDraw;
  53.  
  54.     private void Awake()
  55.     {
  56.         this.line = this.GetComponent<LineRenderer>();
  57.     }
  58.  
  59.     public void Update()
  60.     {
  61.         Transform transform = this.transform;
  62.  
  63.         // Check if our state has changed to try to minimize the number of
  64.         // operations we need to do in Update.
  65.         if (transform.position != this.positionLastDraw
  66.             || transform.rotation != this.rotationLastDraw
  67.             || this.radius != this.radiusLastDraw
  68.             || this.segments != this.segmentsLastDraw
  69.             || this.arcFraction != this.arcFractionLastDraw
  70.             || this.arcOrigin != this.arcOriginLastDraw)
  71.         {
  72.             this.Draw();
  73.  
  74.             this.positionLastDraw = transform.position;
  75.             this.rotationLastDraw = transform.rotation;
  76.  
  77.             this.radiusLastDraw = this.radius;
  78.             this.segmentsLastDraw = this.segments;
  79.             this.arcFractionLastDraw = this.arcFraction;
  80.             this.arcOriginLastDraw = this.arcOrigin;
  81.         }
  82.     }
  83.  
  84.     public void Draw()
  85.     {
  86.         Transform transform = this.transform;
  87.  
  88.         // Calculate the number of points we need based off the arc fraction.
  89.         // This means the level of detail we get throughout the arc remains
  90.         // consistent, otherwise smaller arcs would have more detail.
  91.         int points = (int)(this.segments * this.arcFraction) + 1;
  92.  
  93.         Vector3 center = transform.position;
  94.         Quaternion rotation = transform.rotation;
  95.  
  96.         // We use this to adjust the origin of our arc.
  97.         float originAngle = (Mathf.PI / 2) - (Mathf.PI * this.arcFraction * this.arcOrigin * 2);
  98.  
  99.         // Go about defining the line verts.
  100.         this.line.positionCount = points;
  101.         for (int i = 0; i < points; i++)
  102.         {
  103.             float angle = (2 * Mathf.PI * i / this.segments) + originAngle;
  104.             Vector3 position = new Vector3(
  105.                 center.x + Mathf.Sin(angle) * this.radius,
  106.                 center.y + Mathf.Cos(angle) * this.radius
  107.             );
  108.             this.line.SetPosition(i, rotation * position);
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement