Skip to content

Code Examples

Below are commonly used methods along with example code snippets for various tasks.

Set brush size

paintManager.Brush.Size = value;

Set brush hardness

paintManager.Brush.Hardness = value;

Set brush color

var brushColor = paintManager.Brush.Color;
brushColor = new Color(color.r, color.g, color.b, brushColor.a);
paintManager.Brush.SetColor(brushColor);

Set brush opacity

var color = paintManager.Brush.Color;
color.a = value;
paintManager.Brush.SetColor(color);

Set brush angle

paintManager.Brush.RenderAngle = value;

Creating PaintManager from code

PaintManager can be created from code. Here is an example of creating a PaintManager:

public void AddPaintManager(GameObject objectForPainting, Material material, string shaderTextureName = "_MainTex")
{
    var paintManager = objectForPainting.AddComponent<PaintManager>();
    paintManager.ObjectForPainting = objectForPainting;
    paintManager.Material.SourceMaterial = material;
    paintManager.Material.ShaderTextureName = shaderTextureName;
    paintManager.Init();
}

To reinitialize a PaintManager with a single line of code, use:

paintManager.Init();

Change texture for painting using code

public void ChangeTexture(Texture texture)
{
    var material = paintManager.Material.SourceMaterial;
    material.SetTexture(paintManager.Material.ShaderTextureName, texture);
    paintManager.Material.SourceMaterial = material;
    paintManager.Init();
}

Render (repaint) paint object and its layers

paintManager.Render();

Set default texture size for objects without source texture

paintManager.Material.DefaultTextureWidth = ;
paintManager.Material.DefaultTextureHeight = ;

Fill layer texture with a color

This method clears the layer texture:

paintManager.Tool = PaintTool.Brush;
paintManager.ToolsManager.CurrentTool.FillWithColor(Color.clear);
paintManager.Render();

Change tool

if (PaintController.Instance.UseSharedSettings)
{
    // if PaintController has UseSharedSettings enabled
    PaintController.Instance.Tool = PaintTool...;
}
else
{
    // otherwise:
    paintManager.Tool = PaintTool...;
}

Set blur tool parameters

if (paintManager.ToolsManager.CurrentTool is BlurTool blurTool)
{
    blurTool.Settings.Iterations = 3;
    blurTool.Settings.BlurStrength = 2f;
    blurTool.Settings.DownscaleRatio = 1;
}

Set Gaussian blur tool parameters

if (paintManager.ToolsManager.CurrentTool is GaussianBlurTool gaussianBlurTool)
{
    gaussianBlurTool.Settings.KernelSize = 3;
    gaussianBlurTool.Settings.Spread = 5f;
}

Enable/Disable input for all PaintManagers

InputController.Instance.enabled = value;

Enable/Disable input for PaintManager instance

paintManager.PaintObject.ProcessInput = value;

Get the average color of a PaintManager

public AverageColorCalculator AverageColorCalculator;

AverageColorCalculator.OnGetAverageColor += color => Debug.Log("Average Color: " + color);

Save painting result to file

private void SaveResultToFile(PaintManager paintManager, string path)
{
    var texture2D = paintManager.GetResultTexture();
    var pngData = texture2D.EncodeToPNG();
    if (pngData != null)
    {
        File.WriteAllBytes(path, pngData);
    }
    Destroy(texture2D);
}

Save Result Texture to Application.persistentDataPath

private void SaveResultToPersistentDataPath(PaintManager paintManager, string fileName)
{
    var texture2D = paintManager.GetResultTexture();
    var pngData = texture2D.EncodeToPNG();
    if (pngData != null)
    {
        var filePath = System.IO.Path.Combine(Application.persistentDataPath, fileName);
        System.IO.File.WriteAllBytes(filePath, pngData);
    }
    Destroy(texture2D);
}

Load from texture

public void LoadResultTextureFromFile(PaintManager paintManager, string fileName)
{
    var filePath = System.IO.Path.Combine(Application.persistentDataPath, fileName);
    var textureData = System.IO.File.ReadAllBytes(filePath);
    var texture = new Texture2D(1, 1);
    texture.LoadImage(textureData);
    var shaderTextureName = paintManager.Material.ShaderTextureName;
    var previousTexture = paintManager.Material.SourceMaterial.GetTexture(shaderTextureName);
    paintManager.Material.SourceMaterial.SetTexture(shaderTextureName, texture);

    // if using a SpriteRenderer
    var spriteRenderer = paintManager.ObjectForPainting.GetComponent<SpriteRenderer>();
    if (spriteRenderer != null)
    {
        var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
        spriteRenderer.sprite = sprite;
    }

    // if using a RawImage
    var rawImage = paintManager.ObjectForPainting.GetComponent<RawImage>();
    if (rawImage != null)
    {
        rawImage.texture = texture;
    }

    // copy texture to new layer:
    paintManager.CopySourceTextureToLayer = true;
    // copy texture to background layer:
    // paintManager.UseSourceTextureAsBackground = true;
    paintManager.Init();
    paintManager.Material.SourceMaterial.SetTexture(shaderTextureName, previousTexture);
}

Managing LayersContainer with storage

// save LayersContainer to storage:
var filename = "someFileName";
if (paintManager.SaveToLayersContainer(fileName))
{
    Debug.Log("LayersContainer was successfully saved!");
}

// load LayersContainer from storage:
if (paintManager.LoadFromLayersContainer(fileName))
{
    Debug.Log("LayersContainer was successfully loaded!");
}

// delete LayersContainer from storage:
var filename = "someFileName";
if (paintManager.DeleteLayersContainer(fileName))
{
    Debug.Log("LayersContainer was successfully deleted!");
}

// check if LayersContainer exists in storage:
var filename = "someFileName";
if (paintManager.IsLayersContainerExists(fileName))
{
    Debug.Log("LayersContainer exist!");
}

Add a new layer

var layerName = "SomeLayerName";
paintManager.LayersController.AddNewLayer(layerName);

To create a layer from an existing texture:

var layerName = "SomeLayerName";
var layerTexture = ... // reference to Texture2D, note that the Texture dimensions must be as the source texture
paintManager.LayersController.AddNewLayer(layerName, layerTexture);

Remove layer

var layerIndex = 0;
paintManager.LayersController.RemoveLayer(layerIndex);

Drawing from code

To draw in texture space, use the following methods:

// draw a point where argument - position on the texture
paintManager.PaintObject.DrawPoint(new Vector3(512, 512));
// invoke FinishPainting() when drawing dots are finished. FinishPainting() method also saves the result to undo/redo system
paintManager.PaintObject.FinishPainting();


// draw a line where arguments - start and end position on the texture
paintManager.PaintObject.DrawLine(new Vector2(400, 612), new Vector2(100, 100));
// invoke FinishPainting() when drawing lines are finished. FinishPainting() method also saves the result to undo/redo system
paintManager.PaintObject.FinishPainting();

To draw in UV or World space:

InputData prevInputData = default;
var screenPos = new Vector3(...); // screen position of painting
var ray = Camera.main.ScreenPointToRay(screenPosition); //the ray used for raycasts
var inputData = new InputData(ray, screenPosition, 1f, InputSource.Screen, 1);
RaycastController.Instance.RequestRaycast(PaintManager, inputData, prevInputData, container =>
{
    var raycastData = RaycastController.Instance.TryGetRaycast(container, inputData.FingerId, inputData.Ray.origin);
    // check if we ray intersected an object
    if (!raycastData.Equals(default))
    {
        // then use methods for painting:
        // PaintManager.PaintObject.OnMouseDown(inputData, raycastData);
        PaintManager.PaintObject.OnMouseButton(inputData, raycastData);
        // PaintManager.Render();
        // PaintManager.PaintObject.OnMouseUp(inputData);
        // PaintManager.PaintObject.FinishPainting();
    }
});
prevInputData = inputData;

Check if user can undo/redo

paintManager.StatesController.CanUndo();
paintManager.StatesController.CanRedo();

Subscribe to change state events

paintManager.StatesController.OnUndoStatusChanged += OnUndoStatusChanged;
paintManager.StatesController.OnRedoStatusChanged += OnRedoStatusChanged;

private void OnUndoStatusChanged(bool canUndo)
{
    Debug.Log("Can undo: " + canUndo);
}

private void OnRedoStatusChanged(bool canRedo)
{
    Debug.Log("Can redo: " + canRedo);
}

Undo/Redo action

paintManager.StatesController.Undo();
paintManager.StatesController.Redo();

Remove undo/redo states

paintManager.StatesController.ResetStates();