如何在Unity3D中绘制圆圈?

Oks*_*ana 14 unity-game-engine

如何在Unity 3d中绘制圆圈?我想在不同的物体周围画圆圈.圆的半径是不同的,圆是纹理 - 正方形.

小智 13

我发现这个代码有一个很大的错误.点数(大小)不应为"(2*pi/theta_scale)+ 1",因为这会使圆圈绘制6.28倍.大小应为"1/theta_scale + 1".所以对于theta_scale为0.01,它需要绘制100个点,而对于theta_scale为0.1,它需要绘制10个点.否则它将分别绘制62次和628次.这是我使用的代码.

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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果修改"大小"中除以ThetaScale的数字,则可以制作扫描规格/饼图类型图形.

  • 这应该是正确的答案,因为@ Jerdak的代码绘制了许多不必要的行,但是这段代码只传递一次并在一次通过中绘制每个角.但请注意,对于无孔线,您应该这样做:`Size =(int)((1f/ThetaScale)+ 2f)` (2认同)

Jer*_*dak 9

有关类似问题,请参阅Unity Answers.

另外:

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;
}
Run Code Online (Sandbox Code Playgroud)

LineRenderer需要连续点.您可以稍微修改此代码以使用圆柱游戏对象而不是线渲染器.我发现LineRenderer有点可怕.

最后,类似于第一个链接,您可以将圆形纹理附加到单位平面.使不属于圆的纹理的任何部分透明.然后缩放并对齐平面以适合您的对象.不幸的是,如果有人看起来几乎与飞机平行,这种方法并不好.


Jon*_*kin 5

Jerdak的解决方案很好,但是代码很混乱,所以我不得不稍微调整一下。这是一个类的代码,我在循环中使用 i 以避免错误。

它还用其游戏对象位置更新圆圈的位置。

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);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)