Scripts/Utils/Outliner.cs
namespace Milaine.Utils;

public sealed class Outliner : Component {
  [Property, Group("Interaction")]
  public float MaxClickDistance { get; set; } = 1000f;

  [Property, Group("Outline"), Title("Color Hover")]
  public Color HoverOutlineColor { get; set; } = Color.White;

  [Property, Group("Outline"), Range(0.1f, 10f)]
  public float OutlineWidth { get; set; } = 0.5f;

  private HighlightOutline outline;

  protected override void OnAwake() {
  }

  protected override void OnStart() {
    outline = GameObject.GetOrAddComponent<HighlightOutline>();
    outline.Color = Color.Transparent;
    outline.Width = OutlineWidth;
  }

  protected override void OnUpdate() {
    var camera = Scene.Camera;

    if (!camera.IsValid()) {
      return;
    }

    var ray = camera.ScreenPixelToRay(Mouse.Position);
    var trace = Scene.Trace.Ray(ray, MaxClickDistance).Run();

    bool isHoveredNow = trace.Hit && trace.GameObject == GameObject;

    if (outline.IsValid()) {
      outline.Color = isHoveredNow ? HoverOutlineColor : Color.Transparent;
    }
  }
}