├── .gitignore
├── LICENSE
├── README.md
└── src
├── GizmosExtensions.cs
└── GizmosExtensions.cs.meta
/.gitignore:
--------------------------------------------------------------------------------
1 | [Ll]ibrary/
2 | [Tt]emp/
3 | [Oo]bj/
4 | [Bb]uild/
5 | [Bb]uilds/
6 | Assets/AssetStoreTools*
7 |
8 | # Visual Studio cache directory
9 | .vs/
10 |
11 | # Autogenerated VS/MD/Consulo solution and project files
12 | ExportedObj/
13 | .consulo/
14 | *.csproj
15 | *.unityproj
16 | *.sln
17 | *.suo
18 | *.tmp
19 | *.user
20 | *.userprefs
21 | *.pidb
22 | *.booproj
23 | *.svd
24 | *.pdb
25 | *.opendb
26 |
27 | # Unity3D generated meta files
28 | *.pidb.meta
29 | *.pdb.meta
30 |
31 | # Unity3D Generated File On Crash Reports
32 | sysinfo.txt
33 |
34 | # Builds
35 | *.apk
36 | *.unitypackage
37 |
38 | *.meta
39 |
40 | gif.gif
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Marvin
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # GizmoExtensions
2 | Utility class(es) that extend Unity's Gizmos
3 |
4 | 
5 |
6 | ## Features
7 |
8 | * adds `Quaternion rotation` as an input parameter for the primitive types
9 | * Adds the following Gizmos:
10 | * Circles
11 | * Cylinders
12 | * Arcs
13 | * Arrows
14 | * Capsules
15 |
16 | ## Install
17 | Just drop the `/src` folder into your Unity Assets
18 |
19 | if you want to keep up to date you might want to clone the repo into your `Assets/` folder instead
20 |
21 | ## Contribute
22 | Do you have any Gizmo extensions? Feel free to contribute!
23 |
--------------------------------------------------------------------------------
/src/GizmosExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using UnityEngine;
3 |
4 | namespace Utils {
5 | public static class GizmosExtensions {
6 |
7 | ///
8 | /// Draws a wire cube with a given rotation
9 | ///
10 | ///
11 | ///
12 | ///
13 | public static void DrawWireCube(Vector3 center, Vector3 size, Quaternion rotation = default(Quaternion)) {
14 | var old = Gizmos.matrix;
15 | if (rotation.Equals(default(Quaternion)))
16 | rotation = Quaternion.identity;
17 | Gizmos.matrix = Matrix4x4.TRS(center, rotation, size);
18 | Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
19 | Gizmos.matrix = old;
20 | }
21 |
22 | public static void DrawArrow(Vector3 from, Vector3 to, float arrowHeadLength = 0.25f, float arrowHeadAngle = 20.0f) {
23 | Gizmos.DrawLine(from, to);
24 | var direction = to - from;
25 | var right = Quaternion.LookRotation(direction) * Quaternion.Euler(0, 180 + arrowHeadAngle, 0) * new Vector3(0, 0, 1);
26 | var left = Quaternion.LookRotation(direction) * Quaternion.Euler(0, 180 - arrowHeadAngle, 0) * new Vector3(0, 0, 1);
27 | Gizmos.DrawLine(to, to + right * arrowHeadLength);
28 | Gizmos.DrawLine(to, to + left * arrowHeadLength);
29 | }
30 |
31 | public static void DrawWireSphere(Vector3 center, float radius, Quaternion rotation = default(Quaternion)) {
32 | var old = Gizmos.matrix;
33 | if (rotation.Equals(default(Quaternion)))
34 | rotation = Quaternion.identity;
35 | Gizmos.matrix = Matrix4x4.TRS(center, rotation, Vector3.one);
36 | Gizmos.DrawWireSphere(Vector3.zero, radius);
37 | Gizmos.matrix = old;
38 | }
39 |
40 |
41 | ///
42 | /// Draws a flat wire circle (up)
43 | ///
44 | ///
45 | ///
46 | ///
47 | ///
48 | public static void DrawWireCircle(Vector3 center, float radius, int segments = 20, Quaternion rotation = default(Quaternion)) {
49 | DrawWireArc(center,radius,360,segments,rotation);
50 | }
51 |
52 | ///
53 | /// Draws an arc with a rotation around the center
54 | ///
55 | /// center point
56 | /// radiu
57 | /// angle in degrees
58 | /// number of segments
59 | /// rotation around the center
60 | public static void DrawWireArc(Vector3 center, float radius, float angle, int segments = 20,
61 | Quaternion rotation = default(Quaternion))
62 | {
63 | var old = Gizmos.matrix;
64 |
65 | // Check if the rotation is the default Quaternion (identity), and if so, set it to Quaternion.identity
66 | if (rotation.Equals(default(Quaternion)))
67 | rotation = Quaternion.identity;
68 |
69 | Gizmos.matrix = Matrix4x4.TRS(center, rotation, Vector3.one);
70 | Vector3 from = Vector3.forward * radius;
71 | var step = Mathf.RoundToInt(angle / segments);
72 | for (int i = 0; i <= angle; i += step)
73 | {
74 | var to = new Vector3(radius * Mathf.Sin(i * Mathf.Deg2Rad), 0, radius * Mathf.Cos(i * Mathf.Deg2Rad));
75 | Gizmos.DrawLine(from, to);
76 | from = to;
77 | }
78 |
79 | Gizmos.matrix = old;
80 | }
81 |
82 |
83 | ///
84 | /// Draws an arc with a rotation around an arbitraty center of rotation
85 | ///
86 | /// the circle's center point
87 | /// radius
88 | /// angle in degrees
89 | /// number of segments
90 | /// rotation around the centerOfRotation
91 | /// center of rotation
92 | public static void DrawWireArc(Vector3 center, float radius, float angle, int segments, Quaternion rotation, Vector3 centerOfRotation)
93 | {
94 |
95 | var old = Gizmos.matrix;
96 | if (rotation.Equals(default(Quaternion)))
97 | rotation = Quaternion.identity;
98 | Gizmos.matrix = Matrix4x4.TRS(centerOfRotation, rotation, Vector3.one);
99 | var deltaTranslation = centerOfRotation - center;
100 | Vector3 from = deltaTranslation + Vector3.forward * radius;
101 | var step = Mathf.RoundToInt(angle / segments);
102 | for (int i = 0; i <= angle; i += step)
103 | {
104 | var to = new Vector3(radius * Mathf.Sin(i * Mathf.Deg2Rad), 0, radius * Mathf.Cos(i * Mathf.Deg2Rad)) + deltaTranslation;
105 | Gizmos.DrawLine(from, to);
106 | from = to;
107 | }
108 |
109 | Gizmos.matrix = old;
110 | }
111 |
112 | ///
113 | /// Draws an arc with a rotation around an arbitraty center of rotation
114 | ///
115 | /// Gizmo matrix applied before drawing
116 | /// radius
117 | /// angle in degrees
118 | /// number of segments
119 | public static void DrawWireArc(Matrix4x4 matrix, float radius, float angle, int segments)
120 | {
121 | var old = Gizmos.matrix;
122 | Gizmos.matrix = matrix;
123 | Vector3 from = Vector3.forward * radius;
124 | var step = Mathf.RoundToInt(angle / segments);
125 | for (int i = 0; i <= angle; i += step)
126 | {
127 | var to = new Vector3(radius * Mathf.Sin(i * Mathf.Deg2Rad), 0, radius * Mathf.Cos(i * Mathf.Deg2Rad));
128 | Gizmos.DrawLine(from, to);
129 | from = to;
130 | }
131 |
132 | Gizmos.matrix = old;
133 | }
134 |
135 | ///
136 | /// Draws a wire cylinder face up with a rotation around the center
137 | ///
138 | ///
139 | ///
140 | ///
141 | ///
142 | public static void DrawWireCylinder(Vector3 center, float radius, float height, Quaternion rotation = default(Quaternion)) {
143 | var old = Gizmos.matrix;
144 | if (rotation.Equals(default(Quaternion)))
145 | rotation = Quaternion.identity;
146 | Gizmos.matrix = Matrix4x4.TRS(center,rotation,Vector3.one);
147 | var half = height / 2;
148 |
149 | //draw the 4 outer lines
150 | Gizmos.DrawLine( Vector3.right * radius - Vector3.up * half, Vector3.right * radius + Vector3.up * half);
151 | Gizmos.DrawLine( - Vector3.right * radius - Vector3.up * half, -Vector3.right * radius + Vector3.up * half);
152 | Gizmos.DrawLine( Vector3.forward * radius - Vector3.up * half, Vector3.forward * radius + Vector3.up * half);
153 | Gizmos.DrawLine( - Vector3.forward * radius - Vector3.up * half, - Vector3.forward * radius + Vector3.up * half);
154 |
155 | //draw the 2 cricles with the center of rotation being the center of the cylinder, not the center of the circle itself
156 | DrawWireArc(center + Vector3.up * half,radius,360,20,rotation, center);
157 | DrawWireArc(center + Vector3.down * half, radius, 360, 20, rotation, center);
158 | Gizmos.matrix = old;
159 | }
160 |
161 | ///
162 | /// Draws a wire capsule face up
163 | ///
164 | ///
165 | ///
166 | ///
167 | ///
168 | public static void DrawWireCapsule(Vector3 center, float radius, float height, Quaternion rotation = default(Quaternion))
169 | {
170 | if (rotation.Equals(default(Quaternion)))
171 | rotation = Quaternion.identity;
172 | var old = Gizmos.matrix;
173 | Gizmos.matrix = Matrix4x4.TRS(center,rotation,Vector3.one);
174 | var half = height / 2 - radius;
175 |
176 | //draw cylinder base
177 | DrawWireCylinder(center,radius,height - radius * 2,rotation);
178 |
179 | //draw upper cap
180 | //do some cool stuff with orthogonal matrices
181 | var mat = Matrix4x4.Translate(center + rotation * Vector3.up * half) * Matrix4x4.Rotate(rotation * Quaternion.AngleAxis(90,Vector3.forward));
182 | DrawWireArc(mat,radius,180,20);
183 | mat = Matrix4x4.Translate(center + rotation * Vector3.up * half) * Matrix4x4.Rotate(rotation * Quaternion.AngleAxis(90,Vector3.up)* Quaternion.AngleAxis(90, Vector3.forward));
184 | DrawWireArc(mat, radius, 180, 20);
185 |
186 | //draw lower cap
187 | mat = Matrix4x4.Translate(center + rotation * Vector3.down * half) * Matrix4x4.Rotate(rotation * Quaternion.AngleAxis(90, Vector3.up) * Quaternion.AngleAxis(-90, Vector3.forward));
188 | DrawWireArc(mat, radius, 180, 20);
189 | mat = Matrix4x4.Translate(center + rotation * Vector3.down * half) * Matrix4x4.Rotate(rotation * Quaternion.AngleAxis(-90, Vector3.forward));
190 | DrawWireArc(mat, radius, 180, 20);
191 |
192 | Gizmos.matrix = old;
193 |
194 | }
195 |
196 | ///
197 | /// Draws a visualization for Physics.BoxCast
198 | ///
199 | ///
200 | ///
201 | ///
202 | ///
203 | ///
204 | ///
205 | public static void DrawBoxCast(
206 | Vector3 center, Vector3 halfExtents, Vector3 direction, Quaternion orientation, float maxDistance, bool showCast = false)
207 | {
208 | DrawBoxCast(center, halfExtents, direction, orientation, maxDistance, Physics.DefaultRaycastLayers, showCast);
209 | }
210 |
211 | ///
212 | /// Draws a visualization for Physics.BoxCast
213 | ///
214 | ///
215 | ///
216 | ///
217 | ///
218 | ///
219 | ///
220 | ///
221 | public static void DrawBoxCast(
222 | Vector3 center, Vector3 halfExtents, Vector3 direction, Quaternion orientation, float maxDistance, int layerMask, bool showCast)
223 | {
224 | // calculate vars
225 | Color gizmosColor = Gizmos.color;
226 | Vector3 end = center + direction * maxDistance;
227 | Vector3 halfExtentsZ = orientation * Vector3.forward * halfExtents.z;
228 | Vector3 halfExtentsY = orientation * Vector3.up * halfExtents.y;
229 | Vector3 halfExtentsX = orientation * Vector3.right * halfExtents.x;
230 |
231 | // change color and draw hitmarker if show BoxCast
232 | if (showCast && Physics.BoxCast(center, halfExtents, direction, out RaycastHit hitInfo, orientation, maxDistance, layerMask))
233 | {
234 | Gizmos.color = Gizmos.color == Color.red ? Color.magenta : Color.red;
235 | Gizmos.DrawWireCube(hitInfo.point, 0.25f * Vector3.one);
236 | }
237 |
238 | // draw boxes
239 | Matrix4x4 matrix = Gizmos.matrix;
240 | Gizmos.matrix = Matrix4x4.TRS(center, orientation, Vector3.one);
241 | Gizmos.DrawWireCube(Vector3.zero, 2 * halfExtents);
242 | Gizmos.matrix = Matrix4x4.TRS(end, orientation, Vector3.one);
243 | Gizmos.DrawWireCube(Vector3.zero, 2 * halfExtents);
244 | Gizmos.matrix = matrix;
245 |
246 | // draw connect lines 1
247 | Gizmos.DrawLine(center - halfExtentsX - halfExtentsY - halfExtentsZ, end - halfExtentsX - halfExtentsY - halfExtentsZ);
248 | Gizmos.DrawLine(center + halfExtentsX - halfExtentsY - halfExtentsZ, end + halfExtentsX - halfExtentsY - halfExtentsZ);
249 | Gizmos.DrawLine(center - halfExtentsX + halfExtentsY - halfExtentsZ, end - halfExtentsX + halfExtentsY - halfExtentsZ);
250 | Gizmos.DrawLine(center + halfExtentsX + halfExtentsY - halfExtentsZ, end + halfExtentsX + halfExtentsY - halfExtentsZ);
251 |
252 | // draw connect lines 2
253 | Gizmos.DrawLine(center - halfExtentsX - halfExtentsY + halfExtentsZ, end - halfExtentsX - halfExtentsY + halfExtentsZ);
254 | Gizmos.DrawLine(center + halfExtentsX - halfExtentsY + halfExtentsZ, end + halfExtentsX - halfExtentsY + halfExtentsZ);
255 | Gizmos.DrawLine(center - halfExtentsX + halfExtentsY + halfExtentsZ, end - halfExtentsX + halfExtentsY + halfExtentsZ);
256 | Gizmos.DrawLine(center + halfExtentsX + halfExtentsY + halfExtentsZ, end + halfExtentsX + halfExtentsY + halfExtentsZ);
257 |
258 | // reset color
259 | Gizmos.color = gizmosColor;
260 | }
261 |
262 | }
263 | }
264 |
--------------------------------------------------------------------------------
/src/GizmosExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 02038ae7878b71c4db268360e58bc763
3 | timeCreated: 1527091523
4 | licenseType: Free
5 | MonoImporter:
6 | externalObjects: {}
7 | serializedVersion: 2
8 | defaultReferences: []
9 | executionOrder: 0
10 | icon: {instanceID: 0}
11 | userData:
12 | assetBundleName:
13 | assetBundleVariant:
14 |
--------------------------------------------------------------------------------