riversong code showcase
This commit is contained in:
61
Source/Riversong/Game/DebugCommands/DrawGizmosSystem.cs
Normal file
61
Source/Riversong/Game/DebugCommands/DrawGizmosSystem.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user