Unity3d 使用Gizmos畫一個(gè)圓圈
Gizmos是場景視圖里的一個(gè)可視化調(diào)試工具。
在做項(xiàng)目過程中。我們常常會用到它,比如:繪制一條射線等。
Unity3D 4.2版本號截至。眼下僅僅提供了繪制射線,線段,網(wǎng)格球體,實(shí)體球體,網(wǎng)格立方體,實(shí)體立方體,圖標(biāo)。GUI紋理,以及攝像機(jī)線框。
假設(shè)須要繪制一個(gè)圓環(huán)還須要自己寫代碼
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)過測試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() { } }
運(yùn)行一下看一下效果:
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
C#實(shí)現(xiàn)Windows Form調(diào)用R進(jìn)行繪圖與顯示的方法
眾所周知R軟件功能非常強(qiáng)大,可以很好的進(jìn)行各類統(tǒng)計(jì),并能輸出圖形。下面介紹一種R語言和C#進(jìn)行通信的方法,并將R繪圖結(jié)果顯示到WinForm UI界面上的方法,文中介紹的很詳細(xì),需要的朋友可以參考下。2017-02-02C#打包應(yīng)用程序,與.NETFramework介紹
C#打包應(yīng)用程序,與.NETFramework介紹,需要的朋友可以參考一下2013-05-05C#實(shí)現(xiàn)獲取枚舉中元素個(gè)數(shù)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)獲取枚舉中元素個(gè)數(shù)的方法,是深入理解C#程序設(shè)計(jì)所需要掌握的基本技巧,需要的朋友可以參考下2014-08-08C#基于正則表達(dá)式刪除字符串中數(shù)字或非數(shù)字的方法
這篇文章主要介紹了C#基于正則表達(dá)式刪除字符串中數(shù)字或非數(shù)字的方法,涉及C#針對數(shù)字的簡單正則匹配相關(guān)操作技巧,需要的朋友可以參考下2017-06-06C# 總結(jié)QueueUserWorkItem傳參幾種方式案例詳解
這篇文章主要介紹了C# 總結(jié)QueueUserWorkItem傳參幾種方式案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09c#實(shí)現(xiàn)windows遠(yuǎn)程桌面連接程序代碼
本篇文章主要介紹了c#實(shí)現(xiàn)windows遠(yuǎn)程桌面連接程序代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05