简体   繁体   English

如何从 asmdef 中排除 UnityEditor 引用?

[英]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.

相关问题 错误:“名为 &#39;UnityEditor.StandardEvents&#39; 的程序集已经存在 (Packages/com.unity.standardevents/Editor/Editor.asmdef)” - Error: “Assembly with name 'UnityEditor.StandardEvents' already exists (Packages/com.unity.standardevents/Editor/Editor.asmdef)” Unity命令行构建需要UnityEditor.BuildPipeline,但是由于缺少“ UnityEditor”参考,构建失败 - Unity command-line build requires UnityEditor.BuildPipeline, but builds fail for missing 'UnityEditor' reference 来自UnityEditor.InspectorWindow.GetEditorsWithPreviews的NullReferenceException - NullReferenceException from UnityEditor.InspectorWindow.GetEditorsWithPreviews 如何在Unity中创建对话框(不使用UnityEditor)? - How to create dialogbox in Unity (not using UnityEditor)? 重命名外部dll中的类,而不会在unityEditor中丢失对它的引用 - Rename class in external dll without losing reference to it in unityEditor Unity 2020 和 Firebase 上的问题(无法解析参考“UnityEditor.iOS.Extensions.Xcode”) - Problem on Unity 2020 and Firebase (Unable to resolve reference 'UnityEditor.iOS.Extensions.Xcode') 错误 CS0234:命名空间“UnityEditor”中不存在类型或命名空间名称“iOS”。 您是否缺少程序集参考? - error CS0234: The type or namespace name `iOS' does not exist in the namespace `UnityEditor'. Are you missing an assembly reference? 如何获取当前目录的路径? (ScriptableObject, UnityEditor.AssetDatabase) - How to get the path to the current directory? (ScriptableObject, UnityEditor.AssetDatabase) 如何在UnityEditor中正确关闭socket.io连接? - How to close a socket.io connection correctly in UnityEditor? 如何从 OnCollisionStay 中排除与地面的碰撞? - How to exclude collisions with ground from OnCollisionStay?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM