Hi all, I hope everyone is doing well. I am creating a game where the user/player can customize components by using C# scripts that get automatically compiled, loaded in as assemblies, and then executed (sort of like how applications will have plugin systems that add additional modded behavior or features).
I can dynamically compile scripts into assemblies without any issue when running the game within the Unity editor (ver 2021.3.15f1), however the compilation does NOT work when exported to a standalone Windows application. I get this error when compilation is attempted: “Error running <projects_dir>\mono\mini\mono.exe: The system cannot find the file specified.”
I have tried to fix this in a variety of ways, from downloading mono to see if there’s something I can fix myself, to trying to rig directory paths to make this work - however I have run into a wall with every attempt to fix this.
I am desperately hoping someone can assist me in this. My whole project might have to get flushed if I cannot make compilation happen within an exported application.
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
if (provider != null)
{
string[] sourceFiles = sourceFilePathList.ToArray();
string outputAssemblyDirectory = Path.GetDirectoryName(sourceFiles[0]);
string outputAssemblyFileName = Path.GetFileNameWithoutExtension("OutputFileName");
string dllFileName = $"{outputAssemblyDirectory}\\{outputAssemblyFileName}.dll";
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = false; // Generate a dll instead of an exe
cp.OutputAssembly = dllFileName; // Specify the assembly file name to generate
cp.GenerateInMemory = false; // Save the assembly as a physical file
cp.TreatWarningsAsErrors = false; // Set whether to treat all warnings as errors
// Error is produced on the following line in exported app, but works fine in Unity editor:
CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceFiles);
}
Please help! Thank you so much!
-Jason