Files
AppleHillsProduction/Assets/Scripts/UI/Tracking/TrackableTarget.cs

85 lines
2.6 KiB
C#
Raw Normal View History

using UnityEngine;
namespace UI.Tracking
{
/// <summary>
/// Component that marks a GameObject as trackable by the OffScreenTrackerManager.
/// Automatically registers/unregisters with the manager when enabled/disabled.
/// </summary>
public class TrackableTarget : MonoBehaviour
{
[Header("Configuration")]
[Tooltip("Icon to display in the off-screen tracking pin")]
[SerializeField] private Sprite icon;
[Tooltip("Should this target display distance to the TrackingDistanceSource (e.g., player)?")]
[SerializeField] private bool trackDistance = false;
/// <summary>
/// The icon to display in the tracking pin
/// </summary>
public Sprite Icon => icon;
/// <summary>
/// Should this target track and display distance?
/// </summary>
public bool TrackDistance => trackDistance;
/// <summary>
/// The world position of this target
/// </summary>
public Vector3 WorldPosition => transform.position;
/// <summary>
/// Get the screen-space bounds of this target's visual representation.
/// Checks for Renderer or Collider2D to determine size.
/// </summary>
public Bounds GetWorldBounds()
{
// Try to get bounds from Renderer first (most accurate for visuals)
Renderer renderer = GetComponentInChildren<Renderer>();
if (renderer != null)
{
return renderer.bounds;
}
// Fallback to Collider2D
Collider2D collider = GetComponent<Collider2D>();
if (collider != null)
{
return collider.bounds;
}
// Last resort: just return position with minimal bounds
return new Bounds(transform.position, Vector3.one * 0.1f);
}
private void OnEnable()
{
// Register with the manager when enabled
if (OffScreenTrackerManager.Instance != null)
{
OffScreenTrackerManager.Instance.RegisterTarget(this);
}
}
private void OnDisable()
{
// Unregister from the manager when disabled
if (OffScreenTrackerManager.Instance != null)
{
OffScreenTrackerManager.Instance.UnregisterTarget(this);
}
}
/// <summary>
/// Allow runtime icon changes
/// </summary>
public void SetIcon(Sprite newIcon)
{
icon = newIcon;
}
}
}