Displaying Logs: Unity3D Set-up

This post is a continuation of the series where we are looking into creating a C++ plugin that is meant to store data in SQLite3 database for both Unity3D and Unreal Engine 4.

In the previous post, we have set up the plugin DLL such that we can register a delegate that can be invoked from the plugin when a log needs to be sent to Unity3D project.

We have already imported the C++ DLL into the project in an earlier post in the series. We will use the same project to import the new DLL.

Import DLL

Let’s start by opening the Unity Project from the earlier post. If you dont have it already you can even download the project from the repository. We will now need to replace the DLL in the “\Plugins\x64” folder from the newly created DLL with logging class from the previous post. If you are unsure about where to get the DLL from or how it needs to be imported check out the “Import the Plugin” section from this post.

Since the DLL is a native plugin, we will have to restart Unity3D for it to be loaded. Once Unity3D editor is reopened, we are ready to add in the functionality receive the logs.

Create Delegate To Receive and Print Logs

On the Unity side we will need to create a delegate that will be invoked from the DLL when ever a log needs to be raised. Let’s start with creating this delegate first.

In the Storage Manager class that we started creating and working on here, we start by declaring the delegate and defining the same enum for the level of logs that is in the DLL.

    public delegate void LogsCallback(int a_iLogLevel, string a_strLogReceived);
    LogsCallback m_delOnLogReceived;
enum eLogType
    iLOG = 0,
    iWARNING = 1,
    iERROR = 2

We also need to declare the extern function “RegisterForLogs” that is implemented in the DLL.

    [DllImport(c_strDLLName, CallingConvention = CallingConvention.Cdecl)]
    private static extern void RegisterForLogs(LogsCallback a_delOnLogGenerated);

Let us now write the function that will actually display any logs that are received from the DLL and display it in the console window of Unity3D.

    private void OnLogReceived(int a_iLogLevel, string a_strLogReceived)
        eLogType enumLogType = (eLogType)a_iLogLevel;
        switch (enumLogType)
            case eLogType.iLOG:
            case eLogType.iWARNING:
            case eLogType.iERROR:

Everything is set up and now all that is left is to hook everything up. Let’s do this in the start function. Here is the entire start funtion:

    void Start ()

        m_delOnLogReceived += OnLogReceived;

        int num1 = 10;
        int num2 = 87;
        int sum = SumOf(num1, num2);
        string msg = string.Format("The Sum of {0} and {1} is {2}", num1, num2, sum);

As you can see we are first registering for the logs before calling the SumOf function from the DLL. When the sum of function is called from Unity, the log that we have added in the DLL will be invoked. Here is the final output in the console window. You can downalod the repository from here.

Log Received from Native Plugin

One thought on “Displaying Logs: Unity3D Set-up

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.