Unity3D:Gizmos畫圓(原創)

來源:互聯網
上載者:User

Unity3D引擎技術交流QQ群:【21568554】

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,來調整圓的位置,旋轉,縮放。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.