How can I draw a circle in Unity3D?

How to draw circle in Unity 3d? I want to draw a circle around different objects. The radiuses of the circles are different and the circles have textures - squares.


Solution 1:

I found a big error with this code. The number of points (Size) shouldn't be "(2 * pi / theta_scale) + 1" because this causes the circle to draw 6.28 times. The size should be "1 / theta_scale + 1". So for a theta_scale of 0.01 it needs to draw 100 points, and for a theta_scale of 0.1 it needs to draw 10 points. Otherwise it would draw 62 times and 628 times respectively. Here is the code I used.

using UnityEngine;
using System.Collections;

public class DrawRadar: MonoBehaviour {
    public float ThetaScale = 0.01 f;
    public float radius = 3 f;
    private int Size;
    private LineRenderer LineDrawer;
    private float Theta = 0 f;

    void Start() {
        LineDrawer = GetComponent < LineRenderer > ();
    }

    void Update() {
        Theta = 0 f;
        Size = (int)((1 f / ThetaScale) + 1 f);
        LineDrawer.SetVertexCount(Size);
        for (int i = 0; i < Size; i++) {
            Theta += (2.0 f * Mathf.PI * ThetaScale);
            float x = radius * Mathf.Cos(Theta);
            float y = radius * Mathf.Sin(Theta);
            LineDrawer.SetPosition(i, new Vector3(x, y, 0));
        }
    }
}

If you modify the number in "Size" that is divided by ThetaScale, you can make a sweeping gauge/pie chart type graphic.

Solution 2:

See Unity Answers for a similar question.

Alternatively:

float theta_scale = 0.1;  // Circle resolution

LineRenderer lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
lineRenderer.SetColors(c1, c2);
lineRenderer.SetWidth(0.2F, 0.2F);
lineRenderer.SetVertexCount(size);

int i = 0;
for(float theta = 0; theta < 2 * PI; theta += theta_scale) {
    x = r*cos(theta);
    y = r*sin(theta);

    Vector3 pos = new Vector3(x, y, 0);
    lineRenderer.SetPosition(i, pos);
    i+=1;
}

The LineRenderer requires continuous points. You can modify this code slightly to use cylinder game objects instead of a line renderer. I find the LineRenderer to be a bit hideous.

Lastly, similar to the first link, you could attach a circle texture to a unit plane. Make any part of the texture that isn't part of the circle transparent. Then just scale and align the plane to fit your object. Unfortunately this method isn't great if someone is looking almost parallel to the plane.

Solution 3:

Jerdak's solution is good, but the code is messy so I had to tweak a little. Here's the code for a class, where I use i in the loop to avoid a bug.

It also updates the circle's position with its gameObject position.

using UnityEngine;
using System.Collections;

public class CircleDraw : MonoBehaviour {   
  float theta_scale = 0.01f;        //Set lower to add more points
  int size; //Total number of points in circle
  float radius = 3f;
  LineRenderer lineRenderer;

  void Awake () {       
    float sizeValue = (2.0f * Mathf.PI) / theta_scale; 
    size = (int)sizeValue;
    size++;
    lineRenderer = gameObject.AddComponent<LineRenderer>();
    lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
    lineRenderer.SetWidth(0.02f, 0.02f); //thickness of line
    lineRenderer.SetVertexCount(size);      
  }

  void Update () {      
    Vector3 pos;
    float theta = 0f;
    for(int i = 0; i < size; i++){          
      theta += (2.0f * Mathf.PI * theta_scale);         
      float x = radius * Mathf.Cos(theta);
      float y = radius * Mathf.Sin(theta);          
      x += gameObject.transform.position.x;
      y += gameObject.transform.position.y;
      pos = new Vector3(x, y, 0);
      lineRenderer.SetPosition(i, pos);
    }
  }
}

Solution 4:

Using Shader Graph we can now draw pixel perfect circle.

Unlit Shader Graph

Once you created this graph, create a new material based on this shader.

result

Then create a new gameobject with a sprite renderer and set the material you just created.

You can scale the circle using the "scale" parameter of the material.