We have a sheet of rectangles randomly scattered across the screen.

private List<Rect> testrect; 

It is not always clear where they really are, so I would like to highlight them - fill with color, add visible borders or something like that. How to do it? In standard C # there is such a solution.

 SolidBrush solidBrush = new SolidBrush( Color.FromArgb(255, 255, 0, 0)); e.Graphics.FillRectangle(solidBrush, 0, 0, 100, 60); 

But it, like, for Windows Forms. Will this go to Unity? Or is there a better way?

  • For example in OnGUI to draw - pavel
  • @pavel Thank you! But as? There, it seems, the coordinates in OnGUI do not go as usual, y is at zero from above, at maximum from below. Those. We do void OnGUI and there new Rect which coincides with ours? Can you answer in the answer? - Dmitrii
  • I'll sign for the company. - pavel
  • easy offtopic: fix the "array" to "list" - these are very different things - eastwing

1 answer 1

To draw on the scene, you can draw using the EditorGUI and its methods, for example DrawRect, in the usual OnGUI method. It looks like this:

 public static void DrawRect(Rect rect, Color color); 

that is, it accepts an object of type Rect as input and paints it in Color . The simplest form would be:

 void OnGUI() { Rect rect = new Rect(0, 0, 300, 300); Color color = Color.blue; EditorGUI.DrawRect(rect, color); } 

Accordingly, for all the rectangles in the list, it is enough to go over it and draw, such as

 Color color = Color.blue; foreach (var rect in list) { EditorGUI.DrawRect(rect, color); } 

In the Scene tab, you can use the OnDrawGizmos method and draw rectangles using Gizmos.DrawGUITexture . Accordingly, it is necessary to set the texture to a rectangle.

 public Texture2D texture2D; // Тут будет текстура, хотя бы просто материал красный void OnDrawGizmos() { foreach (var rect in list) { Gizmos.DrawGUITexture(rect, texture2D); } } 

Or in the same place to draw at the expense of Gizmos.DrawCube , simply taking coordinates from rect

 void OnDrawGizmos() { // Задаем цвет рисуемого предмета !!! Color color = Color.red; foreach (var rect in list) { Gizmos.DrawCube(rect.position, rect.size); } }