• 09/04/13 02:23 PM
    Sign in to follow this  

    C++ Plugin Debug Log with Unity

    General and Gameplay Programming

    hojjatjafary
    When I was writing a C++ plugin for Unity in our upcoming iOS game (Shadow Blade), I was thinking that it would be very good if I could write some debug logs into the Unity console. After going on and progressing the plugin without logs, suddenly an idea came to my mind! This is an explanation of how to redirect some debug text from a C++ plugin to Unity's console. The method is based on Delegates in C#, Delegates can be treated as a typedef of a function pointers in C/C++ and you can call them from unmanaged code. You can write a function in C# that takes a string as it's argument and print it into the Unity's console window, and then pass a pointer of this function to C++ dll, so you can call the function from C++ by it's pointer, this will redirect your strings from C++ to log window. First of all define a delegate like this: [code] using System.Runtime.InteropServices; ... [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void MyDelegate(string str); ... [/code] 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): [code] static void CallBackFunction(string str) { Debug.Log("::CallBaaaaaaack : " + str); } [/code] In the [tt]Start()[/tt] function of your Unity script instantiate the defined delegate: [code] 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 ); [/code] The [tt]SetDebugFunction[/tt] is a method that assigns the function pointer to a pointer that you defined in your C++ code: [code] typedef void (*FuncPtr)( const char * ); FuncPtr Debug; [/code] 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. [code] extern "C" { EXPORT_API void SetDebugFunction( FuncPtr fp ) { Debug = fp; } } [/code] Don't forget to import it in your C# code: [code] [DllImport ("UnityPlugin")] public static extern void SetDebugFunction( IntPtr fp ); [/code] Now you can call this function everywhere in your C++ plugin: [code] ... Debug( "String From C++ Dll" ); ... [/code] And the resault: (I called a function from my plugin that logs a string into console window) Unity.png


      Report Article
    Sign in to follow this  


    User Feedback

    Create an account or sign in to leave a review

    You need to be a member in order to leave a review

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now


    cdizz

    Report ·

      

    Share this review


    Link to review
    Ectara

    Report ·

      

    Share this review


    Link to review
    Regashi

    Report ·

      

    Share this review


    Link to review