简体   繁体   English

每次我重新加载项目时,统一的 Csproj 文件都会更改

[英]Csproj file in unity changes everytime i reload the project

I want to change the 'csproj' file of my unity project in order to be able to access a specific library as this answer sugests.我想更改我的统一项目的“csproj”文件,以便能够按照答案的建议访问特定的库。

I am manually editing the file but every time i reload the project, the 'csproj' file returns to it's initial state.我正在手动编辑文件,但每次重新加载项目时,“csproj”文件都会返回到它的初始 state。

Is this a common issue?这是一个普遍的问题吗? Is there a way to avoid this and change the file permanently?有没有办法避免这种情况并永久更改文件?


EDIT: My goal is to use CSharpCodeProvider so if there is another way of doing it without changing the 'csproj' file, i will gladly adopt the approach编辑:我的目标是使用CSharpCodeProvider ,所以如果有另一种方法可以在不更改“csproj”文件的情况下进行,我会很乐意采用这种方法

As far as I know, you cannot prevent Unity from overwriting your csproj files upon recompilation of your scripts. 据我所知,您不能阻止Unity在重新编译脚本时覆盖您的csproj文件。

When working with larger code bases and Unity3D I tend to put most of my code into separate .Net assemblies (DLLs). 使用更大的代码库和Unity3D时,我倾向于将大部分代码放入单独的.Net程序集(DLL)中。 This helps me when I'm sharing code between multiple projects and game servers, and Unity doesn't need to compile all scripts when it detects changes. 当我在多个项目和游戏服务器之间共享代码时,这有助于我,Unity在检测到更改时不需要编译所有脚本。

To do that fire up your .Net IDE of choice and create a new class library project (which will output a .dll). 为此,启动您选择的.Net IDE并创建一个新的类库项目(将输出.dll)。 Make sure it is targeting the .Net Framework 3.5 or lower. 确保它的目标是.Net Framework 3.5或更低版本。 Then, you copy the output .dll into you Unity project's Assets folder in a post-build step. 然后,在构建后的步骤中将输出.dll复制到Unity项目的Assets文件夹中。 I like to use a post-build step to prevent me from forgetting to copy the assembly after a change. 我喜欢使用后期构建步骤来防止我在更改后忘记复制程序集。

DLLs in the project's Assets folder are automatically referenced by Unity when it creates its C# projects. 项目的Assets文件夹中的DLL在创建其C#项目时由Unity自动引用。 This means all of the containing code can be used from within your scripts. 这意味着可以在脚本中使用所有包含的代码。 Note, though, that the other way around will not work as easily: you cannot use code from Unity scripts in your separate project. 但请注意,相反的方法不会那么容易:您不能在单独的项目中使用Unity脚本中的代码。

Depending on what exactly you want to do with CSharpCodeProvider you can put your generation logic (and the necessary Import directive) into such a separate project. 根据您对CSharpCodeProvider的确切要求,您可以将生成逻辑(以及必要的Import指令)放入这样一个单独的项目中。

Edit: For more information how we set up our project structure you can watch this talk by the lead programmer on Jagged Alliance Online from Unite11. 编辑:有关我们如何设置项目结构的更多信息,您可以在Unite11上通过Jagged Alliance Online的首席程序员观看此演讲

Just for completeness:只是为了完整性:

There is an undocumented feature in Unity which allows you to edit the generated .csproj files (programmatically), by using an AssetPostprocessor : Unity 中有一个未记录的功能,它允许您使用AssetPostprocessor编辑生成的.csproj文件(以编程方式):

Inside any folder under Assets named Editor , create a class with the following structure:在名为EditorAssets下的任何文件夹中,创建具有以下结构的 class:

public class CsprojPostprocessor : AssetPostprocessor {
        public static string OnGeneratedCSProject(string path, string content) {
            return PatchCsprojContent(content);
        }
    }

where PathCsprojContent is a function which returns a patched version of the .csproj file with the additions you need.其中PathCsprojContent是 function,它返回.csproj文件的修补版本,其中添加了您需要的内容。

In case you want to patch only certain .csproj files, you can check the path beforehand.如果您只想修补某些.csproj文件,您可以事先检查路径。 For example, if you want to process only the Asset .csproj file called Assembly-CSharp.csproj , you can add the following check to the start of OnGeneratedCsProject :例如,如果您只想处理名为Assembly-CSharp.csproj的资产.csproj文件,则可以将以下检查添加到OnGeneratedCsProject的开头:

if (!path.EndsWith("Assembly-CSharp.csproj")) {
    return content;
}

Reference: https://learn.microsoft.com/en-us/visualstudio/gamedev/unity/extensibility/customize-project-files-created-by-vstu参考: https://learn.microsoft.com/en-us/visualstudio/gamedev/unity/extensibility/customize-project-files-created-by-vstu

As Suigi suggested, i have created a .dll file with the help of a .Net IDE ( SharpDevelop ) and just copied it in the 'Assets' folder of unity. 正如Suigi建议的那样,我在.Net IDE( SharpDevelop )的帮助下创建了一个.dll文件,并将其复制到了Unity的'Assets'文件夹中。

( How do i create a .dll file with SharpDevelop ) 如何使用SharpDevelop创建.dll文件

Here is the class that i wrote to produce the .dll file: 这是我编写的用于生成.dll文件的类:

using System;
using System.Collections.Generic;
using System.CodeDom.Compiler;
using System.Diagnostics;
using Microsoft.CSharp;
using System.Reflection;

namespace dllTest {

public class AssemblyGeneration {

    public static Assembly generateAssemblyFromCode(string code, string assemblyFilename) {
        CSharpCodeProvider codeProvider = new CSharpCodeProvider();
        System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
        parameters.GenerateExecutable = false;
        parameters.OutputAssembly = assemblyFilename + ".dll";
        CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, code);

        Assembly assembly = null;
        if (!results.Errors.HasErrors) {
            assembly = results.CompiledAssembly;
            return assembly;
        }
        else {
            return null;
        }
    }
}
}

In order to use it in any unity script, just use the namespace 'dllTest'. 要在任何统一脚本中使用它,只需使用命名空间'dllTest'。 Then, you can call AssemblyGeneration.generateAssemblyFromCode(...) . 然后,您可以调用AssemblyGeneration.generateAssemblyFromCode(...)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM