61 lines
1.6 KiB
C#
61 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
namespace DanieleMarotta.RiversongCodeShowcase
|
|
{
|
|
[GameSystemGroup(typeof(DebugSystemGroup))]
|
|
public class DrawGizmosSystem : GameSystem, IInitializable, IDisposable
|
|
{
|
|
[InjectService]
|
|
private IEngine _engine;
|
|
|
|
private DrawGizmosSceneProxy _sceneProxy;
|
|
|
|
private List<IDrawGizmos> _callbacks = new();
|
|
|
|
public DrawGizmosSystem(IServiceLocator serviceLocator) : base(serviceLocator)
|
|
{
|
|
}
|
|
|
|
public UniTask InitializeAsync()
|
|
{
|
|
#if DEBUG
|
|
_sceneProxy = new GameObject(nameof(DrawGizmosSceneProxy)).AddComponent<DrawGizmosSceneProxy>();
|
|
_sceneProxy.DrawGizmos += OnDrawGizmos;
|
|
#endif
|
|
|
|
foreach (var system in _engine.Systems)
|
|
if (system is IDrawGizmos callback)
|
|
_callbacks.Add(callback);
|
|
|
|
return UniTask.CompletedTask;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_sceneProxy.DrawGizmos -= OnDrawGizmos;
|
|
}
|
|
|
|
private void OnDrawGizmos(bool selected)
|
|
{
|
|
foreach (var callback in _callbacks) callback.DrawGizmos(selected);
|
|
}
|
|
|
|
private class DrawGizmosSceneProxy : MonoBehaviour
|
|
{
|
|
public event Action<bool> DrawGizmos;
|
|
|
|
private void OnDrawGizmos()
|
|
{
|
|
DrawGizmos?.Invoke(false);
|
|
}
|
|
|
|
private void OnDrawGizmosSelected()
|
|
{
|
|
DrawGizmos?.Invoke(true);
|
|
}
|
|
}
|
|
}
|
|
} |