UGTK cat mascotUGTKengine within an engine Request access

UGTK / Toolkit / Ui Module / Drawing System Completed

Ui Module — UGTK

Drawing System

Summary

The Drawing System is a 2D paint surface built on a RawImage + Texture2D. Users can draw with mouse/touch, and other UI objects can paint onto the surface by their screen position. The surface can be cleared, saved to PNG, and exposes its content as a Sprite through events.


Content


Modules Dependencies

This module has no hard UGTK dependencies. It uses Unity UI (RawImage) and the EventSystem.


Setup

  1. Put the DrawSurface prefab on your Canvas. Mn_DrawingSurface2D is the surface: it takes the pointer events and writes into a texture.
  2. Set the texture size, the brush and the colour on it. The size is the resolution of the drawing, and it cannot be changed later without discarding what is on it - a surface sized for a thumbnail cannot be exported as a poster afterwards.
  3. Use PenDrawing if you want a visible pen following the finger; Mn_UiPenSelfDrawer is what makes it track the pointer.
  4. Take the result from the DrawingSurfaceSpriteEvent, which hands you a Sprite you can save, upload or display.

The surface implements Unity's own pointer interfaces, so it obeys the normal UI raycast rules: anything drawn on top of it with a raycast target will take the strokes instead, and that is the usual reason a surface appears not to respond.


How To Use

  1. Add Mn_DrawingSurface2D to a GameObject with a RawImage (assign targetImage or leave empty to auto-grab it). Configure texture size, background color, pen color and penRadius.
  2. With enablePointerDrawing on, the user draws directly with mouse/touch (multi-touch aware, with line smoothing between drag points).
  3. Paint from other scripts via DrawAtScreenPosition, DrawAtWorldPosition or DrawAtLocalPoint.
  4. Use Clear(), SaveToPng(fileName), SetPenColor() and SetPenRadius(). Hook the Sprite-only events OnDrawBegin, OnDrawEnd, OnSurfaceCleared and OnSavedToPng.
  5. To make a moving UI object paint by itself, add Mn_UiPenSelfDrawer, assign the target surface, and choose point-brush or full-rect-area brush mode.

Testing Scene

Open DrawingScene in the module's Debug/ folder.

What to do, in order:

  1. Draw a stroke with the mouse, then clear.
  2. Draw very fast, in a wide sweep.
  3. Draw off the edge of the surface and back on.
  4. Fill the surface with many strokes.

Step 2 is the honest test of any drawing tool: a fast stroke arrives as widely spaced points, and whether the line stays smooth or turns into a dotted trail depends entirely on the interpolation between them.

Two strokes drawn on the surface in DrawingScene. The pink square is the pen following the pointer; the line appears as the stroke is made, not when it is released.


Technical Info


© 2026 Marcello De Bonis. All rights reserved