[英]How to exclude UnityEditor reference from asmdef?
How to exclude UnityEditor reference from asmdef?如何从 asmdef 中排除 UnityEditor 引用?
Why I need it:为什么我需要它:
I have an asmdef file.我有一个 asmdef 文件。 For example, it is MyAssembly/MyAssembly.asmdef
.例如,它是MyAssembly/MyAssembly.asmdef
。 The MyAssembly
contains a lot of features and each feature staff is placed in its own folder. MyAssembly
包含许多功能,每个功能人员都放置在自己的文件夹中。 And some of these features has a code that is needed only in editor, and it refers to UnityEditor namespace.其中一些特性有一个只有在编辑器中才需要的代码,它引用了 UnityEditor 命名空间。 Such editor code is placed into an Editor folder.此类编辑器代码放置在 Editor 文件夹中。
But as you know, Editor folder name means nothing in terms of asmdef usage.但如您所知,Editor 文件夹名称对 asmdef 的使用没有任何意义。 So I add AssemblyDefenitionReference in each folder and refer it to the MyAssemblyEditor.asmdef
assembly definition.所以我在每个文件夹中添加了 AssemblyDefenitionReference 并将其引用到MyAssemblyEditor.asmdef
程序集定义。 So the paths looks like this:所以路径看起来像这样:
MyAssembly/MyAssembly.asmdef
MyAssembly/Editor/MyAssemblyEditor.asmdef
- this folder contains no code. MyAssembly/Editor/MyAssemblyEditor.asmdef
- 此文件夹不包含任何代码。 It's needed just to place asmdef, because it's not allowed to place two asmdefs in a single folder.只需要放置 asmdef,因为不允许在一个文件夹中放置两个 asmdef。MyAssembly/SomeFeature/Editor/*feature editor staff*
MyAssembly/SomeFeature/Editor/Editor.asmref
- refers to MyAssemblyEditor.asmdef
MyAssembly/SomeFeature/Editor/Editor.asmref
- 指MyAssemblyEditor.asmdef
MyAssembly/SomeFeature/*feature staff*
All this works good.这一切都很好。 But the problem is that, when some developer adds a new feature, he can forget to add a reference to the MyAssemblyEditor.asmdef
in the editor folder.但问题是,当一些开发人员添加新功能时,他可能忘记在编辑器文件夹中添加对MyAssemblyEditor.asmdef
的引用。 And there are no any errors will be shown in this case.在这种情况下不会显示任何错误。 This mistake will be revealed only when the build will be cooked.只有在构建完成时才会显示此错误。 But I'd like that using of UnityEditor in MyAssembly
will be instantly marked as an error.但我希望在MyAssembly
中使用 UnityEditor 会立即被标记为错误。
Feel free to suggest other solution for this problem.随意为这个问题提出其他解决方案。
This thread got me thinking I can use CsprojPostprocessor to remove all references to UnityEditor from my csproj file.这个线程让我想到我可以使用 CsprojPostprocessor 从我的 csproj 文件中删除对 UnityEditor 的所有引用。 I wrote such class:我写了这样的 class:
using System.Text.RegularExpressions;
using UnityEditor;
// ReSharper disable once CheckNamespace
public class CsprojPostprocessor : AssetPostprocessor
{
public static string OnGeneratedCSProject(string path, string content)
{
if (!path.EndsWith("Editor.csproj") && !path.EndsWith("Tests.csproj"))
{
var newContent =
Regex.Replace(content, "<Reference Include=\"UnityEditor(.|\n)*?</Reference>", "");
return newContent;
}
return content;
}
}
It also can be done with an xml parser or something.也可以使用 xml 解析器或其他东西来完成。
The only thing, that confuse me is that this mechanism is badly documented and doesn't look like something simple users should use.唯一让我感到困惑的是,这种机制的文档记录很差,看起来不像是简单的用户应该使用的东西。 So I use it at my own risk, but looks like there is no guarantee it will be strongly supported in future.所以我使用它需要自己承担风险,但看起来不能保证将来会得到强烈支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.