How to Use Dynamic .NET TWAIN in Unity Projects

This tutorial aims to help .NET developers use Dynamic .NET TWAIN in Unity projects.

Download

Unity Infinite Loading Issue

It’s weird when running the Unity editor first time, I got stuck on the loading screen. If you suffer from the issue, remove %USERPROFILE%\AppData\Roaming\Unity, %USERPROFILE%\AppData\Local\Unity, and %USERPROFILE%\AppData\LocalLow\Unity, and then relaunch Unity.

Making Dynamic .NET TWAIN Work in Unity

Create a Unity project with a 2D template.

unity project

Create a UI button.

unity button

Next, we need to receive the button click event. Select Project > Assets to create a C# script file.

unity script

Double-click the script file to launch Visual Studio 2015.

The code snippet is very simple:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using Dynamsoft.TWAIN;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Threading;

public class ScanDocument : MonoBehaviour, Dynamsoft.TWAIN.Interface.IAcquireCallback
{

    TwainManager dynamicDotNetTwain = null;

    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        
    }

    public void Scan()
    {
        Debug.Log("Scan");
        if (dynamicDotNetTwain == null)
        {
            dynamicDotNetTwain = new TwainManager("");
            dynamicDotNetTwain.ScanInNewProcess = true;
            dynamicDotNetTwain.IfShowUI = false;
        }
        dynamicDotNetTwain.SelectSourceByIndex(0);
        dynamicDotNetTwain.CloseSource();
        dynamicDotNetTwain.OpenSource();
        dynamicDotNetTwain.AcquireImage(this as Dynamsoft.TWAIN.Interface.IAcquireCallback);
    }

    public void OnPreAllTransfers()
    {

    }

    public bool OnPreTransfer()
    {
        return true;
    }

    public bool OnPostTransfer(Bitmap bitmap)
    {
        Debug.Log("Saving images...");
        bitmap.Save("g:\\test.png");
        return true;
    }

    public void OnPostAllTransfers()
    {

    }

    public void OnSourceUIClose()
    {

    }

    public void OnTransferCancelled()
    {

    }

    public void OnTransferError()
    {

    }
}

Add DLL files and import namespaces for Dynamic .NET TWAIN and Bitmap. Note: do not add references in Visual Studio, because it won’t automatically import DLLs to Unity projects. And you will see ‘missing an assembly reference’ error in Unity editor console.

unity reference

The correct way is to drag DLL files (Dynamsoft.Twain.dll and System.Drawing.dll) to Assets. After that, the references will be added to Visual Studio project automatically.

Don’t add System.Drawing.dll from .NET Framework. Use Unity\Editor\Data\Mono\lib\mono\2.0\System.Drawing.dll provided by Mono.

Drag the C# script file to Main Camera.

Select the button and then bind the click event with ScanDocuments.Scan() function.

unity button event

Play the app. You can now scan and save documents in the Unity application.

unity play