diff --git a/com.unity.uiwidgets/Runtime/engine2/UIWidgetsPanel.cs b/com.unity.uiwidgets/Runtime/engine2/UIWidgetsPanel.cs index 26897c80..a2371f6e 100644 --- a/com.unity.uiwidgets/Runtime/engine2/UIWidgetsPanel.cs +++ b/com.unity.uiwidgets/Runtime/engine2/UIWidgetsPanel.cs @@ -79,7 +79,7 @@ namespace Unity.UIWidgets.engine2 { _width, _height, _devicePixelRatio, Application.streamingAssetsPath); Input_OnEnable(); - NativeConsole.InitNativeConsoleDelegate(NativeConsole.LogMessageFromCpp); + NativeConsole.OnEnable(); } protected virtual void main() { diff --git a/com.unity.uiwidgets/Runtime/engine2/native_console.cs b/com.unity.uiwidgets/Runtime/engine2/native_console.cs index 750cdf49..480727e2 100644 --- a/com.unity.uiwidgets/Runtime/engine2/native_console.cs +++ b/com.unity.uiwidgets/Runtime/engine2/native_console.cs @@ -5,14 +5,18 @@ using System.Runtime.InteropServices; using NativeBindings = Unity.UIWidgets.ui.NativeBindings; public static class NativeConsole { - public delegate void LogDelegate(IntPtr message, int iSize); + internal delegate void LogDelegate(IntPtr message, int iSize); [DllImport(NativeBindings.dllName)] - public static extern void InitNativeConsoleDelegate(LogDelegate log); + internal static extern void InitNativeConsoleDelegate(LogDelegate log); [MonoPInvokeCallback(typeof(LogDelegate))] - public static void LogMessageFromCpp(IntPtr message, int iSize) { + internal static void LogMessageFromCpp(IntPtr message, int iSize) { Debug.Log(Marshal.PtrToStringAnsi(message, iSize)); } + public static void OnEnable() + { + InitNativeConsoleDelegate(LogMessageFromCpp); + } } diff --git a/engine/Build.bee.cs b/engine/Build.bee.cs index a8cc1ee3..635e251d 100644 --- a/engine/Build.bee.cs +++ b/engine/Build.bee.cs @@ -308,6 +308,8 @@ class Build "src/shell/platform/unity/unity_surface_manager.h", "src/shell/platform/unity/win32_task_runner.cc", "src/shell/platform/unity/win32_task_runner.h", + "src/shell/platform/unity/unity_console.cc", + "src/shell/platform/unity/unity_console.h", "src/shell/version/version.cc", "src/shell/version/version.h", diff --git a/engine/src/shell/platform/unity/uiwidgets_panel.cc b/engine/src/shell/platform/unity/uiwidgets_panel.cc index 21b9e717..a8fd7b79 100644 --- a/engine/src/shell/platform/unity/uiwidgets_panel.cc +++ b/engine/src/shell/platform/unity/uiwidgets_panel.cc @@ -11,7 +11,6 @@ #include "shell/common/switches.h" #include "uiwidgets_system.h" #include "unity_external_texture_gl.h" -#include "unity_console.h" namespace uiwidgets { @@ -459,13 +458,11 @@ UIWIDGETS_API(void) UIWidgetsPanel_onMouseDown(UIWidgetsPanel* panel, float x, float y, int button) { panel->OnMouseDown(x, y, button); - UnityConsole::WriteLine("OnMouseDown"); } UIWIDGETS_API(void) UIWidgetsPanel_onMouseUp(UIWidgetsPanel* panel, float x, float y, int button) { panel->OnMouseUp(x, y, button); - UnityConsole::WriteLine("OnMouseUp"); } UIWIDGETS_API(void) diff --git a/engine/src/shell/platform/unity/unity_console.cc b/engine/src/shell/platform/unity/unity_console.cc new file mode 100644 index 00000000..7a6ce8ac --- /dev/null +++ b/engine/src/shell/platform/unity/unity_console.cc @@ -0,0 +1,22 @@ +#include "unity_console.h" +#include + +namespace uiwidgets { + + void UnityConsole::WriteLine(const char* fmt, ...) { + char log_str[512] = { 0 }; + va_list ap; + va_start(ap, fmt); + vsprintf(log_str, fmt, ap); + _log(log_str, strlen(log_str)); + va_end(ap); +} + +LogDelegate UnityConsole::_log; + +UIWIDGETS_API(void) +InitNativeConsoleDelegate(LogDelegate Log) { + UnityConsole::_log = Log; +} + +} diff --git a/engine/src/shell/platform/unity/unity_console.h b/engine/src/shell/platform/unity/unity_console.h index e2ad19cd..ebc95bf2 100644 --- a/engine/src/shell/platform/unity/unity_console.h +++ b/engine/src/shell/platform/unity/unity_console.h @@ -1,27 +1,26 @@ +#pragma once #include "runtime/mono_api.h" namespace uiwidgets { typedef void (*LogDelegate)(char* message, int iSize); -extern "C"{ + class UnityConsole{ public: - static void (*Log)(char* message,int iSize); - /* + static LogDelegate _log; + + /** output the log to unity editor console window + @param fmt log format + @param ... log args + @return null + + example: + UnityConsole::WriteLine("output log without fmt param"); + UnityConsole::WriteLine("%s: %d + %d = %d","output log with param", 1, 2, 3); */ - static void WriteLine(const char* fmt, ...){ - char log_str[512] = { 0 }; - va_list ap; - va_start(ap, fmt); - sprintf_s(log_str, fmt, ap); - Log(log_str, strlen(log_str)); - va_end(ap); - } + static void WriteLine(const char* fmt, ...); }; - void (*UnityConsole::Log)(char* message, int iSize); - UIWIDGETS_API(void) - InitNativeConsoleDelegate(LogDelegate Log){ UnityConsole::Log = Log; } -} + } // namespace uiwidgets