Skip to content

Instantly share code, notes, and snippets.

@darkon76
Last active June 9, 2021 17:45
Show Gist options
  • Save darkon76/ce66c3897a85a9c95af3a26ec417d97c to your computer and use it in GitHub Desktop.
Save darkon76/ce66c3897a85a9c95af3a26ec417d97c to your computer and use it in GitHub Desktop.
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>();
}
}
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();
}
}
@darkon76
Copy link
Author

GizmoHelper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment