How can I draw a circle in Unity3D?

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

+13
unity3d
source share
8 answers

I found a big mistake with this code. The number of dots (size) should not be "(2 * pi / theta_scale) + 1" because it makes the circle draw 6.28 times. The size should be "1 / theta_scale + 1". So for theta_scale 0.01 you need to draw 100 points, and for theta_scale 0.1 you need to draw 10 points. Otherwise, he would have scored 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.01f; public float radius = 3f; private int Size; private LineRenderer LineDrawer; private float Theta = 0f; void Start () { LineDrawer = GetComponent<LineRenderer>(); } void Update () { Theta = 0f; Size = (int)((1f / ThetaScale) + 1f); LineDrawer.SetVertexCount(Size); for(int i = 0; i < Size; i++){ Theta += (2.0f * 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 change the number to "Size", which is divided into ThetaScale, you can make a graphic type with a wide metric / pie chart.

+13
source share

See Unity's answers to 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; } 

LineRenderer requires continuous points. You can modify this code a bit to use cylindrical game objects instead of a line renderer. I find LineRenderer a little disgusting.

Finally, like the first link, you can attach a circle texture to a unit plane. Make any part of the texture that is not part of the circle transparent. Then simply scale and align the plane to suit your subject. Unfortunately, this method is not very good if someone is looking almost parallel to the plane.

+9
source share

Jerdak's solution is good, but the code is dirty, so I had to fix it a bit. Here is the code for the class where I use I in a loop to avoid an error.

It also updates the position of the circle using the 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); } } } 
+3
source share

Using the Shader Graph, we can draw a perfect circle of pixels.

Unlit Shader Graph

After creating this graph, create new material based on this shader.

result

Then create a new game object with the sprite visualization tool and set the material you just created.

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

+1
source share

Using the sprite, the following was done. Chan is flying on stage, so she’s just above the plane. I had her flight, so I could get a good screenshot, and not because he would play poorly with the plane.

I used a low resolution circular sprite. X turn 90 Zoom X 15, Y 15, Z 1

Then I set the Sort layer, so it will appear above the default level. I tested this when I came across this post. It does a great job with shadows. I would have to figure out what layer shadows are drawn to make sure they appear on the sprite.

enter image description here

0
source share

I have a shader from which I usually start to create effects such as lens flare, and it forms a circle. Using shaders is the best choice because you get a perfectly smooth and round circle.

It is also easy to experiment and customize the shader, since shader changes do not require recompilation and re-entry into playback mode.

0
source share

I recommend creating an extension method for GameObject. Worked well for me.

 public static class GameObjectExtension { const int numberOfSegments = 360; public static void DrawCircle(this GameObject go, float radius, float lineWidth, Color startColor, Color endColor, bool lineRendererExists=true) { LineRenderer circle = lineRendererExists ? go.GetComponent<LineRenderer>() : go.AddComponent<LineRenderer>(); circle.useWorldSpace = false; circle.startWidth = lineWidth; circle.endWidth = lineWidth; circle.endColor = endColor; circle.startColor = startColor; circle.positionCount = numberOfSegments + 1; Vector3 [] points = new Vector3[numberOfSegments + 1]; for (int i = 0; i < numberOfSegments + 1; i++) { float rad = Mathf.Deg2Rad * i; points[i] = new Vector3(Mathf.Sin(rad) * radius, 0, Mathf.Cos(rad) * radius); } circle.SetPositions(points); } } 

1 Another point to pay attention to: if the LineRenderer component is not used, the last parameter must be false

0
source share

A circle can draw using a shader drawing pixel if it is located in a radius from the center.

-one
source share

All Articles