2017-05-02 2 views
7

Wenn Sie C++ - Plugins in Unity erstellen, ist es einfacher, Debug.Log zu verwenden, um Variablenwerte schnell anzuzeigen, aber diese Funktion ist nur von C# -Seite verfügbar. Dies erschwert das Debuggen des C++ - Plugins, da der Debugger von Unity dies nicht unterstützt. std::cout ist keine Option, da sie nicht im Editor angezeigt wird.Verwenden Sie Debug.Log aus C++

Ich schaute in die Unity C++ - API unter <UnityInstallationDirecory>\Editor\Data\PluginAPI, fand aber nichts über die Anmeldung in der API.

Haben Sie Vorschläge zur Anzeige im Editor-Protokoll von C++?

+1

Ist Reverse Aufrufen einer Option für Sie? http://answers.unity3d.com/questions/30620/how-to-debug-c-dll-code.html – Smartis

+1

@Smartis Danke. Das sieht vielversprechend aus, wird aber auf iOS aufgrund von AOT nicht funktionieren. Es ist ein guter Anfang. Ich werde eine Antwort schreiben, die auch auf iOS funktioniert, wenn ich es funktioniere. – Programmer

Antwort

5

Dies kann mit einer Callback-Funktion erfolgen. Senden Sie einen Zeiger auf eine Funktion von C# nach C++ speichern Sie es in einer temporären Variable. Setzen Sie Debug.Log innerhalb dieser Callback-Funktion und ermöglichen Sie es, Zeichenfolgen als Zeiger zu empfangen (IntPtr).

Wenn diese Funktion aus C++ aufgerufen wird, konvertieren Sie die IntPtr in Zeichenfolge mit Marshal.PtrToStringAnsi.

Damit es unter iOS funktioniert, müssen Sie das Attribut MonoPInvokeCallback für die Callback-Funktion verwenden.

C# (Attach zu einem leeren Gameobject):

using AOT; 
using System; 
using System.Runtime.InteropServices; 
using UnityEngine; 

public class DebugCPP : MonoBehaviour 
{ 

    // Use this for initialization 
    void OnEnable() 
    { 
     RegisterDebugCallback(OnDebugCallback); 
    } 

    //------------------------------------------------------------------------------------------------ 
    [DllImport("DebugLogPlugin", CallingConvention = CallingConvention.Cdecl)] 
    static extern void RegisterDebugCallback(debugCallback cb); 
    //Create string param callback delegate 
    delegate void debugCallback(IntPtr request, int color, int size); 
    enum Color { red, green, blue, black, white, yellow, orange }; 
    [MonoPInvokeCallback(typeof(debugCallback))] 
    static void OnDebugCallback(IntPtr request, int color, int size) 
    { 
     //Ptr to string 
     string debug_string = Marshal.PtrToStringAnsi(request, size); 

     //Add Specified Color 
     debug_string = 
      String.Format("{0}{1}{2}{3}{4}", 
      "<color=", 
      ((Color)color).ToString(), 
      ">", 
      debug_string, 
      "</color>" 
      ); 

     UnityEngine.Debug.Log(debug_string); 
    } 
} 

C++ (DebugCPP.h):

#pragma once 
#include<stdio.h> 
#include <string> 
#include <stdio.h> 
#include <sstream> 

#define DLLExport __declspec(dllexport) 

extern "C" 
{ 
    //Create a callback delegate 
    typedef void(*FuncCallBack)(const char* message, int color, int size); 
    static FuncCallBack callbackInstance = nullptr; 
    DLLExport void RegisterDebugCallback(FuncCallBack cb); 
} 

//Color Enum 
enum class Color { Red, Green, Blue, Black, White, Yellow, Orange }; 

class Debug 
{ 
public: 
    static void Log(const char* message, Color color = Color::Black); 
    static void Log(const std::string message, Color color = Color::Black); 
    static void Log(const int message, Color color = Color::Black); 
    static void Log(const char message, Color color = Color::Black); 
    static void Log(const float message, Color color = Color::Black); 
    static void Log(const double message, Color color = Color::Black); 
    static void Log(const bool message, Color color = Color::Black); 

private: 
    static void send_log(const std::stringstream &ss, const Color &color); 
}; 

C++ (DebugCPP.cpp):

#include "DebugCPP.h" 

#include<stdio.h> 
#include <string> 
#include <stdio.h> 
#include <sstream> 

//------------------------------------------------------------------- 
void Debug::Log(const char* message, Color color) { 
    if (callbackInstance != nullptr) 
     callbackInstance(message, (int)color, (int)strlen(message)); 
} 

void Debug::Log(const std::string message, Color color) { 
    const char* tmsg = message.c_str(); 
    if (callbackInstance != nullptr) 
     callbackInstance(tmsg, (int)color, (int)strlen(tmsg)); 
} 

void Debug::Log(const int message, Color color) { 
    std::stringstream ss; 
    ss << message; 
    send_log(ss, color); 
} 

void Debug::Log(const char message, Color color) { 
    std::stringstream ss; 
    ss << message; 
    send_log(ss, color); 
} 

void Debug::Log(const float message, Color color) { 
    std::stringstream ss; 
    ss << message; 
    send_log(ss, color); 
} 

void Debug::Log(const double message, Color color) { 
    std::stringstream ss; 
    ss << message; 
    send_log(ss, color); 
} 

void Debug::Log(const bool message, Color color) { 
    std::stringstream ss; 
    if (message) 
     ss << "true"; 
    else 
     ss << "false"; 

    send_log(ss, color); 
} 

void Debug::send_log(const std::stringstream &ss, const Color &color) { 
    const std::string tmp = ss.str(); 
    const char* tmsg = tmp.c_str(); 
    if (callbackInstance != nullptr) 
     callbackInstance(tmsg, (int)color, (int)strlen(tmsg)); 
} 
//------------------------------------------------------------------- 

//Create a callback delegate 
void RegisterDebugCallback(FuncCallBack cb) { 
    callbackInstance = cb; 
} 

Verwendung von C++:

Debug::Log("Hellow Red", Color::Red); 
Debug::Log("Hellow Green", Color::Green); 
Debug::Log("Hellow Blue", Color::Blue); 
Debug::Log("Hellow Black", Color::Black); 
Debug::Log("Hellow White", Color::White); 
Debug::Log("Hellow Yellow", Color::Yellow); 
Debug::Log("Hellow Orange", Color::Orange); 

Debug::Log(true, Color::Black); 
Debug::Log(false, Color::Red); 

Output vom Editor:

enter image description hereJetzt können Sie einfach Debug.LogWarning und Debug.LogError implementieren.

+3

dang, gleichmäßige Farben! Nettes Feature. Danke – Smartis

+3

Farbe ist wirklich nützlich, um verschiedene Protokolle leicht zu trennen. Bitte schön! – Programmer

Verwandte Themen