Initial import
This commit is contained in:
67
Assets/Scripts/Hybrid/SelectionBoxUI.cs
Normal file
67
Assets/Scripts/Hybrid/SelectionBoxUI.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user