您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)怎么在Unity3d中利用Gizmos畫一個(gè)圓,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
using UnityEngine; using System; public class HeGizmosCircle : MonoBehaviour { public Transform m_Transform; public float m_Radius = 1; // 圓環(huán)的半徑 public float m_Theta = 0.1f; // 值越低圓環(huán)越平滑 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; // 設(shè)置矩陣 Matrix4x4 defaultMatrix = Gizmos.matrix; Gizmos.matrix = m_Transform.localToWorldMatrix; // 設(shè)置顏色 Color defaultColor = Gizmos.color; Gizmos.color = m_Color; // 繪制圓環(huán) 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); // 恢復(fù)默認(rèn)顏色 Gizmos.color = defaultColor; // 恢復(fù)默認(rèn)矩陣 Gizmos.matrix = defaultMatrix; } }
把代碼拖到一個(gè)GameObject上,關(guān)聯(lián)該GameObject的Transform,然后就能夠在Scene視圖窗體里顯示一個(gè)圓了。
通過調(diào)整Transform的Position。Rotation。Scale,來調(diào)整圓的位置,旋轉(zhuǎn),縮放。
補(bǔ)充:??基于Unity3D使用LineRender組件繪制圓線
在此記錄一下使用Unity3D 的LineRender繪制線的過程,經(jīng)過測(cè)試LineRender與OpenGL的GL_LINE_STRIP繪制方式一樣,因此計(jì)算完點(diǎn)之后需要把起始點(diǎn)即為終點(diǎn),多算一個(gè)點(diǎn)才算閉合。
代碼如下:
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() { } }
關(guān)于怎么在Unity3d中利用Gizmos畫一個(gè)圓就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。