如何在 .dll 中为 Unity 定义预处理器 ifdef?
How to define preprocessor ifdef in .dll for Unity?
我有一个在 Unity 中使用的 .dll
(非托管代码)。我需要在 .dll
中添加一些功能,以便为 Android 设备构建 Unity 项目。
因此,在我的案例中,我需要使用 if
等预处理器功能
我试图将它添加到我的 .cpp
文件中以进行测试
DecoderStream::DecoderStream()
{
debug_in_unity("DecoderStream", "DecoderStream"); // 1 comment
#if UNITY_ANDROID && !UNITY_EDITOR
debug_in_unity("DecoderStream", "UNITY_ANDROID"); // 2 comment
#elif UNITY_EDITOR
debug_in_unity("DecoderStream", "UNITY_EDITOR"); // 3 comment
m_p_tex_decoder = new DecoderTextureFFmpeg();
#else
debug_in_unity("DecoderStream", "else"); // 4 comment
#endif
}
如果我编译此代码并在 Unity 中 运行 它,我会看到 1 comment
和 4 comment
我看到预处理器条件不起作用,我的意思是它无法识别无论是 android 还是 unity 编辑器...
我做错了什么?
来自 here,顾名思义
Pre-processors are a way of making text processing with your C program before they are actually compiled.
因此,如果您不编译已经定义相应指令的 DLL,您的代码基本上只会导致编译
DecoderStream::DecoderStream()
{
debug_in_unity("DecoderStream", "DecoderStream"); // 1 comment
debug_in_unity("DecoderStream", "else"); // 4 comment
}
使用它在 compile-time“知道”的指令。由于 DLL 已经被编译为机器代码,因此它不再“知道”pre-processors 的概念。
长话短说:您必须在 Unity 端使用 pre-processors 并实现不同的方法,例如
DecoderStream::DecoderStreamAndroid()
{
debug_in_unity("DecoderStream", "DecoderStream"); // 1 comment
debug_in_unity("DecoderStream", "UNITY_ANDROID"); // 2 comment
}
DecoderStream::DecoderStreamEditor()
{
debug_in_unity("DecoderStream", "DecoderStream"); // 1 comment
debug_in_unity("DecoderStream", "UNITY_EDITOR"); // 3 comment
m_p_tex_decoder = new DecoderTextureFFmpeg();
}
DecoderStream::DecoderStreamElse()
{
debug_in_unity("DecoderStream", "DecoderStream"); // 1 comment
debug_in_unity("DecoderStream", "else"); // 4 comment
}
并在 Unity 端相应地调用它们
#if UNITY_ANDROID && !UNITY_EDITOR
DecoderStreamAndroid();
#elif UNITY_EDITOR
DecoderStreamEditor();
#else
DecoderStreamElse();
#endif
here might be the [Conditional]
属性的替代方法。但是,它不能用于返回值,只能用于 void
方法。
我有一个在 Unity 中使用的 .dll
(非托管代码)。我需要在 .dll
中添加一些功能,以便为 Android 设备构建 Unity 项目。
因此,在我的案例中,我需要使用 if
等预处理器功能
我试图将它添加到我的 .cpp
文件中以进行测试
DecoderStream::DecoderStream()
{
debug_in_unity("DecoderStream", "DecoderStream"); // 1 comment
#if UNITY_ANDROID && !UNITY_EDITOR
debug_in_unity("DecoderStream", "UNITY_ANDROID"); // 2 comment
#elif UNITY_EDITOR
debug_in_unity("DecoderStream", "UNITY_EDITOR"); // 3 comment
m_p_tex_decoder = new DecoderTextureFFmpeg();
#else
debug_in_unity("DecoderStream", "else"); // 4 comment
#endif
}
如果我编译此代码并在 Unity 中 运行 它,我会看到 1 comment
和 4 comment
我看到预处理器条件不起作用,我的意思是它无法识别无论是 android 还是 unity 编辑器...
我做错了什么?
来自 here,顾名思义
Pre-processors are a way of making text processing with your C program before they are actually compiled.
因此,如果您不编译已经定义相应指令的 DLL,您的代码基本上只会导致编译
DecoderStream::DecoderStream()
{
debug_in_unity("DecoderStream", "DecoderStream"); // 1 comment
debug_in_unity("DecoderStream", "else"); // 4 comment
}
使用它在 compile-time“知道”的指令。由于 DLL 已经被编译为机器代码,因此它不再“知道”pre-processors 的概念。
长话短说:您必须在 Unity 端使用 pre-processors 并实现不同的方法,例如
DecoderStream::DecoderStreamAndroid()
{
debug_in_unity("DecoderStream", "DecoderStream"); // 1 comment
debug_in_unity("DecoderStream", "UNITY_ANDROID"); // 2 comment
}
DecoderStream::DecoderStreamEditor()
{
debug_in_unity("DecoderStream", "DecoderStream"); // 1 comment
debug_in_unity("DecoderStream", "UNITY_EDITOR"); // 3 comment
m_p_tex_decoder = new DecoderTextureFFmpeg();
}
DecoderStream::DecoderStreamElse()
{
debug_in_unity("DecoderStream", "DecoderStream"); // 1 comment
debug_in_unity("DecoderStream", "else"); // 4 comment
}
并在 Unity 端相应地调用它们
#if UNITY_ANDROID && !UNITY_EDITOR
DecoderStreamAndroid();
#elif UNITY_EDITOR
DecoderStreamEditor();
#else
DecoderStreamElse();
#endif
here might be the [Conditional]
属性的替代方法。但是,它不能用于返回值,只能用于 void
方法。