Hello,
I am making a game in which a player can type his own script and play it runtime. It works perfectly fine in Unity Editor but when i try building the game and run the script i get an error which i can’t solve. I hope you guys can help me.
My project is set:
Scripting Backend: Mono
Api: .NET 4.x
public void PlayScript()
{
if (compilationWindow.activeSelf == true)
return;
StringBuilder source = new StringBuilder();
source.Append("using UnityEngine;\nusing SplitAndMerge;\npublic class PlayersCode{\npublic static void Run(){\nSplitAndMerge.Program.MainUnity(new string []{\"\n");
source.Append(scriptText.text.Replace("\u0022", "\u005c\u0022"));
source.Append("\"});}}");
var assembly = Kompiluj(source.ToString().Replace("\n", ""));
var method = assembly.GetType("PlayersCode").GetMethod("Run");
var del = (Action)Delegate.CreateDelegate(typeof(Action), method);
del.Invoke();
showCompilationError();
}
........
private Assembly Kompiluj(string kod)
{
var provider = new CSharpCodeProvider();
var param = new CompilerParameters()
{
GenerateExecutable = false,
GenerateInMemory = true
};
//standardowe assembly z projektu itp
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
param.ReferencedAssemblies.Add(assembly.Location);
}
//dodatkowe assembly
param.ReferencedAssemblies.Add("System.dll");
param.ReferencedAssemblies.Add("Assets/Resources/cscs.dll");
var results = provider.CompileAssemblyFromSource(param, kod);
Debug.Log(results.Errors.Count);
if (results.Errors.Count > 0)
{
foreach (CompilerError CompErr in results.Errors)
{
Debug.Log(
"Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";" +
Environment.NewLine + Environment.NewLine);
}
}
return results.CompiledAssembly;
}
I tried creating the folder there and i copy pasted files from unity, it “worked” but i still got another error :
Exception: Compiler failed to produce the assembly. Output: ‘’
at Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch
