Files
EE2Clone/Assets/Scripts/Hybrid/SelectionBoxUI.cs
ldbetteridge 58da5d1d71 Initial import
2026-03-31 15:59:23 +01:00

68 lines
2.1 KiB
C#

using UnityEngine;
namespace EE2Clone.Hybrid
{
/// <summary>
/// Draws the rubber-band selection box on screen when the player is drag-selecting.
/// </summary>
public class SelectionBoxUI : MonoBehaviour
{
[SerializeField] private Color boxColor = new(0.2f, 0.8f, 0.2f, 0.25f);
[SerializeField] private Color borderColor = new(0.2f, 0.8f, 0.2f, 0.8f);
private Texture2D _fillTexture;
private Texture2D _borderTexture;
private void Awake()
{
_fillTexture = new Texture2D(1, 1);
_fillTexture.SetPixel(0, 0, Color.white);
_fillTexture.Apply();
_borderTexture = new Texture2D(1, 1);
_borderTexture.SetPixel(0, 0, Color.white);
_borderTexture.Apply();
}
private void OnGUI()
{
var sel = SelectionManager.Instance;
if (sel == null || !sel.IsDragging) return;
var start = sel.DragStart;
var end = sel.DragEnd;
// Convert to GUI coordinates (Y is flipped)
start.y = Screen.height - start.y;
end.y = Screen.height - end.y;
var rect = new Rect(
Mathf.Min(start.x, end.x),
Mathf.Min(start.y, end.y),
Mathf.Abs(end.x - start.x),
Mathf.Abs(end.y - start.y)
);
// Draw fill
GUI.color = boxColor;
GUI.DrawTexture(rect, _fillTexture);
// Draw border
GUI.color = borderColor;
float b = 2f;
GUI.DrawTexture(new Rect(rect.x, rect.y, rect.width, b), _borderTexture);
GUI.DrawTexture(new Rect(rect.x, rect.yMax - b, rect.width, b), _borderTexture);
GUI.DrawTexture(new Rect(rect.x, rect.y, b, rect.height), _borderTexture);
GUI.DrawTexture(new Rect(rect.xMax - b, rect.y, b, rect.height), _borderTexture);
GUI.color = Color.white;
}
private void OnDestroy()
{
if (_fillTexture != null) Destroy(_fillTexture);
if (_borderTexture != null) Destroy(_borderTexture);
}
}
}