Skip to content

Instantly share code, notes, and snippets.

@hecomi
Last active January 4, 2016 16:42
Show Gist options
  • Save hecomi/4361657677ba7178027a to your computer and use it in GitHub Desktop.
Save hecomi/4361657677ba7178027a to your computer and use it in GitHub Desktop.
Unity でネイティブ側から Debug.Log() するヤツ
#if defined(__CYGWIN32__)
#define UNITY_INTERFACE_API __stdcall
#define UNITY_INTERFACE_EXPORT __declspec(dllexport)
#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(WINAPI_FAMILY)
#define UNITY_INTERFACE_API __stdcall
#define UNITY_INTERFACE_EXPORT __declspec(dllexport)
#elif defined(__MACH__) || defined(__ANDROID__) || defined(__linux__) || defined(__QNX__)
#define UNITY_INTERFACE_API
#define UNITY_INTERFACE_EXPORT
#else
#define UNITY_INTERFACE_API
#define UNITY_INTERFACE_EXPORT
#endif
extern "C"
{
using debug_log_func_type = void(UNITY_INTERFACE_API *)(const char*);
namespace
{
debug_log_func_type debug_log_func = nullptr;
}
void debug_log(const char* msg)
{
if (debug_log_func != nullptr) debug_log_func(msg);
}
UNITY_INTERFACE_EXPORT void set_debug_log_func(debug_log_func_type func)
{
debug_log_func = func;
}
UNITY_INTERFACE_EXPORT void debug_log_test()
{
debug_log("hogehoge");
}
}
using UnityEngine;
using System;
using System.Runtime.InteropServices;
public class NativeDebugLog : MonoBehaviour
{
public delegate void DebugLogDelegate(string str);
DebugLogDelegate debugLogFunc = msg => Debug.Log(msg);
[DllImport ("unity_debug_log")]
public static extern void set_debug_log_func(DebugLogDelegate func);
[DllImport ("unity_debug_log")]
public static extern void debug_log_test();
void Start()
{
debug_log_test();
set_debug_log_func(debugLogFunc);
debug_log_test();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment