使用C ++中的Debug.Log
在Unity中制作C ++插件时,使用Debug.Log
可以更快地查看变量值,但此function仅适用于C#端。 这使得调试C ++插件非常困难,因为Unity的调试器不支持它。 std::cout
不是一个选项,因为它没有在编辑器中显示。
我查看了位于\Editor\Data\PluginAPI
的Unity C ++ API,但没有找到有关登录API的任何信息。
有关如何在C ++编辑器日志中显示的任何建议?
这可以通过回调函数完成。 发送一个函数指针从C#到C ++将它存储在一个临时变量中。 将Debug.Log
放在该回调函数中,并允许它接收字符串作为指针( IntPtr
)。
从C ++调用此函数时,将IntPtr
转换为Marshal.PtrToStringAnsi
字符串。
要使其在iOS上运行,您必须在回调函数上使用MonoPInvokeCallback
属性。
C# (附加到空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}", "", debug_string, "" ); UnityEngine.Debug.Log(debug_string); } }
C ++ ( DebugCPP.h
):
#pragma once #include #include #include #include #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 #include #include #include //------------------------------------------------------------------- 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; }
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);
编辑输出 :
现在,您可以轻松实现Debug.LogWarning
和Debug.LogError
。
- 从前端javascript中加载的DLL中调用函数(在clientside javascript中加载dll)
- 更改创建XML阅读器时使用的XmlDictionaryReaderQuotas对象上的MaxArrayLength属性
- 0xC0020001:字符串绑定无效。 – 仅在WPF中发生
- 如何突出显示DataGridView行或使其暂时发光?
- C# – 使用HTMLAgilityPack获取JavaScript变量值
- 如何使用C#来清理html页面上的输入?
- 发送电子邮件的问题
- 指向Windows 10 UWP时PostAsync抛出IRandomAccessStream错误
- 用于计算百分位数以移除exception值的快速算法