【问题标题】:What Target Framework and References to use when compiling a DLL for Unity为 Unity 编译 DLL 时使用什么目标框架和引用
【发布时间】:2021-06-27 10:15:03
【问题描述】:

我有一些我在 Unity 2019.4 中通常使用的脚本,并且还编译成单独的 DLL 以在没有源代码的情况下分发。一切正常,直到我开始在几个地方使用 System.Array.Empty<T>(),这在 Unity 中运行良好,但阻止我编译单独的 DLL。

  • 最初,我将 Target Framework 设置为 Unity 3.5 .net full Base Class Libraries,但现在出现错误:'Array' does not contain a definition for 'Empty'
  • 显然Array.Empty 是在.NET Framework 4.6 中添加的,但如果我将目标框架设置为那个(或更高版本),我会得到:The type 'ISet<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 从我使用HashSet<T> 的任何地方(即使我没有使用@ 987654328@直接)。
  • 如果我从 Unity 安装目录手动添加对 System.dll 的引用,它会给出完全相同的错误,但会显示 ISet<> 现在在 netstandard dll 中。
  • 从 Unity 安装目录中添加对 netstandard.dll 的引用会得到 Predefined type 'System.Object' is not defined or imported
  • 尝试通过引用mscorlib.dll 来解决这个问题,得到The type 'HashSet<T>' exists in both 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' and 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'

那么我应该使用什么目标框架和参考来为 Unity 2019.4 编译 DLL?

【问题讨论】:

    标签: c# .net unity3d


    【解决方案1】:

    我通过复制 Unity 为装配定义生成的一个 csproj 文件并删除我不需要的东西来解决了这个问题。

    主要区别似乎是包含此块:

          <PropertyGroup>
            <NoConfig>true</NoConfig>
            <NoStdLib>true</NoStdLib>
            <AddAdditionalExplicitAssemblyReferences>false</AddAdditionalExplicitAssemblyReferences>
            <ImplicitlyExpandNETStandardFacades>false</ImplicitlyExpandNETStandardFacades>
            <ImplicitlyExpandDesignTimeFacades>false</ImplicitlyExpandDesignTimeFacades>
          </PropertyGroup>
    

    我猜它还可以定义自己对mscorlib.dllnetstandard.dllSystem.Core.dll 的引用,而不会与框架导入的引用发生冲突。

    【讨论】:

      猜你喜欢
      • 2017-10-19
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      相关资源
      最近更新 更多