【问题标题】:How to define preprocessor ifdef in .dll for Unity?如何在 .dll 中为 Unity 定义预处理器 ifdef?
【发布时间】:2020-10-05 07:57:37
【问题描述】:

我有一个在 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 comment4 comment 我看到预处理器条件不起作用,我的意思是它无法识别无论是 android 还是 unity 编辑器...

我做错了什么?

【问题讨论】:

  • 您是否在不同的环境中重新构建(您希望设置/取消设置开关)并相应地使用生成的 dll?

标签: c++ unity3d c-preprocessor


【解决方案1】:

来自here,顾名思义

预处理器是一种在您的 C 程序在实际编译之前进行文本处理的方法。

因此,如果您不编译已经定义了相应指令的 DLL,您的代码基本上只会导致编译

DecoderStream::DecoderStream()
{
    debug_in_unity("DecoderStream", "DecoderStream");  // 1 comment

    debug_in_unity("DecoderStream", "else"); // 4 comment 
}

在编译时使用它“知道”的指令。而且由于 DLL 已经被编译为机器代码,它不再“知道”预处理器的概念。

长话短说:您将不得不在 Unity 端使用预处理器并实现不同的方法,例如

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 的替代品可能是[Conditional] 属性。但是,它不能用于返回值,只能用于void 方法。

【讨论】:

    猜你喜欢
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    相关资源
    最近更新 更多