Tanoda
GizmoDrawer.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using System.Linq;
5using UnityEngine;
6
8{
9 public sealed class GizmoDrawer
10 {
11 public const string GizmoObjectName = "RuntimeEditor_Gizmo";
12
13 private readonly List<IGizmoEntry> _lines = new List<IGizmoEntry>();
14 private bool _show;
15
16 public ICollection<IGizmoEntry> Lines
17 {
18 get
19 {
20 return _lines;
21 }
22 }
23
24 public GizmoDrawer(MonoBehaviour coroutineTarget)
25 {
26 coroutineTarget.StartCoroutine(EndOfFrame());
27 }
28
29 public bool Show
30 {
31 get
32 {
33 return ShowGizmos && (_show || ShowGizmosOutsideEditor);
34 }
35
36 set
37 {
38 _show = value;
39 }
40 }
41
42 public static bool ShowGizmos { get; set; }
43 public static bool ShowGizmosOutsideEditor { get; set; }
44
45 public static void DisplayControls()
46 {
47 if (!UnityFeatureHelper.SupportsVectrosity) return;
48
49 GUILayout.BeginHorizontal(GUI.skin.box);
50 {
51 GUILayout.Label("Gizmos");
52 GUILayout.FlexibleSpace();
53 ShowGizmos = GUILayout.Toggle(ShowGizmos, "Show selection");
54 ShowGizmosOutsideEditor = GUILayout.Toggle(ShowGizmosOutsideEditor, "When closed");
55 }
56 GUILayout.EndHorizontal();
57 }
58
59 public void UpdateState(Transform rootTransform)
60 {
61 ClearGizmos();
62
63 if (rootTransform != null && Show)
64 UpdateStateInt(rootTransform);
65 }
66
67 private void ClearGizmos()
68 {
69 if (_lines.Count == 0) return;
70 _lines.ForEach(x => x.Destroy());
71 _lines.Clear();
72 }
73
74 private IEnumerator EndOfFrame()
75 {
76 while (true)
77 {
78 yield return new WaitForEndOfFrame();
79 if (Show)
80 {
81 foreach (var x in _lines)
82 x.Draw();
83 }
84 else
85 {
86 ClearGizmos();
87 }
88 }
89 }
90
91 private void UpdateStateInt(Transform rootTransform)
92 {
93 var renderer = rootTransform.GetComponent<Renderer>();
94
95 if (renderer == null)
96 {
97 foreach (Transform tr in rootTransform)
98 UpdateStateInt(tr);
99 return;
100 }
101
102 var children = renderer.GetComponentsInChildren<Renderer>();
103
104 // Force update the bounds
105 var s = renderer as SkinnedMeshRenderer;
106 if (s != null)
107 s.updateWhenOffscreen = true;
108 foreach (var child in children.OfType<SkinnedMeshRenderer>())
109 child.updateWhenOffscreen = true;
110
111 var bounds2D = new RendererGizmo(renderer, children);
112 _lines.Add(bounds2D);
113 }
114
115 /*public class ColliderGizmo : IGizmoEntry
116 {
117 public ColliderGizmo(Collider collider)
118 {
119 if (collider is BoxCollider bc)
120 {
121 //bc.size
122 }
123 else if (collider is SphereCollider sc)
124 {
125 //sc.radius
126 }
127 //collider.
128 VectorLine = new VectorLine(BoundsObjectName, new List<Vector2>(8), 1f, LineType.Discrete);
129 }
130
131 public void Destroy()
132 {
133 throw new System.NotImplementedException();
134 }
135
136 public void Draw()
137 {
138 throw new System.NotImplementedException();
139 }
140 }*/
141 }
142}
ICollection< IGizmoEntry > Lines
Definition: GizmoDrawer.cs:17
GizmoDrawer(MonoBehaviour coroutineTarget)
Definition: GizmoDrawer.cs:24
void UpdateState(Transform rootTransform)
Definition: GizmoDrawer.cs:59