How can I draw a circle in Unity3D?

2019-03-10 02:26发布

How to draw circle in Unity 3d? I want draw circle around different object. Radiuses of circles are different and circle has texture - squares.

标签: unity3d
7条回答
The star\"
2楼-- · 2019-03-10 02:38

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);
    }
  }
}
查看更多
啃猪蹄的小仙女
3楼-- · 2019-03-10 02:46

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.

查看更多
迷人小祖宗
4楼-- · 2019-03-10 02:46

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.

查看更多
不美不萌又怎样
5楼-- · 2019-03-10 02:48

Circle can draw using shader - draw pixel if it on radius from center.

查看更多
Juvenile、少年°
6楼-- · 2019-03-10 02:49

Did the following with a Sprite. Chan is flying in the scene, so she's slightly above the plane. I had her flying so I could get a good screenshot, not because it wouldn't play well with the plane.

I used a low-resolution circle sprite. X rotation 90 Scale X 15, Y 15, Z 1

Then I set the Sorting Layer, so it will render above the Default Layer. I was testing this out when I came across this post. It doesn't handle shadows well. I'd have to figure out what layer shadows are drawn on to make sure they get rendered onto the sprite.

enter image description here

查看更多
姐就是有狂的资本
7楼-- · 2019-03-10 02:50

I have a shader from which I usually start making effects like lens flares, and it makes a circle. Using shader is the best choice because you will get perfectly smooth and round circle.

Also it's easy to experiment with and tune the shader since shader changes don't require recompile and re-entering of play mode.

查看更多
登录 后发表回答