Last active
June 9, 2021 17:45
-
-
Save darkon76/ce66c3897a85a9c95af3a26ec417d97c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
using UnityEngine; | |
public static class GizmoHelper | |
{ | |
private static Color _color; | |
private static GizmoHelperDrawer _gizmoHelperComp; | |
public static Color Color | |
{ | |
get => _color; | |
set | |
{ | |
_color = value; | |
_gizmoHelperComp.Actions.Add(()=>{Gizmos.color = value;}); | |
} | |
} | |
[Conditional("UNITY_EDITOR")] | |
public static void DrawCube(Vector3 center, Vector3 size) | |
{ | |
_gizmoHelperComp.Actions.Add(()=>{Gizmos.DrawCube(center, size);}); | |
} | |
[Conditional("UNITY_EDITOR")] | |
public static void DrawLine(Vector3 @from, Vector3 to) | |
{ | |
_gizmoHelperComp.Actions.Add(()=>{Gizmos.DrawLine(@from, to);}); | |
} | |
[Conditional("UNITY_EDITOR")] | |
public static void DrawRay(Vector3 @from, Vector3 direction) | |
{ | |
_gizmoHelperComp.Actions.Add(()=>{Gizmos.DrawRay(@from, direction);}); | |
} | |
[Conditional("UNITY_EDITOR")] | |
public static void DrawSphere(Vector3 center, float radius) | |
{ | |
_gizmoHelperComp.Actions.Add(()=>{Gizmos.DrawSphere(center, radius);}); | |
} | |
[Conditional("UNITY_EDITOR")] | |
public static void DrawSphere(Vector3 center, float radius, Color color) | |
{ | |
Color = color; | |
DrawSphere(center, radius); | |
} | |
[Conditional("UNITY_EDITOR")] | |
public static void DrawWireCube(Vector3 center, Vector3 size) | |
{ | |
_gizmoHelperComp.Actions.Add(()=>{Gizmos.DrawWireCube(center, size);}); | |
} | |
[Conditional("UNITY_EDITOR")] | |
public static void DrawWireSphere(Vector3 center, float radius) | |
{ | |
_gizmoHelperComp.Actions.Add(()=>{Gizmos.DrawWireSphere(center, radius);}); | |
} | |
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] | |
private static void OnRuntimeMethodLoad() | |
{ | |
var go = new GameObject("_GizmoHelper"); | |
GameObject.DontDestroyOnLoad(go); | |
_gizmoHelperComp = go.AddComponent<GizmoHelperDrawer>(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public sealed class GizmoHelperDrawer : MonoBehaviour | |
{ | |
public List<Action> Actions = new List<Action>(); | |
void OnDrawGizmos() | |
{ | |
foreach (var action in Actions) | |
{ | |
action?.Invoke(); | |
} | |
Actions.Clear(); | |
} | |
} |
Author
darkon76
commented
Apr 22, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment