1 using UnityEngine;
2 using System;
3
4 public class HeGizmosCircle : MonoBehaviour
5 {
6 public Transform m_Transform;
7 public float m_Radius = 1; // 圆环的半径
8 public float m_Theta = 0.1f; // 值越低圆环越平滑
9 public Color m_Color = Color.green; // 线框颜色
10
11 void Start()
12 {
13 if (m_Transform == null)
14 {
15 throw new Exception("Transform is NULL.");
16 }
17 }
18
19 void OnDrawGizmos()
20 {
21 if (m_Transform == null) return;
22 if (m_Theta < 0.0001f) m_Theta = 0.0001f;
23
24 // 设置矩阵
25 Matrix4x4 defaultMatrix = Gizmos.matrix;
26 Gizmos.matrix = m_Transform.localToWorldMatrix;
27
28 // 设置颜色
29 Color defaultColor = Gizmos.color;
30 Gizmos.color = m_Color;
31
32 // 绘制圆环
33 Vector3 beginPoint = Vector3.zero;
34 Vector3 firstPoint = Vector3.zero;
35 for (float theta = 0; theta < 2 * Mathf.PI; theta += m_Theta)
36 {
37 float x = m_Radius * Mathf.Cos(theta);
38 float z = m_Radius * Mathf.Sin(theta);
39 Vector3 endPoint = new Vector3(x, 0, z);
40 if (theta == 0)
41 {
42 firstPoint = endPoint;
43 }
44 else
45 {
46 Gizmos.DrawLine(beginPoint, endPoint);
47 }
48 beginPoint = endPoint;
49 }
50
51 // 绘制最后一条线段
52 Gizmos.DrawLine(firstPoint, beginPoint);
53
54 // 恢复默认颜色
55 Gizmos.color = defaultColor;
56
57 // 恢复默认矩阵
58 Gizmos.matrix = defaultMatrix;
59 }
60 }