# Painter2D

> Object to draw 2D vector graphics.

## Definition

* **Type:** Class
* **Namespace:** [UnityEngine.UIElements](/engine/6000.7/script-reference/unityengine/uielements.md)
* **Assembly:** UnityEngine.UIElementsModule
* **Implements:** [IDisposable](https://learn.microsoft.com/dotnet/api/system.idisposable)

```csharp
public class Painter2D : IDisposable
```

## Remarks

The example below demonstrates how to use the Painter2D class to draw content in a [VisualElement](/engine/6000.7/script-reference/unityengine/uielements/visualelement.md) with the [\_generateVisualContent](/engine/6000.7/script-reference/unityengine/uielements/visualelement/generatevisualcontent.md) callback.

You can also create a standalone .ctor object to draw content offscreen, and use the [Painter2D.SaveToVectorImage](/engine/6000.7/script-reference/unityengine/uielements/painter2d/savetovectorimage.md) method to save the painter content in a [VectorImage](/engine/6000.7/script-reference/unityengine/uielements/vectorimage.md) asset.

## Examples

```csharp
using UnityEngine;
using UnityEngine.UIElements;

[RequireComponent(typeof(UIDocument))]
public class Painter2DExample : MonoBehaviour
{
    public void OnEnable()
    {
        var doc = GetComponent<UIDocument>();
        doc.rootVisualElement.generateVisualContent += Draw;
    }

    void Draw(MeshGenerationContext ctx)
    {
        var painter = ctx.painter2D;
        painter.lineWidth = 10.0f;
        painter.lineCap = LineCap.Round;
        painter.strokeGradient = new Gradient() {
            colorKeys = new GradientColorKey[] {
                new GradientColorKey() { color = Color.red, time = 0.0f },
                new GradientColorKey() { color = Color.blue, time = 1.0f }
            }
        };
        painter.BeginPath();
        painter.MoveTo(new Vector2(10, 10));
        painter.BezierCurveTo(new Vector2(100, 100), new Vector2(200, 0), new Vector2(300, 100));
        painter.Stroke();
    }
}
```

## Constructors

| Constructor                                                                             | Description                           |
| --------------------------------------------------------------------------------------- | ------------------------------------- |
| [Painter2D()](/engine/6000.7/script-reference/unityengine/uielements/painter2d/ctor.md) | Initializes an instance of Painter2D. |

## Properties

| Property                                                                                                     | Description                                                                                                                                                                                                                      |
| ------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [dashOffset](/engine/6000.7/script-reference/unityengine/uielements/painter2d/dashoffset.md)                 | The offset to the first dash to use when drawing paths using [Painter2D.Stroke](/engine/6000.7/script-reference/unityengine/uielements/painter2d/stroke.md).                                                                     |
| [fillColor](/engine/6000.7/script-reference/unityengine/uielements/painter2d/fillcolor.md)                   | The color used for fill paths when using [Painter2D.Fill](/engine/6000.7/script-reference/unityengine/uielements/painter2d/fill.md).                                                                                             |
| [fillGradient](/engine/6000.7/script-reference/unityengine/uielements/painter2d/fillgradient.md)             | The fill gradient to use when using. [Painter2D.Fill](/engine/6000.7/script-reference/unityengine/uielements/painter2d/fill.md).                                                                                                 |
| [fillTexture](/engine/6000.7/script-reference/unityengine/uielements/painter2d/filltexture.md)               | Texture2D to use when filling paths using [Painter2D.Fill](/engine/6000.7/script-reference/unityengine/uielements/painter2d/fill.md).                                                                                            |
| [lineCap](/engine/6000.7/script-reference/unityengine/uielements/painter2d/linecap.md)                       | The cap to use when drawing paths using [Painter2D.Stroke](/engine/6000.7/script-reference/unityengine/uielements/painter2d/stroke.md).                                                                                          |
| [lineJoin](/engine/6000.7/script-reference/unityengine/uielements/painter2d/linejoin.md)                     | The join to use when drawing paths using [Painter2D.Stroke](/engine/6000.7/script-reference/unityengine/uielements/painter2d/stroke.md).                                                                                         |
| [lineWidth](/engine/6000.7/script-reference/unityengine/uielements/painter2d/linewidth.md)                   | The line width of draw paths when using [Painter2D.Stroke](/engine/6000.7/script-reference/unityengine/uielements/painter2d/stroke.md).                                                                                          |
| [miterLimit](/engine/6000.7/script-reference/unityengine/uielements/painter2d/miterlimit.md)                 | When using [LineJoin.Miter](/engine/6000.7/script-reference/unityengine/uielements/linejoin/miter.md) joins, this defines the limit on the ratio of the miter length to the stroke width before converting the miter to a bevel. |
| [strokeColor](/engine/6000.7/script-reference/unityengine/uielements/painter2d/strokecolor.md)               | The color of draw paths when using [Painter2D.Stroke](/engine/6000.7/script-reference/unityengine/uielements/painter2d/stroke.md).                                                                                               |
| [strokeFillGradient](/engine/6000.7/script-reference/unityengine/uielements/painter2d/strokefillgradient.md) | The stroke fill gradient to use when using. [Painter2D.Stroke](/engine/6000.7/script-reference/unityengine/uielements/painter2d/stroke.md).                                                                                      |
| [strokeGradient](/engine/6000.7/script-reference/unityengine/uielements/painter2d/strokegradient.md)         | The stroke gradient to use when using [Painter2D.Stroke](/engine/6000.7/script-reference/unityengine/uielements/painter2d/stroke.md).                                                                                            |

## Methods

| Method                                                                                                     | Description                                                                                                                                                                                                                                                                                               |
| ---------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Arc](/engine/6000.7/script-reference/unityengine/uielements/painter2d/arc.md)                             | Adds an arc to the current sub-path to the provided position, radius and angles.                                                                                                                                                                                                                          |
| [ArcTo](/engine/6000.7/script-reference/unityengine/uielements/painter2d/arcto.md)                         | Adds an arc to the current sub-path to the provided position using a control point.                                                                                                                                                                                                                       |
| [BeginPath](/engine/6000.7/script-reference/unityengine/uielements/painter2d/beginpath.md)                 | Begins a new path and empties the list of recorded sub-paths.                                                                                                                                                                                                                                             |
| [BezierCurveTo](/engine/6000.7/script-reference/unityengine/uielements/painter2d/beziercurveto.md)         | Adds a cubic bezier curve to the current sub-path to the provided position using two control points.                                                                                                                                                                                                      |
| [Clear](/engine/6000.7/script-reference/unityengine/uielements/painter2d/clear.md)                         | When created as a detached painter, clears the current content. Does nothing otherwise.                                                                                                                                                                                                                   |
| [ClosePath](/engine/6000.7/script-reference/unityengine/uielements/painter2d/closepath.md)                 | Closes the current sub-path with a straight line. If the sub-path is already closed, this does nothing.                                                                                                                                                                                                   |
| [Dispose](/engine/6000.7/script-reference/unityengine/uielements/painter2d/dispose.md)                     | Dispose the Painter2D object and free its internal unmanaged resources.                                                                                                                                                                                                                                   |
| [Fill](/engine/6000.7/script-reference/unityengine/uielements/painter2d/fill.md)                           | Fills the currently defined path.                                                                                                                                                                                                                                                                         |
| [GetDashPattern](/engine/6000.7/script-reference/unityengine/uielements/painter2d/getdashpattern.md)       | Copies the current dash pattern into the provided span.                                                                                                                                                                                                                                                   |
| [LineTo](/engine/6000.7/script-reference/unityengine/uielements/painter2d/lineto.md)                       | Adds a straight line to the current sub-path to the provided position.                                                                                                                                                                                                                                    |
| [MoveTo](/engine/6000.7/script-reference/unityengine/uielements/painter2d/moveto.md)                       | Begins a new sub-path at the provied coordinate.                                                                                                                                                                                                                                                          |
| [PopClip](/engine/6000.7/script-reference/unityengine/uielements/painter2d/popclip.md)                     | Removes the most recently added clipping region.                                                                                                                                                                                                                                                          |
| [PushClip](/engine/6000.7/script-reference/unityengine/uielements/painter2d/pushclip.md)                   | Uses the current path as a clipping region. This region clips subsequent `Fill()` and `Stroke()` operations.                                                                                                                                                                                              |
| [QuadraticCurveTo](/engine/6000.7/script-reference/unityengine/uielements/painter2d/quadraticcurveto.md)   | Adds a quadratic bezier curve to the current sub-path to the provided position using a control point.                                                                                                                                                                                                     |
| [SaveToVectorImage](/engine/6000.7/script-reference/unityengine/uielements/painter2d/savetovectorimage.md) | Saves the content of this [Painter2D](/engine/6000.7/script-reference/unityengine/uielements/painter2d.md) to a [VectorImage](/engine/6000.7/script-reference/unityengine/uielements/vectorimage.md) object.                                                                                              |
| [SetDashPattern](/engine/6000.7/script-reference/unityengine/uielements/painter2d/setdashpattern.md)       | Sets the dash pattern to use when drawing paths using [Painter2D.Stroke](/engine/6000.7/script-reference/unityengine/uielements/painter2d/stroke.md). Use [Painter2D.GetDashPattern](/engine/6000.7/script-reference/unityengine/uielements/painter2d/getdashpattern.md) to retrieve the current pattern. |
| [Stroke](/engine/6000.7/script-reference/unityengine/uielements/painter2d/stroke.md)                       | Strokes the currently defined path.                                                                                                                                                                                                                                                                       |
