Jump to content
  • Advertisement
  • Remove ads and support GameDev.net for only $3. Learn more: The New GDNet+: No Ads!

  • 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: 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) Unity.png


      Report Article
    Sign in to follow this  


    User Feedback


    Hi, I've read your work and though you described pretty much what you wanted to do with your solution, I think that in all fairness, this article is pretty unclear. You seem to be hopping from one place to the next with fairly little explainers as to what exactly you are doing. The end result is pretty much misleading as it just shows that the calls of your methods work - nothing more. It's a hardcoded string. Log some actual feedback from the C++ plugin.

    To me, the two things that you certainly need to correct here are your explainers, your code examples and your end result.

    Now, to me what you've done is a bit of an overkill, to tell you the truth. However, I did not down vote your work based on that - if you manage do present your idea in a way that is clear, concise and actually showcases a real result, I'll be more then happy to revise my vote.

    Cheers. 

    Share this comment


    Link to comment
    Share on other sites

    Hi ivan,

    Thank you for the points you mensioned,

    I added a paragraph at the start of article that I think make many things clear,

     

    "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."

     

    log messages need not be always hard coded, but often are, strings like "plugin inited.", "initialization faild" or so on, but you can use sprintf or any method you know to prepare a log string and then print it, I wrote only a string to show how to use that function pointer.

     

    GL,

    Hojjat.

    Share this comment


    Link to comment
    Share on other sites

    Hi ivan,

    Thank you for the points you mensioned,

    I added a paragraph at the start of article that I think make many things clear,

     

    "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."

     

    log messages need not be always hard coded, but often are, strings like "plugin inited.", "initialization faild" or so on, but you can use sprintf or any method you know to prepare a log string and then print it, I wrote only a string to show how to use that function pointer.

     

    GL,

    Hojjat.

    Hi again, I think you missed my point a bit :) I know what you want to accomplish with this process of yours, however the overall material does not really illustrate the full picture. In my point - you are missing your target audience. If you want to make this a tutorial, you need more explainers for the people that are reading your material. More explainers in your text and more explainers through code. If this is an overview of a solution, you should try to post some more detailed test cases from your work and state why you've developed this solution and why it is better (in your mind) then for example writing directly to ../Logs/Unity/Player.log  or some other approach. That's just off the top of my head.

    I'm not trying to bash your effort or anything but this just needs more work. Now it looks more or less like a quick blog post for people who are already following your stuff.

    Share this comment


    Link to comment
    Share on other sites

    Good to know, but I'm always surprised to see people write c++ plugin for unity. Is it for gain performance ? Or it miss some important feature in unity ? (Or may be just you doesn't like c# ... ?)

    Share this comment


    Link to comment
    Share on other sites

    Good to know, but I'm always surprised to see people write c++ plugin for unity. Is it for gain performance ? Or it miss some important feature in unity ? (Or may be just you doesn't like c# ... ?)

    Hi,

    I don't like C# so much but this is not the reason, it is not a missing feature in Unity, some times you have some existing C++ code that you like to use them in Unity or you need to use another external plugin like Steam, you can also write plugins to gain performance improvements, here you can find more detail, they gained 25x improvements by writing C++ plugins.

    Share this comment


    Link to comment
    Share on other sites

    They gained 25x performance on some math intensive functions in their game. They never mention what it did to the overall performance or how much it improved by removing those few bottlenecks.

    Share this comment


    Link to comment
    Share on other sites

    Hi Hojjat. Exactly what I needed, and got it to work in no time. And personally, I think the writing is clear enough for anyone that's already somewhat familiar with basic C++ Unity plugin writing, Thanks!

    Share this comment


    Link to comment
    Share on other sites

    This worked perfectly for me and took about 20 minutes. The only thing I had trouble with was finding that the 'IntPtr' type requires 'using System' in the C# code. Thank you for this writeup! 

    Share this comment


    Link to comment
    Share on other sites


    Create an account or sign in to comment

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

    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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!