C++ Plugin Debug Log with Unity
When writing plugins for Unity or other engines it would be very helpful if you could write out some log information from your DLL to the engine's log window. This is what I did in our Indie game Shadow Blade at Dead Mage Studio.
using System.Runtime.InteropServices; ... [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void MyDelegate(string str); ... This is a delegate of type a function that has a string argument and returns nothing. Take care of your calling conventions, by default calling conventions of a C++ project in Visual Studio are __cdecl (/Gd) you can find it in the project properties -> C/C++ -> Advanced options. Then write a method that you will call it from C++ to print the log string, this is a member function of a Unity script file (a mono script): static void CallBackFunction(string str) { Debug.Log("::CallBaaaaaaack : " + str); } In the Start() function of your Unity script instantiate the defined delegate: MyDelegate callback_delegate = new MyDelegate( CallBackFunction ); // Convert callback_delegate into a function pointer that can be // used in unmanaged code. IntPtr intptr_delegate = Marshal.GetFunctionPointerForDelegate(callback_delegate); // Call the API passing along the function pointer. SetDebugFunction( intptr_delegate ); The SetDebugFunction is a method that assigns the function pointer to a pointer that you defined in your C++ code: typedef void (*FuncPtr)( const char * ); FuncPtr Debug; You can access this pointer in other source code by an extern modifier or any global access method you know such as writing a singleton class. extern "C" { EXPORT_API void SetDebugFunction( FuncPtr fp ) { Debug = fp; } } Don't forget to import it in your C# code: [DllImport ("UnityPlugin")] public static extern void SetDebugFunction( IntPtr fp ); Now you can call this function everywhere in your C++ plugin: ... Debug( "String From C++ Dll" ); ... And the resault: (I called a function from my plugin that logs a string into console window)
Related Tutorials
Balancing Game Development and Creative Direction in Indie Production
A practical look at how indie developers can balance creative direction with hands-on game development. This article co…
My Unreal Engine Development Process: From Core Idea to Playable Build
A practical overview of my Unreal Engine development process, covering how I move from a core game idea to a playable b…
Introducing LaneGraph: The Ultimate Road Network Solution for Unity
Discover the power of LaneGraph, a lightweight and flexible lane-based navigation system for Unity. LaneGraph makes it…
Retargeting Mixamo Characters with Root Motion In Unreal Engine 5.4.
I have always found Retargeting Mixamo Characters To have Root Motion is a serious lengthy Tast, Recently I stumbled up…
How To Make A SIMPLE Main Menu In Unity
In this tutorial for unity, i go over how to make a simple main menu for unity, it's an unlisted video because i do not…
Guide to Gameplay Balance
A perspective on competitive gameplay balance, from a background of "shooter" sandbox design.
Discussion