Unity3d 使用Gizmos畫一個圓圈

Gizmos是場景視圖裡的一個可視化調試工具。

在做項目過程中。我們常常會用到它,比如:繪制一條射線等。

Unity3D 4.2版本號截至。眼下僅僅提供瞭繪制射線,線段,網格球體,實體球體,網格立方體,實體立方體,圖標。GUI紋理,以及攝像機線框。

假設須要繪制一個圓環還須要自己寫代碼

using UnityEngine;
using System;
public class HeGizmosCircle : MonoBehaviour
{
       public Transform m_Transform;
       public float m_Radius = 1; // 圓環的半徑
       public float m_Theta = 0.1f; // 值越低圓環越平滑
       public Color m_Color = Color.green; // 線框顏色
       
       void Start()
       {
              if (m_Transform == null)
              {
                     throw new Exception("Transform is NULL.");
              }
       }
       void OnDrawGizmos()
       {
              if (m_Transform == null) return;
              if (m_Theta < 0.0001f) m_Theta = 0.0001f;
              // 設置矩陣
              Matrix4x4 defaultMatrix = Gizmos.matrix;
              Gizmos.matrix = m_Transform.localToWorldMatrix;
              // 設置顏色
              Color defaultColor = Gizmos.color;
              Gizmos.color = m_Color;
              // 繪制圓環
              Vector3 beginPoint = Vector3.zero;
              Vector3 firstPoint = Vector3.zero;
              for (float theta = 0; theta < 2 * Mathf.PI; theta += m_Theta)
              {
                     float x = m_Radius * Mathf.Cos(theta);
                     float z = m_Radius * Mathf.Sin(theta);
                     Vector3 endPoint = new Vector3(x, 0, z);
                     if (theta == 0)
                     {
                            firstPoint = endPoint;
                     }
                     else
                     {
                            Gizmos.DrawLine(beginPoint, endPoint);
                     }
                     beginPoint = endPoint;
              }
              // 繪制最後一條線段
              Gizmos.DrawLine(firstPoint, beginPoint);
              // 恢復默認顏色
              Gizmos.color = defaultColor;
              // 恢復默認矩陣
              Gizmos.matrix = defaultMatrix;
       }
}

把代碼拖到一個GameObject上,關聯該GameObject的Transform,然後就能夠在Scene視圖窗體裡顯示一個圓瞭。

通過調整Transform的Position。Rotation。Scale,來調整圓的位置,旋轉,縮放。

補充:基於Unity3D使用LineRender組件繪制圓線

在此記錄一下使用Unity3D 的LineRender繪制線的過程,經過測試LineRender與OpenGL的GL_LINE_STRIP繪制方式一樣,因此計算完點之後需要把起始點即為終點,多算一個點才算閉合。

代碼如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; 
public class DrawLines: MonoBehaviour
{
    public float m_radius = 1.0f;
    public Material m_material;
    public float m_lineWidth = 1.0f;
    private List<Vector3> vPath = new List<Vector3>();
    // Start is called before the first frame update
    void Start()
    {
        int count = 60;
       for (int i=1; i<= (count+1); i++)
        {
            if(i == (count+1))
            {
                float x = Mathf.Cos(2 * Mathf.PI / count) * m_radius;
                float y = transform.localPosition.y;
                float z = Mathf.Sin(2 * Mathf.PI / count) * m_radius;
                vPath.Add(new Vector3(x, y, z));
            }
            else
            {
                float x = Mathf.Cos(2 * Mathf.PI / count * i) * m_radius;
                float y = transform.localPosition.y;
                float z = Mathf.Sin(2 * Mathf.PI / count * i) * m_radius;
                vPath.Add(new Vector3(x, y, z));
            } 
        } 
        GameObject lineGroup = new GameObject("LineGroup");
        GameObject lineObject = new GameObject("RadarLine");
        LineRenderer line = lineObject.AddComponent<LineRenderer>();
        line.material = m_material;
        line.useWorldSpace = false;
        line.positionCount = vPath.Count;
        line.startWidth = m_lineWidth;
        line.endWidth = m_lineWidth;
        line.SetPositions(vPath.ToArray()); 
    } 
    // Update is called once per frame
    void Update()
    {        
    }
}

運行一下看一下效果:

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀:

    None Found