- Home /
How can I draw a box ingame?
How can I make a script, which does this (see image) ingame on the canvas? It doesn't have to select things inside it, just draw a box with outline, and transparent on the inside
Answer by kaosermail · Nov 26, 2018 at 09:21 PM
bool dragging = false;
Rect selectionBox;
Vector3 initialMousePosition;
Vector3 currentMousePosition;
void OnGUI()
{
Event event = Event.current;
if (event.type == EventType.MouseDrag)
{
if (!dragging)
{
dragging = true;
initialMousePosition = event.mousePosition;
}
}
if (event.type == EventType.MouseUp)
{
dragging= false;
}
if (dragging)
{
currentMousePosition = event.mousePosition;
selectionBox= new Rect(initialMousePosition.x, initialMousePosition.y, currentMousePosition.x - initialMousePosition.x, currentMousePosition.y - initialMousePosition.y);
GUI.Box(selectionBox, GUIContent.none);
}
}
Answer by piter00999 · Nov 26, 2018 at 09:13 PM
For box without outline you can just add Image object to your canvas manipulate its alpha and color using Image object color variable. Changing its size, position can be done using simple scripts (if you want to change them runtime). For outline maybe just use custom texture for Image object which would be something like white square with f.e. 1px of black border, but with this strange texture stretching can occur from what I know :/.
Your answer
Follow this Question
Related Questions
my GUI.box is transparent 1 Answer
Draw minimap on panel 2 Answers
How can I change the canvas being viewed by my camera? 1 Answer
Multiple button draws 1 Answer
Where is my HP Bar? 3 Answers