Csproj file in unity changes every time I reload the project

I want to modify the 'csproj' file of my unity project to be able to access a specific library as this sugests answer.

I manually edit the file, but every time I reload the project, the 'csproj' file returns to its original state.

Is this a common problem? Is there a way to avoid this and keep changing the file?


EDIT: My goal is to use CSharpCodeProvider , so if there is another way to do this without modifying the "csproj" file, I'll happily take the approach

+3


source to share


2 answers


As far as I know, you cannot prevent Unity from overwriting your csproj files when you recompile your scripts.

When working with larger codebases and Unity3D, I tend to lean most of my code into separate .Net assemblies (DLLs). It helps me when I am sharing code between multiple projects and game servers, and Unity doesn't need to compile all scripts when changes are detected.

To do this, start your .Net IDE of choice and create a new class library project (which will output the DLL). Make sure it targets .Net Framework 3.5 or lower. Then you copy the output .dll file to the Resources folder of your Unity project during post-build. I like to use the post-build step so that I don't forget to copy the build after the change.



The DLL files in the project's Assets folder are automatically referenced by Unity when it creates its C # projects. This means that all the containing code can be used from your scripts. Note, however, that the other way will not work as easily: you cannot use code from Unity scripts in your own project.

Depending on what exactly you want to do with the CSharpCodeProvider , you can put your generation logic (and the required directive Import

) in such a separate project.

Edit: For more information on how we're building our project structure, you can watch this talk by Unite11's Jagged Alliance Online Lead Programmer.

+5


source


As Suigi suggested, I created a .dll file using the .Net IDE ( SharpDevelop ) and just copied it to the Unity Assets folder.

( How to create DLL file with SharpDevelop )

Here is the class I wrote to create the DLL file:



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;
        }
    }
}
}

      

To use it in any unity script, just use the 'dllTest' namespace. Then you can call AssemblyGeneration.generateAssemblyFromCode (...).

0


source







All Articles