浏览代码

output cpp log to c#

/siyaoH-1.17-PlatformMessage
guanghuispark 4 年前
当前提交
2055e29d
共有 3 个文件被更改,包括 52 次插入12 次删除
  1. 42
      Samples/UIWidgetsSamples_2019_4/Assets/UIWidgetsExample.cs
  2. 3
      engine/src/shell/platform/unity/uiwidgets_panel.cc
  3. 19
      engine/src/shell/platform/unity/uiwidgets_log.h

42
Samples/UIWidgetsSamples_2019_4/Assets/UIWidgetsExample.cs


using FontStyle = Unity.UIWidgets.ui.FontStyle;
using ui_ = Unity.UIWidgets.widgets.ui_;
namespace UIWidgetsSample {
using AOT;
using System;
using System.Runtime.InteropServices;
namespace UIWidgetsSample {
protected void OnEnable() {
// if you want to use your own font or font icons.
// FontManager.instance.addFont(Resources.Load<Font>(path: "path to your font"), "font family name");
public delegate void LogDelegate(IntPtr message, int iSize);
// load custom font with weight & style. The font weight & style corresponds to fontWeight, fontStyle of
// a TextStyle object
// FontManager.instance.addFont(Resources.Load<Font>(path: "path to your font"), "Roboto", FontWeight.w500,
// FontStyle.italic);
[DllImport("libUIWidgets_d.dll")]
public static extern void InitCSharpDelegate(LogDelegate log);
// add material icons, familyName must be "Material Icons"
// FontManager.instance.addFont(Resources.Load<Font>(path: "path to material icons"), "Material Icons");
//C# Function for C++'s call
[MonoPInvokeCallback(typeof(LogDelegate))]
public static void LogMessageFromCpp(IntPtr message, int iSize)
{
Debug.Log(Marshal.PtrToStringAnsi(message, iSize));
}
protected void OnEnable() {
// if you want to use your own font or font icons.
// FontManager.instance.addFont(Resources.Load<Font>(path: "path to your font"), "font family name");
// load custom font with weight & style. The font weight & style corresponds to fontWeight, fontStyle of
// a TextStyle object
// FontManager.instance.addFont(Resources.Load<Font>(path: "path to your font"), "Roboto", FontWeight.w500,
// FontStyle.italic);
// add material icons, familyName must be "Material Icons"
// FontManager.instance.addFont(Resources.Load<Font>(path: "path to material icons"), "Material Icons");
// Pass the C# Debug function reference to C++ to save, and then C++ calls C# through the function pointer
UIWidgetsExample.InitCSharpDelegate(UIWidgetsExample.LogMessageFromCpp);
base.OnEnable();
}

3
engine/src/shell/platform/unity/uiwidgets_panel.cc


#include "shell/common/switches.h"
#include "uiwidgets_system.h"
#include "unity_external_texture_gl.h"
#include "uiwidgets_log.h"
namespace uiwidgets {

UIWidgetsPanel_onMouseDown(UIWidgetsPanel* panel, float x, float y,
int button) {
panel->OnMouseDown(x, y, button);
UnityLog("OnMouseDown");
UnityLog("OnMouseUp");
}
UIWIDGETS_API(void)

19
engine/src/shell/platform/unity/uiwidgets_log.h


#include "runtime/mono_api.h"
namespace uiwidgets {
//use UnityLog just like printf, and this will output the result to unity
#define UnityLog(...) {char log_str[512] = { 0 }; sprintf_s(log_str, __VA_ARGS__); Debug::Log(log_str, strlen(log_str));}
extern "C"
{
class Debug
{
public:
static void (*Log)(char* message,int iSize);
};
void (*Debug::Log)(char* message, int iSize);
// export c++ function interface for c#
UIWIDGETS_API(void) InitCSharpDelegate(void (*Log)(char* message, int iSize)){Debug::Log = Log;}
}
} // namespace uiwidgets
正在加载...
取消
保存