You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.3 KiB
C#
49 lines
1.3 KiB
C#
1 day ago
|
using UnityEngine;
|
||
|
|
||
|
public class OutlineManager : MonoBehaviour
|
||
|
{
|
||
|
private Outliner currentOutliner;
|
||
|
|
||
|
void Update()
|
||
|
{
|
||
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||
|
float minDistance = Mathf.Infinity;
|
||
|
Outliner closestOutliner = null;
|
||
|
|
||
|
foreach (Outliner outliner in FindObjectsOfType<Outliner>())
|
||
|
{
|
||
|
if (IsRayIntersectingRenderer(ray, outliner, out float distance) && distance < minDistance)
|
||
|
{
|
||
|
minDistance = distance;
|
||
|
closestOutliner = outliner;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (closestOutliner != currentOutliner)
|
||
|
{
|
||
|
RemoveOutline();
|
||
|
currentOutliner = closestOutliner;
|
||
|
currentOutliner?.EnableOutline();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private bool IsRayIntersectingRenderer(Ray ray, Outliner outliner, out float distance)
|
||
|
{
|
||
|
distance = Mathf.Infinity;
|
||
|
Renderer renderer = outliner.GetRenderer();
|
||
|
if (renderer == null) return false;
|
||
|
|
||
|
Bounds bounds = renderer.bounds;
|
||
|
return bounds.IntersectRay(ray, out distance);
|
||
|
}
|
||
|
|
||
|
private void RemoveOutline()
|
||
|
{
|
||
|
if (currentOutliner != null)
|
||
|
{
|
||
|
currentOutliner.DisableOutline();
|
||
|
currentOutliner = null;
|
||
|
}
|
||
|
}
|
||
|
}
|